Plugin config options with no defaults

This commit is contained in:
Justin Moon 2022-07-02 00:13:49 -04:00 committed by Christian Decker
parent 76623b2ef2
commit f111d6772d
2 changed files with 32 additions and 13 deletions

View file

@ -5,6 +5,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
extern crate log; extern crate log;
use log::trace; use log::trace;
use messages::Configuration; use messages::Configuration;
use options::ConfigOption;
use std::collections::HashMap; use std::collections::HashMap;
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
@ -14,7 +15,6 @@ use tokio::sync::Mutex;
use tokio_stream::StreamExt; use tokio_stream::StreamExt;
use tokio_util::codec::FramedRead; use tokio_util::codec::FramedRead;
use tokio_util::codec::FramedWrite; use tokio_util::codec::FramedWrite;
use options::ConfigOption;
pub mod codec; pub mod codec;
pub mod logging; pub mod logging;
@ -25,7 +25,6 @@ extern crate serde_json;
pub mod options; pub mod options;
/// Need to tell us about something that went wrong? Use this error /// Need to tell us about something that went wrong? Use this error
/// type to do that. Use this alias to be safe from future changes in /// type to do that. Use this alias to be safe from future changes in
/// our internal error handling, since we'll implement any necessary /// our internal error handling, since we'll implement any necessary
@ -329,16 +328,25 @@ where
// Match up the ConfigOptions and fill in their values if we // Match up the ConfigOptions and fill in their values if we
// have a matching entry. // have a matching entry.
for opt in self.options.iter_mut() { for opt in self.options.iter_mut() {
if let Some(val) = call.options.get(opt.name()) { let val = call.options.get(opt.name());
opt.value = Some(match (opt.default(), &val) { opt.value = match (&opt, &opt.default(), &val) {
(OValue::String(_), JValue::String(s)) => OValue::String(s.clone()), (_, OValue::String(_), Some(JValue::String(s))) => Some(OValue::String(s.clone())),
(OValue::Integer(_), JValue::Number(n)) => OValue::Integer(n.as_i64().unwrap()), (_, OValue::OptString, Some(JValue::String(s))) => Some(OValue::String(s.clone())),
(OValue::Boolean(_), JValue::Bool(n)) => OValue::Boolean(*n), (_, OValue::OptString, None) => None,
// It's ok to panic, if we get here Core Lightning (_, OValue::Integer(_), Some(JValue::Number(s))) => {
// has not enforced the option type. Some(OValue::Integer(s.as_i64().unwrap()))
(_, _) => panic!("Mismatching types in options: {:?} != {:?}", opt, val), }
}); (_, OValue::OptInteger, 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,
(o, _, _) => panic!("Type mismatch for option {:?}", o),
} }
} }

View file

@ -1,11 +1,14 @@
use serde::ser::{SerializeStruct, Serializer}; use serde::ser::{SerializeStruct, Serializer};
use serde::{Serialize}; use serde::Serialize;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Value { pub enum Value {
String(String), String(String),
Integer(i64), Integer(i64),
Boolean(bool), Boolean(bool),
OptString,
OptInteger,
OptBoolean,
} }
/// An stringly typed option that is passed to /// An stringly typed option that is passed to
@ -45,11 +48,19 @@ impl Serialize for ConfigOption {
s.serialize_field("type", "int")?; s.serialize_field("type", "int")?;
s.serialize_field("default", i)?; s.serialize_field("default", i)?;
} }
Value::Boolean(b) => { Value::Boolean(b) => {
s.serialize_field("type", "bool")?; s.serialize_field("type", "bool")?;
s.serialize_field("default", b)?; s.serialize_field("default", b)?;
} }
Value::OptString => {
s.serialize_field("type", "string")?;
}
Value::OptInteger => {
s.serialize_field("type", "int")?;
}
Value::OptBoolean => {
s.serialize_field("type", "bool")?;
}
} }
s.serialize_field("description", &self.description)?; s.serialize_field("description", &self.description)?;