Skip to content

Commit 5e5de35

Browse files
committed
fix get_capability() params check
because json is unordered, the stored json params could sometimes be a different order than the one checked by the caller; now convert to json and check equality of all fields
1 parent ca63613 commit 5e5de35

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//!
1616
pub use crate::kinode::process::standard::*;
1717
use serde::{Deserialize, Serialize};
18+
use serde_json::Value;
1819

1920
wit_bindgen::generate!({
2021
path: "kinode-wit",
@@ -206,8 +207,36 @@ pub fn can_message(address: &Address) -> bool {
206207
/// Get a capability in our store
207208
/// NOTE unfortunatly this is O(n), not sure if wit let's us do any better
208209
pub fn get_capability(our: &Address, params: &str) -> Option<Capability> {
210+
let params = serde_json::from_str::<Value>(params).unwrap_or_default();
209211
crate::our_capabilities()
210212
.iter()
211-
.find(|cap| cap.issuer == *our && cap.params == params)
213+
.find(|cap| {
214+
let cap_params = serde_json::from_str::<Value>(&cap.params).unwrap_or_default();
215+
cap.issuer == *our && are_equal_json_values(&params, &cap_params)
216+
})
212217
.cloned()
213218
}
219+
220+
fn are_equal_json_objects(obj1: &Value, obj2: &Value) -> bool {
221+
match (obj1.as_object(), obj2.as_object()) {
222+
(Some(map1), Some(map2)) => {
223+
if map1.len() != map2.len() {
224+
return false;
225+
}
226+
227+
map1.iter().all(|(key, val1)| {
228+
map2.get(key)
229+
.map_or(false, |val2| are_equal_json_values(val1, val2))
230+
})
231+
}
232+
_ => false,
233+
}
234+
}
235+
236+
fn are_equal_json_values(val1: &Value, val2: &Value) -> bool {
237+
match (val1, val2) {
238+
(Value::Object(_), Value::Object(_)) => are_equal_json_objects(val1, val2),
239+
_ => val1 == val2,
240+
}
241+
}
242+

0 commit comments

Comments
 (0)