|
15 | 15 | //! |
16 | 16 | pub use crate::kinode::process::standard::*; |
17 | 17 | use serde::{Deserialize, Serialize}; |
| 18 | +use serde_json::Value; |
18 | 19 |
|
19 | 20 | wit_bindgen::generate!({ |
20 | 21 | path: "kinode-wit", |
@@ -206,8 +207,36 @@ pub fn can_message(address: &Address) -> bool { |
206 | 207 | /// Get a capability in our store |
207 | 208 | /// NOTE unfortunatly this is O(n), not sure if wit let's us do any better |
208 | 209 | pub fn get_capability(our: &Address, params: &str) -> Option<Capability> { |
| 210 | + let params = serde_json::from_str::<Value>(params).unwrap_or_default(); |
209 | 211 | crate::our_capabilities() |
210 | 212 | .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(¶ms, &cap_params) |
| 216 | + }) |
212 | 217 | .cloned() |
213 | 218 | } |
| 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