surrealdb_expr/val/value/
set.rs1use crate::val::field_path::{FieldPath, FieldPathPart};
2use crate::val::{Object, Value};
3
4impl Value {
5 pub fn set_at_field_path_owned(&mut self, path: &FieldPath, val: Value) {
14 self.set_at_field_path_owned_depth(path, val, 0);
15 }
16
17 fn set_at_field_path_owned_depth(&mut self, path: &FieldPath, val: Value, depth: usize) {
18 if depth >= path.len() {
19 *self = val;
20 return;
21 }
22
23 let part = &path.0[depth];
24 let is_last = depth == path.len() - 1;
25
26 match (self, part) {
27 (Value::Object(obj), FieldPathPart::Field(key) | FieldPathPart::Lookup(key)) => {
29 if is_last {
30 obj.insert(key.clone(), val);
31 } else {
32 let nested =
33 obj.entry(key.clone()).or_insert_with(|| Value::Object(Object::default()));
34 nested.set_at_field_path_owned_depth(path, val, depth + 1);
35 }
36 }
37 (Value::Array(arr), FieldPathPart::Index(i)) if *i < arr.len() => {
39 if is_last {
40 arr[*i] = val;
41 } else {
42 arr[*i].set_at_field_path_owned_depth(path, val, depth + 1);
43 }
44 }
45 (Value::Array(arr), FieldPathPart::First) if !arr.is_empty() => {
47 if is_last {
48 arr[0] = val;
49 } else {
50 arr[0].set_at_field_path_owned_depth(path, val, depth + 1);
51 }
52 }
53 (Value::Array(arr), FieldPathPart::Last) if !arr.is_empty() => {
55 let last_idx = arr.len() - 1;
56 if is_last {
57 arr[last_idx] = val;
58 } else {
59 arr[last_idx].set_at_field_path_owned_depth(path, val, depth + 1);
60 }
61 }
62 (Value::Set(set), FieldPathPart::Index(i)) if set.nth(*i).is_some() => {
64 if let Some(elem) = set.nth_mut(*i) {
65 if is_last {
66 *elem = val;
67 } else {
68 elem.set_at_field_path_owned_depth(path, val, depth + 1);
69 }
70 }
71 }
72 (Value::Set(set), FieldPathPart::First) if !set.is_empty() => {
74 if let Some(elem) = set.first_mut() {
75 if is_last {
76 *elem = val;
77 } else {
78 elem.set_at_field_path_owned_depth(path, val, depth + 1);
79 }
80 }
81 }
82 (Value::Set(set), FieldPathPart::Last) if !set.is_empty() => {
84 if let Some(elem) = set.last_mut() {
85 if is_last {
86 *elem = val;
87 } else {
88 elem.set_at_field_path_owned_depth(path, val, depth + 1);
89 }
90 }
91 }
92 (Value::Array(arr), FieldPathPart::Field(_) | FieldPathPart::Lookup(_)) => {
95 if let Some((last, rest)) = arr.split_last_mut() {
96 for elem in rest {
97 elem.set_at_field_path_owned_depth(path, val.clone(), depth);
98 }
99 last.set_at_field_path_owned_depth(path, val, depth);
100 }
101 }
102 (target, FieldPathPart::Field(key) | FieldPathPart::Lookup(key)) => {
104 let mut obj = Object::default();
105 if is_last {
106 obj.insert(key.clone(), val);
107 } else {
108 let mut nested = Value::Object(Object::default());
109 nested.set_at_field_path_owned_depth(path, val, depth + 1);
110 obj.insert(key.clone(), nested);
111 }
112 *target = Value::Object(obj);
113 }
114 _ => {} }
116 }
117}