json_patch_ext/
lib.rs

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
//! This module provides some unofficial "extensions" to the [jsonpatch](https://jsonpatch.com)
//! format for describing changes to a JSON document.  In particular, it adds the `*` operator as a
//! valid token for arrays in a JSON document.  It means: apply this change to all elements of this
//! array.  For example, consider the following document:
//!
//! ```json
//! {
//!   "foo": {
//!     "bar": [
//!       {"baz": 1},
//!       {"baz": 2},
//!       {"baz": 3},
//!     ]
//!   }
//! }
//! ```
//!
//! The pathspec `/foo/bar/*/baz` would reference the `baz` field of all three array entries in the
//! `bar` array.  It is an error to use `*` to reference a field that is not an array.  It is an
//! error to use `*` at the end of a path, e.g., `/foo/*`.
//!
//! Additionally, this crate will auto-create parent paths for the AddOperation only, e.g., the
//! result of applying `AddOperation{ path: "/foo/bar", value: 1 }` to the empty document will be
//!
//! ```json
//! { "foo": {"bar": 1}}
//! ```

mod errors;
mod macros;

use json_patch::patch;
// mark these as re-exports in the generated docs (maybe related to
// https://github.com/rust-lang/rust/issues/131180?)
#[doc(no_inline)]
pub use json_patch::{
    AddOperation,
    CopyOperation,
    MoveOperation,
    Patch,
    PatchOperation,
    RemoveOperation,
    ReplaceOperation,
    TestOperation,
};
#[doc(no_inline)]
use jsonptr::index::Index;
use jsonptr::Token;
pub use jsonptr::{
    Pointer,
    PointerBuf,
};
use serde_json::{
    json,
    Value,
};

pub use crate::errors::PatchError;

// PatchMode controls what to do if the referenced element does not exist in the object.
#[derive(Debug, Clone, Copy)]
enum PatchMode {
    Error,
    Create,
    Skip,
}

pub fn add_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Add(AddOperation { path, value })
}

pub fn replace_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Replace(ReplaceOperation { path, value })
}

pub fn remove_operation(path: PointerBuf) -> PatchOperation {
    PatchOperation::Remove(RemoveOperation { path })
}

pub fn move_operation(from: PointerBuf, path: PointerBuf) -> PatchOperation {
    PatchOperation::Move(MoveOperation { from, path })
}

pub fn copy_operation(from: PointerBuf, path: PointerBuf) -> PatchOperation {
    PatchOperation::Copy(CopyOperation { from, path })
}

pub fn test_operation(path: PointerBuf, value: Value) -> PatchOperation {
    PatchOperation::Test(TestOperation { path, value })
}

pub fn escape(input: &str) -> String {
    Token::new(input).encoded().into()
}

pub fn patch_ext(obj: &mut Value, p: PatchOperation) -> Result<(), PatchError> {
    match p {
        PatchOperation::Add(op) => add_or_replace(obj, &op.path, &op.value, false)?,
        PatchOperation::Remove(op) => remove(obj, &op.path)?,
        PatchOperation::Replace(op) => add_or_replace(obj, &op.path, &op.value, true)?,
        x => patch(obj, &[x])?,
    }
    Ok(())
}

fn add_or_replace(obj: &mut Value, path: &PointerBuf, value: &Value, replace: bool) -> Result<(), PatchError> {
    let Some((subpath, tail)) = path.split_back() else {
        return Ok(());
    };

    // "replace" requires that the path you're replacing already exist, therefore we set
    // create_if_not_exists = !replace.  We don't want to skip missing elements.
    let mode = if replace { PatchMode::Error } else { PatchMode::Create };
    for v in patch_ext_helper(subpath, obj, mode)? {
        match v {
            Value::Object(map) => {
                let key = tail.decoded().into();
                if replace && !map.contains_key(&key) {
                    return Err(PatchError::TargetDoesNotExist(path.as_str().into()));
                }
                map.insert(key, value.clone());
            },
            Value::Array(vec) => match tail.to_index()? {
                Index::Num(idx) => {
                    vec.get(idx).ok_or(PatchError::OutOfBounds(idx))?;
                    if replace {
                        vec[idx] = value.clone();
                    } else {
                        vec.insert(idx, value.clone());
                    }
                },
                Index::Next => {
                    vec.push(value.clone());
                },
            },
            _ => {
                return Err(PatchError::UnexpectedType(path.as_str().into()));
            },
        }
    }

    Ok(())
}

fn remove(obj: &mut Value, path: &PointerBuf) -> Result<(), PatchError> {
    let Some((subpath, key)) = path.split_back() else {
        return Ok(());
    };

    for v in patch_ext_helper(subpath, obj, PatchMode::Skip)? {
        v.as_object_mut()
            .ok_or(PatchError::UnexpectedType(subpath.as_str().into()))?
            .remove(key.decoded().as_ref());
    }

    Ok(())
}

// Given JSON pointer, recursively walk through all the possible "end" values that the path
// references; return a mutable reference so we can make modifications at those points.
fn patch_ext_helper<'a>(
    path: &Pointer,
    value: &'a mut Value,
    mode: PatchMode,
) -> Result<Vec<&'a mut Value>, PatchError> {
    let Some(idx) = path.as_str().find("/*") else {
        if path.resolve(value).is_err() {
            match mode {
                PatchMode::Error => return Err(PatchError::TargetDoesNotExist(path.as_str().into())),
                PatchMode::Create => {
                    path.assign(value, json!({}))?;
                },
                PatchMode::Skip => return Ok(vec![]),
            }
        }
        return Ok(vec![path.resolve_mut(value)?]);
    };

    // we checked the index above so unwrap is safe here
    let (head, cons) = path.split_at(idx).unwrap();
    let mut res = vec![];

    // This is a little weird; if mode == Create, and the subpath up to this point doesn't exist,
    // we'll create an empty array which we won't iterate over at all.  I think that's
    // "approximately" fine and less surprising that not creating anything.
    if head.resolve(value).is_err() {
        match mode {
            PatchMode::Error => return Err(PatchError::TargetDoesNotExist(path.as_str().into())),
            PatchMode::Create => {
                path.assign(value, json!([]))?;
            },
            PatchMode::Skip => return Ok(vec![]),
        }
    }
    let next_array_val =
        head.resolve_mut(value)?.as_array_mut().ok_or(PatchError::UnexpectedType(head.as_str().into()))?;
    for v in next_array_val {
        if let Some((_, c)) = cons.split_front() {
            res.extend(patch_ext_helper(c, v, mode)?);
        } else {
            res.push(v);
        }
    }
    Ok(res)
}

#[cfg(test)]
mod tests {
    use assertables::*;
    use rstest::*;

    use super::*;
    use crate as json_patch_ext; // make the macros work in the tests

    #[fixture]
    fn data() -> Value {
        json!({
            "foo": [
                {"baz": {"buzz": 0}},
                {"baz": {"quzz": 1}},
                {"baz": {"fixx": 2}},
            ],
        })
    }

    #[rstest]
    fn test_patch_ext_add(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/buzz");
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 42 }},
                    {"baz": {"quzz": 1, "buzz": 42}},
                    {"baz": {"fixx": 2, "buzz": 42}},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_add_escaped() {
        let path = format_ptr!("/foo/bar/{}", escape("testing.sh/baz"));
        let mut data = json!({});
        let res = patch_ext(&mut data, add_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(data, json!({"foo": {"bar": {"testing.sh/baz": 42}}}));
    }

    #[rstest]
    fn test_patch_ext_replace(mut data: Value) {
        let path = format_ptr!("/foo/*/baz");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": 42},
                    {"baz": 42},
                    {"baz": 42},
                ],
            })
        );
    }

    #[rstest]
    fn test_patch_ext_replace_err(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/buzz");
        let res = patch_ext(&mut data, replace_operation(path, json!(42)));
        println!("{data:?}");
        assert_err!(res);
    }


    #[rstest]
    fn test_patch_ext_remove(mut data: Value) {
        let path = format_ptr!("/foo/*/baz/quzz");
        let res = patch_ext(&mut data, remove_operation(path));
        assert_ok!(res);
        assert_eq!(
            data,
            json!({
                "foo": [
                    {"baz": {"buzz": 0}},
                    {"baz": {}},
                    {"baz": {"fixx": 2}},
                ],
            })
        );
    }
}