MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_eval

Function try_eval

nodedb-query/src/functions/json/legacy.rs:10–120  ·  view source on GitHub ↗
(name: &str, args: &[Value])

Source from the content-addressed store, hash-verified

8use nodedb_types::Value;
9
10pub(super) fn try_eval(name: &str, args: &[Value]) -> Option<Value> {
11 let v = match name {
12 "json_extract" | "json_get" => {
13 let obj = args.first().unwrap_or(&Value::Null);
14 let path = args.get(1).and_then(|v| v.as_str()).unwrap_or("");
15 let mut current = obj;
16 for key in path.split('.') {
17 current = match current {
18 Value::Object(map) => map.get(key).unwrap_or(&Value::Null),
19 Value::Array(arr) => key
20 .parse::<usize>()
21 .ok()
22 .and_then(|i| arr.get(i))
23 .unwrap_or(&Value::Null),
24 _ => &Value::Null,
25 };
26 }
27 current.clone()
28 }
29 "json_set" => {
30 let mut obj = args
31 .first()
32 .cloned()
33 .unwrap_or(Value::Object(HashMap::new()));
34 let key = args.get(1).and_then(|v| v.as_str()).unwrap_or("");
35 let val = args.get(2).cloned().unwrap_or(Value::Null);
36 if let Value::Object(ref mut map) = obj {
37 map.insert(key.to_string(), val);
38 }
39 obj
40 }
41 "json_remove" => {
42 let mut obj = args.first().cloned().unwrap_or(Value::Null);
43 let key = args.get(1).and_then(|v| v.as_str()).unwrap_or("");
44 if let Value::Object(ref mut map) = obj {
45 map.remove(key);
46 }
47 obj
48 }
49 "json_keys" => match args.first() {
50 Some(Value::Object(map)) => {
51 Value::Array(map.keys().map(|k| Value::String(k.clone())).collect())
52 }
53 _ => Value::Null,
54 },
55 "json_values" => match args.first() {
56 Some(Value::Object(map)) => Value::Array(map.values().cloned().collect()),
57 _ => Value::Null,
58 },
59 "json_length" | "json_len" => match args.first() {
60 Some(Value::Object(map)) => Value::Integer(map.len() as i64),
61 Some(Value::Array(arr)) => Value::Integer(arr.len() as i64),
62 Some(Value::String(s)) => Value::Integer(s.len() as i64),
63 _ => Value::Null,
64 },
65 "json_type" => {
66 let type_name = match args.first() {
67 Some(Value::Null) | None => "null",

Callers

nothing calls this directly

Calls 13

value_to_display_stringFunction · 0.85
firstMethod · 0.80
to_stringMethod · 0.80
collectMethod · 0.80
getMethod · 0.45
as_strMethod · 0.45
okMethod · 0.45
cloneMethod · 0.45
insertMethod · 0.45
removeMethod · 0.45
lenMethod · 0.45
to_vecMethod · 0.45

Tested by

no test coverage detected