Skip to content

Commit 7ffd0a3

Browse files
daywalker90rustyrussell
authored andcommitted
cln-plugin: add multi options for String and i64
Changelog-Added: cln-plugin: add multi options for String and i64
1 parent 88504ea commit 7ffd0a3

File tree

4 files changed

+348
-9
lines changed

4 files changed

+348
-9
lines changed

plugins/examples/cln-plugin-startup.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#[macro_use]
44
extern crate serde_json;
55
use cln_plugin::options::{
6-
self, BooleanConfigOption, DefaultIntegerConfigOption, IntegerConfigOption,
6+
self, BooleanConfigOption, DefaultIntegerArrayConfigOption, DefaultIntegerConfigOption,
7+
DefaultStringArrayConfigOption, IntegerArrayConfigOption, IntegerConfigOption,
8+
StringArrayConfigOption,
79
};
810
use cln_plugin::{messages, Builder, Error, Plugin};
9-
use tokio;
1011

1112
const TEST_NOTIF_TAG: &str = "test_custom_notification";
1213

@@ -19,6 +20,32 @@ const TEST_OPTION: DefaultIntegerConfigOption = DefaultIntegerConfigOption::new_
1920
const TEST_OPTION_NO_DEFAULT: IntegerConfigOption =
2021
IntegerConfigOption::new_i64_no_default("opt-option", "An option without a default");
2122

23+
const TEST_MULTI_STR_OPTION: StringArrayConfigOption =
24+
StringArrayConfigOption::new_str_arr_no_default(
25+
"multi-str-option",
26+
"An option that can have multiple string values",
27+
);
28+
29+
const TEST_MULTI_STR_OPTION_DEFAULT: DefaultStringArrayConfigOption =
30+
DefaultStringArrayConfigOption::new_str_arr_with_default(
31+
"multi-str-option-default",
32+
"Default1",
33+
"An option that can have multiple string values with defaults",
34+
);
35+
36+
const TEST_MULTI_I64_OPTION: IntegerArrayConfigOption =
37+
IntegerArrayConfigOption::new_i64_arr_no_default(
38+
"multi-i64-option",
39+
"An option that can have multiple i64 values",
40+
);
41+
42+
const TEST_MULTI_I64_OPTION_DEFAULT: DefaultIntegerArrayConfigOption =
43+
DefaultIntegerArrayConfigOption::new_i64_arr_with_default(
44+
"multi-i64-option-default",
45+
-42,
46+
"An option that can have multiple i64 values with defaults",
47+
);
48+
2249
#[tokio::main]
2350
async fn main() -> Result<(), anyhow::Error> {
2451
let state = ();
@@ -33,6 +60,10 @@ async fn main() -> Result<(), anyhow::Error> {
3360
.option(TEST_OPTION)
3461
.option(TEST_OPTION_NO_DEFAULT)
3562
.option(test_dynamic_option)
63+
.option(TEST_MULTI_STR_OPTION)
64+
.option(TEST_MULTI_STR_OPTION_DEFAULT)
65+
.option(TEST_MULTI_I64_OPTION)
66+
.option(TEST_MULTI_I64_OPTION_DEFAULT)
3667
.setconfig_callback(setconfig_callback)
3768
.rpcmethod("testmethod", "This is a test", testmethod)
3869
.rpcmethod(
@@ -79,10 +110,18 @@ async fn setconfig_callback(
79110
async fn testoptions(p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
80111
let test_option = p.option(&TEST_OPTION)?;
81112
let test_option_no_default = p.option(&TEST_OPTION_NO_DEFAULT)?;
113+
let test_multi_str_option = p.option(&TEST_MULTI_STR_OPTION)?;
114+
let test_multi_str_option_default = p.option(&TEST_MULTI_STR_OPTION_DEFAULT)?;
115+
let test_multi_i64_option = p.option(&TEST_MULTI_I64_OPTION)?;
116+
let test_multi_i64_option_default = p.option(&TEST_MULTI_I64_OPTION_DEFAULT)?;
82117

83118
Ok(json!({
84119
"test-option": test_option,
85-
"opt-option" : test_option_no_default
120+
"opt-option" : test_option_no_default,
121+
"multi-str-option": test_multi_str_option,
122+
"multi-str-option-default": test_multi_str_option_default,
123+
"multi-i64-option": test_multi_i64_option,
124+
"multi-i64-option-default": test_multi_i64_option_default,
86125
}))
87126
}
88127

plugins/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ where
437437
let option_value: Option<options::Value> = match (json_value, default_value) {
438438
(None, None) => None,
439439
(None, Some(default)) => Some(default.clone()),
440+
(Some(JValue::Array(a)), _) => match a.first() {
441+
Some(JValue::String(_)) => Some(OValue::StringArray(
442+
a.iter().map(|x| x.as_str().unwrap().to_string()).collect(),
443+
)),
444+
Some(JValue::Number(_)) => Some(OValue::IntegerArray(
445+
a.iter().map(|x| x.as_i64().unwrap()).collect(),
446+
)),
447+
_ => panic!("Array type not supported for option: {}", name),
448+
},
440449
(Some(JValue::String(s)), _) => Some(OValue::String(s.to_string())),
441450
(Some(JValue::Number(i)), _) => Some(OValue::Integer(i.as_i64().unwrap())),
442451
(Some(JValue::Bool(b)), _) => Some(OValue::Boolean(*b)),

0 commit comments

Comments
 (0)