1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::sql::part::Next;
use crate::sql::part::Part;
use crate::sql::value::Value;

impl Value {
	/// Synchronous method for deleting a field from a `Value`
	pub(crate) fn cut(&mut self, path: &[Part]) {
		if let Some(p) = path.first() {
			// Get the current value at path
			match self {
				// Current value at path is an object
				Value::Object(v) => match p {
					Part::Field(f) => match path.len() {
						1 => {
							v.remove(f.as_str());
						}
						_ => {
							if let Some(v) = v.get_mut(f.as_str()) {
								v.cut(path.next())
							}
						}
					},
					Part::Index(i) => match path.len() {
						1 => {
							v.remove(&i.to_string());
						}
						_ => {
							if let Some(v) = v.get_mut(&i.to_string()) {
								v.cut(path.next())
							}
						}
					},
					_ => {}
				},
				// Current value at path is an array
				Value::Array(v) => match p {
					Part::All => match path.len() {
						1 => {
							v.clear();
						}
						_ => {
							let path = path.next();
							v.iter_mut().for_each(|v| v.cut(path));
						}
					},
					Part::First => match path.len() {
						1 => {
							if !v.is_empty() {
								let i = 0;
								v.remove(i);
							}
						}
						_ => {
							if let Some(v) = v.first_mut() {
								v.cut(path.next())
							}
						}
					},
					Part::Last => match path.len() {
						1 => {
							if !v.is_empty() {
								let i = v.len() - 1;
								v.remove(i);
							}
						}
						_ => {
							if let Some(v) = v.last_mut() {
								v.cut(path.next())
							}
						}
					},
					Part::Index(i) => match path.len() {
						1 => {
							if v.len() > i.to_usize() {
								v.remove(i.to_usize());
							}
						}
						_ => {
							if let Some(v) = v.get_mut(i.to_usize()) {
								v.cut(path.next())
							}
						}
					},
					_ => {
						v.iter_mut().for_each(|v| v.cut(path));
					}
				},
				// Ignore everything else
				_ => (),
			}
		}
	}
}

#[cfg(test)]
mod tests {

	use super::*;
	use crate::sql::idiom::Idiom;
	use crate::syn::test::Parse;

	#[tokio::test]
	async fn cut_none() {
		let idi = Idiom::default();
		let mut val = Value::parse("{ test: { other: null, something: 123 } }");
		let res = Value::parse("{ test: { other: null, something: 123 } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_reset() {
		let idi = Idiom::parse("test");
		let mut val = Value::parse("{ test: { other: null, something: 123 } }");
		let res = Value::parse("{ }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_basic() {
		let idi = Idiom::parse("test.something");
		let mut val = Value::parse("{ test: { other: null, something: 123 } }");
		let res = Value::parse("{ test: { other: null } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_wrong() {
		let idi = Idiom::parse("test.something.wrong");
		let mut val = Value::parse("{ test: { other: null, something: 123 } }");
		let res = Value::parse("{ test: { other: null, something: 123 } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_other() {
		let idi = Idiom::parse("test.other.something");
		let mut val = Value::parse("{ test: { other: null, something: 123 } }");
		let res = Value::parse("{ test: { other: null, something: 123 } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_array() {
		let idi = Idiom::parse("test.something[1]");
		let mut val = Value::parse("{ test: { something: [123, 456, 789] } }");
		let res = Value::parse("{ test: { something: [123, 789] } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_array_field() {
		let idi = Idiom::parse("test.something[1].age");
		let mut val = Value::parse(
			"{ test: { something: [{ name: 'A', age: 34 }, { name: 'B', age: 36 }] } }",
		);
		let res = Value::parse("{ test: { something: [{ name: 'A', age: 34 }, { name: 'B' }] } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_array_fields() {
		let idi = Idiom::parse("test.something[*].age");
		let mut val = Value::parse(
			"{ test: { something: [{ name: 'A', age: 34 }, { name: 'B', age: 36 }] } }",
		);
		let res = Value::parse("{ test: { something: [{ name: 'A' }, { name: 'B' }] } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}

	#[tokio::test]
	async fn cut_array_fields_flat() {
		let idi = Idiom::parse("test.something.age");
		let mut val = Value::parse(
			"{ test: { something: [{ name: 'A', age: 34 }, { name: 'B', age: 36 }] } }",
		);
		let res = Value::parse("{ test: { something: [{ name: 'A' }, { name: 'B' }] } }");
		val.cut(&idi);
		assert_eq!(res, val);
	}
}