Skip to content

Commit

Permalink
replaced unwraps for plugin configurations insert to correct error ch…
Browse files Browse the repository at this point in the history
…eck (eclipse-zenoh#936)

* replaced unwraps for plugin configurations insert to correct error check

* cargo fmt

* comments corrected
  • Loading branch information
milyin authored Apr 16, 2024
1 parent 580f0b6 commit 664efbd
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod include;
use include::recursive_include;
use secrecy::{CloneableSecret, DebugSecret, Secret, SerializableSecret, Zeroize};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{Map, Value};
#[allow(unused_imports)]
use std::convert::TryFrom; // This is a false positive from the rust analyser
use std::{
Expand Down Expand Up @@ -1185,20 +1185,34 @@ impl validated_struct::ValidatedMap for PluginsConfig {
.unwrap()
.entry(plugin)
.or_insert(Value::Null);
let mut new_value = value.clone().merge(key, new_value)?;
if let Some(validator) = self.validator.upgrade() {
match validator.check_config(
plugin,
key,
value.as_object().unwrap(),
new_value.as_object().unwrap(),
) {
Ok(Some(val)) => new_value = Value::Object(val),
Ok(None) => {}
let new_value = value.clone().merge(key, new_value)?;
*value = if let Some(validator) = self.validator.upgrade() {
// New plugin configuration for compare with original configuration.
// Return error if it's not an object.
// Note: it's ok if original "new_value" is not an object: this can be some subkey of the plugin configuration. But the result of the merge should be an object.
// Error occurs if the original plugin configuration is not an object itself (e.g. null).
let Some(new_plugin_config) = new_value.as_object() else {
return Err(format!(
"Attempt to provide non-object value as configuration for plugin `{plugin}`"
)
.into());
};
// Original plugin configuration for compare with new configuration.
// If for some reason it's not defined or not an object, we default to an empty object.
// Usually this happens when no plugin with this name defined. Reject then should be performed by the validator with `plugin not found` error.
let empty_config = Map::new();
let current_plugin_config = value.as_object().unwrap_or(&empty_config);
match validator.check_config(plugin, key, current_plugin_config, new_plugin_config) {
// Validator made changes to the proposed configuration, take these changes
Ok(Some(val)) => Value::Object(val),
// Validator accepted the proposed configuration as is
Ok(None) => new_value,
// Validator rejected the proposed configuration
Err(e) => return Err(format!("{e}").into()),
}
}
*value = new_value;
} else {
new_value
};
Ok(())
}
fn get<'a>(&'a self, mut key: &str) -> Result<&'a dyn Any, GetError> {
Expand Down

0 comments on commit 664efbd

Please sign in to comment.