Skip to content

Commit 9c90ccb

Browse files
pass strings instead of serde value between plugins
1 parent aa43549 commit 9c90ccb

File tree

8 files changed

+32
-42
lines changed

8 files changed

+32
-42
lines changed

plugins/zenoh-backend-example/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl Default for ExampleStorage {
6060

6161
#[async_trait]
6262
impl Volume for ExampleBackend {
63-
fn get_admin_status(&self) -> serde_json::Value {
64-
serde_json::Value::Null
63+
fn get_admin_status(&self) -> String {
64+
String::default()
6565
}
6666
fn get_capability(&self) -> Capability {
6767
Capability {

plugins/zenoh-backend-traits/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ pub struct StoredData {
183183
/// Trait to be implemented by a Backend.
184184
#[async_trait]
185185
pub trait Volume: Send + Sync {
186-
/// Returns the status that will be sent as a reply to a query
186+
/// Returns the status in the form of json string that will be sent as a reply to a query
187187
/// on the administration space for this backend.
188-
fn get_admin_status(&self) -> serde_json::Value;
188+
fn get_admin_status(&self) -> String;
189189

190190
/// Returns the capability of this backend
191191
fn get_capability(&self) -> Capability;

plugins/zenoh-plugin-example/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ struct RunningPlugin(Arc<Mutex<RunningPluginInner>>);
130130
impl PluginControl for RunningPlugin {}
131131

132132
impl RunningPluginTrait for RunningPlugin {
133-
fn config_checker(
134-
&self,
135-
path: &str,
136-
old: &serde_json::Map<String, serde_json::Value>,
137-
new: &serde_json::Map<String, serde_json::Value>,
138-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
133+
fn config_checker(&self, path: &str, old: &str, new: &str) -> ZResult<Option<String>> {
134+
let old = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(old)?;
135+
let new = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(new)?;
139136
let mut guard = zlock!(&self.0);
140137
const STORAGE_SELECTOR: &str = "storage-selector";
141138
if path == STORAGE_SELECTOR || path.is_empty() {

plugins/zenoh-plugin-rest/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub struct Config {
4040
__plugin__: Option<String>,
4141
}
4242

43-
impl From<&Config> for serde_json::Value {
43+
impl From<&Config> for String {
4444
fn from(c: &Config) -> Self {
45-
serde_json::to_value(c).unwrap()
45+
serde_json::to_value(c).unwrap().to_string()
4646
}
4747
}
4848

plugins/zenoh-plugin-storage-manager/src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,12 @@ impl PluginControl for StorageRuntime {
316316
}
317317

318318
impl RunningPluginTrait for StorageRuntime {
319-
fn config_checker(
320-
&self,
321-
_: &str,
322-
old: &serde_json::Map<String, serde_json::Value>,
323-
new: &serde_json::Map<String, serde_json::Value>,
324-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
319+
fn config_checker(&self, _: &str, old: &str, new: &str) -> ZResult<Option<String>> {
320+
let old = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(old)?;
321+
let new = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(new)?;
325322
let name = { zlock!(self.0).name.clone() };
326-
let old = PluginConfig::try_from((&name, old))?;
327-
let new = PluginConfig::try_from((&name, new))?;
323+
let old = PluginConfig::try_from((&name, &old))?;
324+
let new = PluginConfig::try_from((&name, &new))?;
328325
tracing::debug!("config change requested for plugin '{}'", name);
329326
tracing::debug!("old config: {:?}", &old);
330327
tracing::debug!("new config: {:?}", &new);
@@ -381,7 +378,7 @@ impl RunningPluginTrait for StorageRuntime {
381378
rx.recv().await
382379
})
383380
}) {
384-
responses.push(Response::new(key.clone(), value))
381+
responses.push(Response::new(key.clone(), value.to_string()))
385382
}
386383
}
387384
})

plugins/zenoh-plugin-storage-manager/src/memory_backend/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl Plugin for MemoryBackend {
5050

5151
#[async_trait]
5252
impl Volume for MemoryBackend {
53-
fn get_admin_status(&self) -> serde_json::Value {
54-
self.config.to_json_value()
53+
fn get_admin_status(&self) -> String {
54+
self.config.to_json_value().to_string()
5555
}
5656

5757
fn get_capability(&self) -> Capability {

zenoh/src/api/plugins.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ impl PluginControl for RunningPlugin {
4343
impl PluginInstance for RunningPlugin {}
4444

4545
#[non_exhaustive]
46-
#[derive(serde::Serialize, Debug, Clone)]
46+
#[derive(Debug, Clone)]
4747
/// A Response for the administration space.
4848
pub struct Response {
4949
pub key: String,
50-
pub value: serde_json::Value,
50+
pub json_value: String,
5151
}
5252

5353
impl Response {
54-
pub fn new(key: String, value: serde_json::Value) -> Self {
55-
Self { key, value }
54+
pub fn new(key: String, json_value: String) -> Self {
55+
Self { key, json_value }
5656
}
5757
}
5858

@@ -69,12 +69,7 @@ pub trait RunningPluginTrait: Send + Sync + PluginControl {
6969
/// Useful when the changes affect settings that aren't hot-configurable for your plugin.
7070
/// * `Ok(None)` indicates that the plugin has accepted the configuration change.
7171
/// * `Ok(Some(value))` indicates that the plugin would rather the new configuration be `value`.
72-
fn config_checker(
73-
&self,
74-
_path: &str,
75-
_current: &serde_json::Map<String, serde_json::Value>,
76-
_new: &serde_json::Map<String, serde_json::Value>,
77-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
72+
fn config_checker(&self, _path: &str, _current: &str, _new: &str) -> ZResult<Option<String>> {
7873
bail!("Runtime configuration change not supported");
7974
}
8075
/// Used to request plugin's status for the administration space.

zenoh/src/net/runtime/adminspace.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ impl ConfigValidator for AdminSpace {
9191
// on config comparison (see `PluginDiff`)
9292
return Ok(None);
9393
};
94-
plugin.instance().config_checker(path, current, new)
94+
let current = serde_json::to_string(current)?;
95+
let new = serde_json::to_string(new)?;
96+
match plugin.instance().config_checker(path, &current, &new)? {
97+
Some(s) => Ok(Some(serde_json::from_str::<
98+
serde_json::Map<String, serde_json::Value>,
99+
>(&s)?)),
100+
None => Ok(None),
101+
}
95102
}
96103
#[cfg(not(feature = "plugins"))]
97104
{
@@ -958,14 +965,8 @@ fn plugins_status(context: &AdminContext, query: Query) {
958965
Ok(Ok(responses)) => {
959966
for response in responses {
960967
if let Ok(key_expr) = KeyExpr::try_from(response.key) {
961-
match serde_json::to_vec(&response.value) {
962-
Ok(bytes) => {
963-
if let Err(e) = query.reply(key_expr, bytes).encoding(Encoding::APPLICATION_JSON).wait() {
964-
tracing::error!("Error sending AdminSpace reply: {:?}", e);
965-
}
966-
}
967-
Err(e) => tracing::debug!("Admin query error: {}", e),
968-
968+
if let Err(e) = query.reply(key_expr, response.json_value).encoding(Encoding::APPLICATION_JSON).wait() {
969+
tracing::error!("Error sending AdminSpace reply: {:?}", e);
969970
}
970971
} else {
971972
tracing::error!("Error: plugin {} replied with an invalid key", plugin_key);

0 commit comments

Comments
 (0)