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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
mod errors;
pub use errors::Error;
use crate::actions::setter::namespace::Error as SetterErr;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
/// Represents a single group/level of JSON structures used for traversing JSON structures.
///
/// # Example
/// ```json
/// {
/// "test" : { "value" : "my value" }
/// }
/// ```
/// `test.value` would be represented by two Namespace Object's `test` and `value` as a way to
/// traverse the JSON data to point at `my value`.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum Namespace {
/// Represents an id/location for an Object within the destination data.
Object { id: String },
/// Represents that the [Setter](../struct.Setter.html) should merge the source and destination
/// JSON Objects.
MergeObject,
/// Represents an index/location for an Array within the destination data.
Array { index: usize },
/// Represents that the [Setter](../struct.Setter.html) should append the source data to the
/// destination JSON Array.
AppendArray,
/// Represents that the [Setter](../struct.Setter.html) should merge the source and destination
/// JSON Arrays.
MergeArray,
/// Represents that the [Setter](../struct.Setter.html) should combine the source JSON Array to
/// the destination JSON Array by appending all array elements from the source Array to the
/// destinations.
CombineArray,
}
impl Display for Namespace {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Namespace::Object { id } => write!(f, "{}", id),
Namespace::MergeObject => write!(f, "{{}}"),
Namespace::AppendArray => write!(f, "[]"),
Namespace::MergeArray => write!(f, "[-]"),
Namespace::CombineArray => write!(f, "[+]"),
Namespace::Array { index } => write!(f, "[{}]", index),
}
}
}
impl Namespace {
/// parses a transformation syntax string into an Vec of [Namespace](enum.Namespace.html)'s for
/// use in the [Setter](../struct.Setter.html).
///
/// The transformation syntax is very similar to access JSON data in Javascript with a few additions:
/// * `{}` eg. test.value{} which denotes that the source Object and destination Object `value` should merge their data instead of the source replace the destination value
/// * `[]` eg. test.value[] which denotes that the source data should be appended to the Array `value` rather than replacing the destination value.
/// * `[+]` eg. test.value[+] which denotes that the source Array should append all of it's values onto the destination Array.
/// * `[-]` eg. test.value[-] which denotes that the source Array values should replace the destination Array's values at the overlapping indexes.
/// NOTE: `{}`, `[+]` and `[-]` can only be used on the last element of the Namespace syntax.
///
/// To handle special characters such as ``(blank), `[`, `]`, `"` and `.` you can use the explicit
/// key syntax `["example[].blah"]` which would represent the key in the following JSON:
/// ```json
/// {
/// "example[].blah" : "my value"
/// }
/// ```
pub fn parse(input: &str) -> Result<Vec<Namespace>, SetterErr> {
if input.is_empty() {
return Ok(Vec::new());
}
let bytes = input.as_bytes();
let mut namespaces = Vec::new();
let mut idx = 0;
let mut s = Vec::with_capacity(10);
'outer: while idx < bytes.len() {
let b = bytes[idx];
match b {
b'.' => {
if s.is_empty() {
// empty values must be via explicit key
// might also be ending to other types eg. array.
if idx == 0 || idx + 1 == bytes.len() {
// cannot start with '.', if want a blank key must use explicit key syntax
return Err(Error::InvalidDotNotation {
ns: input.to_owned(),
err: r#"Namespace cannot start or end with '.', explicit key syntax of '[""]' must be used to denote a blank key."#.to_owned(),
});
}
idx += 1;
continue;
}
namespaces.push(Namespace::Object {
id: unsafe { String::from_utf8_unchecked(s.clone()) },
});
s.clear();
idx += 1;
continue;
}
b'{' => {
if !s.is_empty() {
namespaces.push(Namespace::Object {
id: unsafe { String::from_utf8_unchecked(s.clone()) },
});
s.clear();
}
// merge object syntax
idx += 1;
if idx < bytes.len() && bytes[idx] != b'}' {
// error invalid merge object syntax
return Err(Error::InvalidMergeObjectSyntax(input.to_owned()));
}
idx += 1;
if idx != bytes.len() {
// error merge object must be the last part in the namespace.
return Err(Error::InvalidMergeObjectSyntax(input.to_owned()));
}
namespaces.push(Namespace::MergeObject);
}
b'[' => {
if !s.is_empty() {
// this syntax named[..] lets create the object
namespaces.push(Namespace::Object {
id: unsafe { String::from_utf8_unchecked(s.clone()) },
});
s.clear();
}
idx += 1;
if idx >= bytes.len() {
// error incomplete namespace
return Err(Error::MissingArrayIndexBracket(input.to_owned()));
}
match bytes[idx] {
b'"' => {
// parse explicit key
idx += 1;
while idx < bytes.len() {
let b = bytes[idx];
match b {
b'"' if bytes[idx - 1] != b'\\' => {
idx += 1;
if bytes[idx] != b']' {
// error invalid explicit key syntax
return Err(Error::InvalidExplicitKeySyntax(
input.to_owned(),
));
}
namespaces.push(Namespace::Object {
id: unsafe { String::from_utf8_unchecked(s.clone()) }
.replace("\\", ""), // unescape required escaped double quotes
});
s.clear();
idx += 1;
continue 'outer;
}
_ => {
idx += 1;
s.push(b)
}
};
}
// error never reached the end bracket of explicit key
return Err(Error::InvalidExplicitKeySyntax(input.to_owned()));
}
b']' => {
// append array index
namespaces.push(Namespace::AppendArray);
idx += 1;
continue 'outer;
}
b'-' => {
// merge array
idx += 1;
if idx < bytes.len() && bytes[idx] != b']' {
// error invalid merge object syntax
return Err(Error::InvalidMergeArraySyntax(input.to_owned()));
}
idx += 1;
if idx != bytes.len() {
// error merge object must be the last part in the namespace.
return Err(Error::InvalidMergeArraySyntax(input.to_owned()));
}
namespaces.push(Namespace::MergeArray);
}
b'+' => {
// merge array
idx += 1;
if idx < bytes.len() && bytes[idx] != b']' {
// error invalid merge object syntax
return Err(Error::InvalidCombineArraySyntax(input.to_owned()));
}
idx += 1;
if idx != bytes.len() {
// error merge object must be the last part in the namespace.
return Err(Error::InvalidCombineArraySyntax(input.to_owned()));
}
namespaces.push(Namespace::CombineArray);
}
_ => {
// parse array index
while idx < bytes.len() {
let b = bytes[idx];
match b {
b']' => {
namespaces.push(Namespace::Array {
index: unsafe {
String::from_utf8_unchecked(s.clone())
}
.parse()?,
});
s.clear();
idx += 1;
continue 'outer;
}
_ => {
idx += 1;
s.push(b)
}
};
}
// error no end bracket
return Err(Error::MissingArrayIndexBracket(input.to_owned()));
}
}
}
_ => {
s.push(b);
idx += 1;
}
};
}
if !s.is_empty() {
namespaces.push(Namespace::Object {
id: unsafe { String::from_utf8_unchecked(s) },
});
}
Ok(namespaces)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_direct_set() {
let ns = "";
let results = Namespace::parse(ns).unwrap();
let expected: Vec<Namespace> = Vec::new();
assert_eq!(expected, results);
}
#[test]
fn test_object_merge() {
let ns = "person{}";
let results = Namespace::parse(ns).unwrap();
let expected = vec![
Namespace::Object {
id: "person".into(),
},
Namespace::MergeObject,
];
assert_eq!(expected, results);
}
#[test]
fn test_array_merge() {
let ns = "person[-]";
let results = Namespace::parse(ns).unwrap();
let expected = vec![
Namespace::Object {
id: "person".into(),
},
Namespace::MergeArray,
];
assert_eq!(expected, results);
}
#[test]
fn test_array_combine() {
let ns = "person[+]";
let results = Namespace::parse(ns).unwrap();
let expected = vec![
Namespace::Object {
id: "person".into(),
},
Namespace::CombineArray,
];
assert_eq!(expected, results);
}
#[test]
fn test_append_array() {
let ns = "person[]";
let results = Namespace::parse(ns).unwrap();
let expected = vec![
Namespace::Object {
id: "person".into(),
},
Namespace::AppendArray,
];
assert_eq!(expected, results);
}
}