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 2 commits
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
67 changes: 28 additions & 39 deletions plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use futures::sink::SinkExt;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
extern crate log;
use log::trace;
use messages::{Configuration, NotificationTopic, FeatureBits};
use options::ConfigOption;
use messages::{Configuration, FeatureBits, NotificationTopic};
use options::UntypedConfigOption;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
Expand Down Expand Up @@ -43,11 +43,11 @@ where
output: Option<O>,

hooks: HashMap<String, Hook<S>>,
options: Vec<ConfigOption>,
options: HashMap<String, UntypedConfigOption>,
rpcmethods: HashMap<String, RpcMethod<S>>,
subscriptions: HashMap<String, Subscription<S>>,
notifications: Vec<NotificationTopic>,
custommessages : Vec<u16>,
custommessages: Vec<u16>,
featurebits: FeatureBits,
dynamic: bool,
// Do we want the plugin framework to automatically register a logging handler?
Expand All @@ -65,7 +65,7 @@ where
init_id: serde_json::Value,
input: FramedRead<I, JsonRpcCodec>,
output: Arc<Mutex<FramedWrite<O, JsonCodec>>>,
options: Vec<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: Vec<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 All @@ -120,18 +120,18 @@ where
output: Some(output),
hooks: HashMap::new(),
subscriptions: HashMap::new(),
options: vec![],
options: HashMap::new(),
rpcmethods: HashMap::new(),
notifications: vec![],
featurebits: FeatureBits::default(),
dynamic: false,
custommessages : vec![],
custommessages: vec![],
logging: true,
}
}

pub fn option(mut self, opt: options::ConfigOption) -> Builder<S, I, O> {
self.options.push(opt);
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 @@ -245,7 +245,7 @@ where

/// Tells lightningd explicitly to allow custommmessages of the provided
/// type
pub fn custommessages(mut self, custommessages : Vec<u16>) -> Self {
pub fn custommessages(mut self, custommessages: Vec<u16>) -> Self {
self.custommessages = custommessages;
self
}
Expand Down Expand Up @@ -369,15 +369,15 @@ where
.collect();

messages::GetManifestResponse {
options: self.options.clone(),
options: self.options.values().cloned().collect(),
subscriptions: self.subscriptions.keys().map(|s| s.clone()).collect(),
hooks: self.hooks.keys().map(|s| s.clone()).collect(),
rpcmethods,
notifications: self.notifications.clone(),
featurebits: self.featurebits.clone(),
dynamic: self.dynamic,
nonnumericids: true,
custommessages : self.custommessages.clone()
custommessages: self.custommessages.clone(),
}
}

Expand All @@ -387,25 +387,22 @@ where

// Match up the ConfigOptions and fill in their values if we
// have a matching entry.
for opt in self.options.iter_mut() {
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 @@ -506,11 +503,7 @@ where
S: Clone + Send,
{
pub fn option(&self, name: &str) -> Option<options::Value> {
self.options
.iter()
.filter(|o| o.name() == name)
.next()
.map(|co| co.value.clone().unwrap_or(co.default().clone()))
self.options.get(name).and_then(|x| x.value.clone())
}
}

Expand Down Expand Up @@ -595,11 +588,7 @@ where
}

pub fn option(&self, name: &str) -> Option<options::Value> {
self.options
.iter()
.filter(|o| o.name() == name)
.next()
.map(|co| co.value.clone().unwrap_or(co.default().clone()))
self.options.get(name).and_then(|c| c.value.clone())
}

/// return the cln configuration send to the
Expand Down Expand Up @@ -739,8 +728,8 @@ impl<S> Plugin<S>
where
S: Clone + Send,
{
pub fn options(&self) -> Vec<ConfigOption> {
self.options.clone()
pub fn options(&self) -> Vec<UntypedConfigOption> {
self.options.values().cloned().collect()
}
pub fn configuration(&self) -> Configuration {
self.configuration.clone()
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