From: Otto Moerbeek Date: Tue, 28 Oct 2025 15:30:40 +0000 (+0100) Subject: Handle values set to default values. X-Git-Tag: rec-5.4.0-alpha1~108^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d770f8120c11e0260736abeb741abdaeb6ca28b0;p=thirdparty%2Fpdns.git Handle values set to default values. Signed-off-by: Otto Moerbeek --- diff --git a/pdns/recursordist/rec-rust-lib/cxxsettings.hh b/pdns/recursordist/rec-rust-lib/cxxsettings.hh index cf4fc318cb..9e82016d6d 100644 --- a/pdns/recursordist/rec-rust-lib/cxxsettings.hh +++ b/pdns/recursordist/rec-rust-lib/cxxsettings.hh @@ -43,7 +43,7 @@ void oldStyleForwardsFileToBridgeStruct(const std::string& filename, ::rust::Vec void oldStyleAllowFileToBridgeStruct(const std::string& filename, ::rust::Vec<::rust::String>& vec); bool oldKVToBridgeStruct(string& key, const string& value, ::rust::String& section, ::rust::String& fieldname, ::rust::String& type_name, pdns::rust::settings::rec::Value& rustvalue); std::string oldStyleSettingsFileToYaml(const string& fname, bool mainFile); -std::string defaultsToYaml(); +std::string defaultsToYaml(bool postProcess = true); YamlSettingsStatus readYamlSettings(const std::string& configname, const std::string& includeDirOnCommandLine, rust::settings::rec::Recursorsettings& settings, std::string& msg, Logr::log_t log); void processAPIDir(const string& includeDirOnCommandLine, pdns::rust::settings::rec::Recursorsettings& settings, Logr::log_t log); void bridgeStructToOldStyleSettings(const pdns::rust::settings::rec::Recursorsettings& settings); diff --git a/pdns/recursordist/rec-rust-lib/cxxsupport.cc b/pdns/recursordist/rec-rust-lib/cxxsupport.cc index 0211735858..0c7a7cb918 100644 --- a/pdns/recursordist/rec-rust-lib/cxxsupport.cc +++ b/pdns/recursordist/rec-rust-lib/cxxsupport.cc @@ -631,7 +631,7 @@ std::string pdns::settings::rec::oldStyleSettingsFileToYaml(const string& fname, return std::string(pdns::rust::settings::rec::map_to_yaml_string(vec)); } -std::string pdns::settings::rec::defaultsToYaml() +std::string pdns::settings::rec::defaultsToYaml(bool postProcess) { // In this function we make use of the fact that we know a little about the formatting of YAML by // serde_yaml. ATM there's no way around that, as serde_yaml itself does not have any support for @@ -689,6 +689,9 @@ std::string pdns::settings::rec::defaultsToYaml() } const auto defs = std::string(pdns::rust::settings::rec::map_to_yaml_string(vec)); + if (!postProcess) { + return defs; + } // We now have a YAML string, with all sections and all default values. Do a litle bit of parsing // to insert the help text lines. std::vector lines; diff --git a/pdns/recursordist/rec-rust-lib/rust-bridge-in.rs b/pdns/recursordist/rec-rust-lib/rust-bridge-in.rs index 9963ef2376..1e75d07b15 100644 --- a/pdns/recursordist/rec-rust-lib/rust-bridge-in.rs +++ b/pdns/recursordist/rec-rust-lib/rust-bridge-in.rs @@ -403,7 +403,7 @@ extern "Rust" { // Prdoduce a YAML formatted string given a data structure known to Serde fn to_yaml_string(self: &Recursorsettings) -> Result; - fn get_value(self: &Recursorsettings, field: &Vec) -> Result; + fn get_value(self: &Recursorsettings, field: &Vec, defaults: &str) -> Result; // When doing a conversion of old-style to YAML style we use a vector of OldStyle structs fn map_to_yaml_string(map: &Vec) -> Result; fn forward_zones_to_yaml_string(vec: &Vec) -> Result; diff --git a/pdns/recursordist/rec-rust-lib/rust/src/bridge.rs b/pdns/recursordist/rec-rust-lib/rust/src/bridge.rs index cc218652ae..0000a8c5c2 100644 --- a/pdns/recursordist/rec-rust-lib/rust/src/bridge.rs +++ b/pdns/recursordist/rec-rust-lib/rust/src/bridge.rs @@ -942,12 +942,7 @@ impl Recursorsettings { } } - pub fn get_value(&self, field: &Vec) -> Result { - let value = serde_yaml::to_value(self); - let value = match value { - Ok(value) => value, - Err(error) => return Err(std::io::Error::other(error.to_string())) - }; + fn get_value1(value: &serde_yaml::Value, field: &[String]) -> Result { if let Some(map) = value.as_mapping() { match field.len() { 0 => { @@ -969,6 +964,25 @@ impl Recursorsettings { Err(std::io::Error::other(field[0].to_owned() + ": not a map")) } + pub fn get_value(&self, field: &Vec, defaults: &str) -> Result { + let value = serde_yaml::to_value(self); + let value = match value { + Ok(value) => value, + Err(error) => return Err(std::io::Error::other(error.to_string())) + }; + match Self::get_value1(&value, field) { + Ok(result) => Ok(result), + Err(_) => { + let defaults_value: serde_yaml::Value = serde_yaml::from_str(defaults).unwrap(); + let yaml = Self::get_value1(&defaults_value, field); + match yaml { + Ok(yaml) => Ok("# Not explicitly set, default value is:\n".to_owned() + &yaml), + Err(x) => Err(x) + } + } + } + } + // validate() is implemented in the (generated) lib.rs } diff --git a/pdns/recursordist/rec_channel_rec.cc b/pdns/recursordist/rec_channel_rec.cc index 6d5bc4bc19..9b0f3a0411 100644 --- a/pdns/recursordist/rec_channel_rec.cc +++ b/pdns/recursordist/rec_channel_rec.cc @@ -315,7 +315,7 @@ static Answer doGetParameter(ArgIterator begin, ArgIterator end) rust::Vec<::rust::String> field; stringtok(field, *begin, "."); try { - auto yaml = settings->get_value(field); + auto yaml = settings->get_value(field, pdns::settings::rec::defaultsToYaml(false)); return {0, std::string(yaml)}; } catch (const std::exception& stdex) {