Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 119 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
Value::Object(map) => {
let key = tail.decoded().into();
if replace && !map.contains_key(&key) {
return Err(PatchError::TargetDoesNotExist(path.as_str().into()));
return Err(PatchError::TargetDoesNotExist(path.to_string()));
}
map.insert(key, value.clone());
},
Expand All @@ -202,7 +202,7 @@
},
},
_ => {
return Err(PatchError::UnexpectedType(path.as_str().into()));
return Err(PatchError::UnexpectedType(path.to_string()));

Check warning on line 205 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L205

Added line #L205 was not covered by tests
},
}
}
Expand All @@ -216,9 +216,22 @@
};

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());
match v {
Value::Object(map) => {
map.remove(key.decoded().as_ref());
},
Value::Array(vec) => {
if let Index::Num(idx) = key.to_index()? {
vec.get(idx).ok_or(PatchError::OutOfBounds(idx))?;
vec.remove(idx);
} else {
return Err(PatchError::UnexpectedType(key.to_string()));
}
},
_ => {
return Err(PatchError::UnexpectedType(path.to_string()));

Check warning on line 232 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L232

Added line #L232 was not covered by tests
},
}
}

Ok(())
Expand Down Expand Up @@ -363,6 +376,49 @@
);
}

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

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

#[rstest]
fn test_patch_ext_add_vec_err(mut data: Value) {
let path = format_ptr!("/foo/a");
let res = patch_ext(&mut data, add_operation(path, json!(42)));
assert_err!(res);
}

#[rstest]
fn test_patch_ext_add_escaped() {
let path = format_ptr!("/foo/bar/{}", escape("testing.sh/baz"));
Expand All @@ -389,11 +445,45 @@
);
}

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

#[rstest]
fn test_patch_ext_replace_vec2(mut data: Value) {
let path = format_ptr!("/foo/-");
let res = patch_ext(&mut data, replace_operation(path, json!(42)));
assert_ok!(res);
assert_eq!(
data,
json!({
"foo": [
{"baz": {"buzz": 0}},
{"baz": {"quzz": 1}},
{"baz": {"fixx": 2}},
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);
}

Expand All @@ -413,4 +503,27 @@
})
);
}

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

#[rstest]
fn test_patch_ext_remove_vec_err(mut data: Value) {
let path = format_ptr!("/foo/-");
let res = patch_ext(&mut data, remove_operation(path));
assert_err!(res);
}
}
Loading