use serde for cli config parse

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2023-05-18 12:24:50 -04:00
parent a560b28829
commit 9073db2622
2 changed files with 82 additions and 81 deletions

View File

@@ -1,11 +1,13 @@
use std::{
collections::HashMap,
fmt::Display,
num::{NonZeroU64, NonZeroUsize},
str::FromStr,
time::SystemTime,
};
use byteorder::{BigEndian, ReadBytesExt};
use serde::{Deserialize, Serialize};
use serde::{de::value::MapDeserializer, Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use strum_macros;
use utils::{
@@ -147,19 +149,56 @@ impl std::ops::Deref for TenantCreateRequest {
}
}
pub fn deserialize_option_number_from_string<'de, T, D>(
deserializer: D,
) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: FromStr + serde::Deserialize<'de>,
<T as FromStr>::Err: Display,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum NumericOrNull<'a, T> {
String(String),
Str(&'a str),
FromStr(T),
Null,
}
match NumericOrNull::<T>::deserialize(deserializer)? {
NumericOrNull::String(s) => match s.as_str() {
"" => Ok(None),
_ => T::from_str(&s).map(Some).map_err(serde::de::Error::custom),
},
NumericOrNull::Str(s) => match s {
"" => Ok(None),
_ => T::from_str(s).map(Some).map_err(serde::de::Error::custom),
},
NumericOrNull::FromStr(i) => Ok(Some(i)),
NumericOrNull::Null => Ok(None),
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct TenantConfig {
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub checkpoint_distance: Option<u64>,
pub checkpoint_timeout: Option<String>,
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub compaction_target_size: Option<u64>,
pub compaction_period: Option<String>,
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub compaction_threshold: Option<usize>,
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub gc_horizon: Option<u64>,
pub gc_period: Option<String>,
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub image_creation_threshold: Option<usize>,
pub pitr_interval: Option<String>,
pub walreceiver_connect_timeout: Option<String>,
pub lagging_wal_timeout: Option<String>,
#[serde(default, deserialize_with = "deserialize_option_number_from_string")]
pub max_lsn_wal_lag: Option<NonZeroU64>,
pub trace_read_requests: Option<bool>,
// We defer the parsing of the eviction_policy field to the request handler.
@@ -167,10 +206,29 @@ pub struct TenantConfig {
// We might do that once the eviction feature has stabilizied.
// For now, this field is not even documented in the openapi_spec.yml.
pub eviction_policy: Option<serde_json::Value>,
#[serde(default)]
#[serde(deserialize_with = "deserialize_option_number_from_string")]
pub min_resident_size_override: Option<u64>,
pub evictions_low_residence_duration_metric_threshold: Option<String>,
}
impl TenantConfig {
pub fn deserialize_from_settings(
settings: HashMap<&str, &str>,
) -> Result<Self, serde_json::Error> {
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TenantConfigWrapper {
#[serde(flatten)]
config: TenantConfig,
}
let config = TenantConfigWrapper::deserialize(
MapDeserializer::<_, serde_json::Error>::new(settings.into_iter()),
)?;
Ok(config.config)
}
}
#[serde_as]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
@@ -817,16 +875,29 @@ mod tests {
err
);
let attach_request = json!({
"config": {
"unknown_field": "unknown_value".to_string(),
},
});
let err = serde_json::from_value::<TenantAttachRequest>(attach_request).unwrap_err();
let config = HashMap::from_iter(std::iter::once(("unknown_field", "unknown_value")));
let err = TenantConfig::deserialize_from_settings(config).unwrap_err();
assert!(
err.to_string().contains("unknown field `unknown_field`"),
"expect unknown field `unknown_field` error, got: {}",
err
);
let config = HashMap::from_iter(std::iter::once(("checkpoint_distance", "not_a_number")));
let err = TenantConfig::deserialize_from_settings(config).unwrap_err();
assert_eq!(err.to_string(), "invalid digit found in string");
}
#[test]
fn test_deserialize_maybe_number() {
let res =
serde_json::from_str::<TenantConfig>(r#"{ "checkpoint_distance": 233 }"#).unwrap();
assert_eq!(res.checkpoint_distance, Some(233));
let res =
serde_json::from_str::<TenantConfig>(r#"{ "checkpoint_distance": "233" }"#).unwrap();
assert_eq!(res.checkpoint_distance, Some(233));
let config = HashMap::from_iter(std::iter::once(("checkpoint_distance", "233")));
let res = TenantConfig::deserialize_from_settings(config).unwrap();
assert_eq!(res.checkpoint_distance, Some(233));
}
}