Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cln_plugin Refactor ConfigOption #7042

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions plugins/examples/cln-plugin-startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ async fn main() -> Result<(), anyhow::Error> {
let state = ();

if let Some(plugin) = Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new(
"test-option",
options::Value::Integer(42),
"a test-option with default 42",
))
.option(options::ConfigOption::new(
"opt-option",
options::Value::OptInteger,
"An optional option",
))
.option(
options::ConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
)
.build(),
)
.option(options::ConfigOption::new_opt_i64("opt-option", "An optional option").build())
.rpcmethod("testmethod", "This is a test", testmethod)
.rpcmethod(
"testoptions",
Expand Down
6 changes: 3 additions & 3 deletions plugins/grpc-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ async fn main() -> Result<()> {
let directory = std::env::current_dir()?;

let plugin = match Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new(
.option(options::ConfigOption::new_i64_with_default(
"grpc-port",
options::Value::Integer(-1),
-1,
"Which port should the grpc plugin listen for incoming connections?",
))
).build())
.configure()
.await?
{
Expand Down
37 changes: 17 additions & 20 deletions plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
extern crate log;
use log::trace;
use messages::{Configuration, FeatureBits, NotificationTopic};
use options::ConfigOption;
use options::UntypedConfigOption;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
Expand Down Expand Up @@ -43,7 +43,7 @@ where
output: Option<O>,

hooks: HashMap<String, Hook<S>>,
options: HashMap<String, ConfigOption>,
options: HashMap<String, UntypedConfigOption>,
rpcmethods: HashMap<String, RpcMethod<S>>,
subscriptions: HashMap<String, Subscription<S>>,
notifications: Vec<NotificationTopic>,
Expand All @@ -65,7 +65,7 @@ where
init_id: serde_json::Value,
input: FramedRead<I, JsonRpcCodec>,
output: Arc<Mutex<FramedWrite<O, JsonCodec>>>,
options: HashMap<String, ConfigOption>,
options: HashMap<String, UntypedConfigOption>,
configuration: Configuration,
rpcmethods: HashMap<String, AsyncCallback<S>>,
hooks: HashMap<String, AsyncCallback<S>>,
Expand Down Expand Up @@ -99,7 +99,7 @@ where
/// The state gets cloned for each request
state: S,
/// "options" field of "init" message sent by cln
options: HashMap<String, ConfigOption>,
options: HashMap<String, UntypedConfigOption>,
/// "configuration" field of "init" message sent by cln
configuration: Configuration,
/// A signal that allows us to wait on the plugin's shutdown.
Expand Down Expand Up @@ -130,7 +130,7 @@ where
}
}

pub fn option(mut self, opt: options::ConfigOption) -> Builder<S, I, O> {
pub fn option(mut self, opt: options::UntypedConfigOption) -> Builder<S, I, O> {
self.options.insert(opt.name().to_string(), opt);
self
}
Expand Down Expand Up @@ -389,23 +389,20 @@ where
// have a matching entry.
for (_name, opt) in self.options.iter_mut() {
let val = call.options.get(opt.name());
opt.value = match (&opt, &opt.default(), &val) {
(_, OValue::String(_), Some(JValue::String(s))) => Some(OValue::String(s.clone())),
(_, OValue::OptString, Some(JValue::String(s))) => Some(OValue::String(s.clone())),
(_, OValue::OptString, None) => None,

(_, OValue::Integer(_), Some(JValue::Number(s))) => {
Some(OValue::Integer(s.as_i64().unwrap()))
opt.value = match (&opt.value, &opt.default(), &val) {
(_, Some(OValue::String(_)), Some(JValue::String(s))) => {
Some(OValue::String(s.clone()))
}
(_, OValue::OptInteger, Some(JValue::Number(s))) => {
(_, None, Some(JValue::String(s))) => Some(OValue::String(s.clone())),
(_, None, None) => None,

(_, Some(OValue::Integer(_)), Some(JValue::Number(s))) => {
Some(OValue::Integer(s.as_i64().unwrap()))
}
(_, OValue::OptInteger, None) => None,

(_, OValue::Boolean(_), Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)),
(_, OValue::OptBoolean, Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)),
(_, OValue::OptBoolean, None) => None,

(_, None, Some(JValue::Number(s))) => Some(OValue::Integer(s.as_i64().unwrap())),
(_, Some(OValue::Boolean(_)), Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)),
(_, None, Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)),
(o, _, _) => panic!("Type mismatch for option {:?}", o),
}
}
Expand Down Expand Up @@ -591,7 +588,7 @@ where
}

pub fn option(&self, name: &str) -> Option<options::Value> {
self.options.get(name).and_then(|x| x.value.clone())
self.options.get(name).and_then(|c| c.value.clone())
}

/// return the cln configuration send to the
Expand Down Expand Up @@ -731,7 +728,7 @@ impl<S> Plugin<S>
where
S: Clone + Send,
{
pub fn options(&self) -> Vec<ConfigOption> {
pub fn options(&self) -> Vec<UntypedConfigOption> {
self.options.values().cloned().collect()
}
pub fn configuration(&self) -> Configuration {
Expand Down
6 changes: 3 additions & 3 deletions plugins/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::options::ConfigOption;
use crate::options::UntypedConfigOption;
use serde::de::{self, Deserializer};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -171,7 +171,7 @@ impl NotificationTopic {

#[derive(Serialize, Default, Debug)]
pub(crate) struct GetManifestResponse {
pub(crate) options: Vec<ConfigOption>,
pub(crate) options: Vec<UntypedConfigOption>,
pub(crate) rpcmethods: Vec<RpcMethod>,
pub(crate) subscriptions: Vec<String>,
pub(crate) notifications: Vec<NotificationTopic>,
Expand All @@ -180,7 +180,7 @@ pub(crate) struct GetManifestResponse {
pub(crate) featurebits: FeatureBits,
pub(crate) nonnumericids: bool,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) custommessages : Vec<u16>
pub(crate) custommessages: Vec<u16>,
}

#[derive(Serialize, Default, Debug, Clone)]
Expand Down
Loading