Bugfix - Error instead of panic when applying malformed patches. ()

This commit is contained in:
Finn Bear 2023-05-19 14:02:43 -07:00 committed by GitHub
parent a699592e6d
commit 0d658842df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 10 deletions

5
Cargo.lock generated
View file

@ -1286,11 +1286,10 @@ dependencies = [
[[package]]
name = "dmp"
version = "0.1.3"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1796e147190351ab441586c68b74494b18a70b0e39fb9bf8e84e38635bf4c92a"
checksum = "bfaa1135a34d26e5cc5b4927a8935af887d4f30a5653a797c33b9a4222beb6d9"
dependencies = [
"regex",
"urlencoding",
]

View file

@ -61,7 +61,7 @@ bung = "0.1.0"
channel = { version = "1.8.0", package = "async-channel" }
chrono = { version = "0.4.24", features = ["serde"] }
derive = { version = "0.8.0", package = "surrealdb-derive" }
dmp = "0.1.3"
dmp = "0.2.0"
echodb = { version = "0.4.0", optional = true }
executor = { version = "1.5.1", package = "async-executor" }
flume = "0.10.14"

View file

@ -65,9 +65,9 @@ impl Value {
op: Op::Change,
path,
value: {
let mut dmp = dmp::new();
let mut pch = dmp.patch_make1(a, b);
let txt = dmp.patch_to_text(&mut pch);
let dmp = dmp::new();
let pch = dmp.patch_make1(a, b);
let txt = dmp.patch_to_text(&pch);
txt.into()
},
}),

View file

@ -15,9 +15,17 @@ impl Value {
Op::Change => {
if let Value::Strand(p) = o.value {
if let Value::Strand(v) = self.pick(&o.path) {
let mut dmp = dmp::new();
let mut pch = dmp.patch_from_text(p.as_string());
let (txt, _) = dmp.patch_apply(&mut pch, v.as_str());
let dmp = dmp::new();
let mut pch = dmp.patch_from_text(p.as_string()).map_err(|e| {
Error::InvalidPatch {
message: format!("{e:?}"),
}
})?;
let (txt, _) = dmp.patch_apply(&mut pch, v.as_str()).map_err(|e| {
Error::InvalidPatch {
message: format!("{e:?}"),
}
})?;
let txt = txt.into_iter().collect::<String>();
self.put(&o.path, Value::from(txt));
}
@ -111,4 +119,12 @@ mod tests {
val.patch(ops).unwrap();
assert_eq!(res, val);
}
#[tokio::test]
async fn patch_change_invalid() {
// See https://github.com/surrealdb/surrealdb/issues/2001
let mut val = Value::parse("{ test: { other: 'test', something: 123 }, temp: true }");
let ops = Value::parse("[{ op: 'change', path: '/test/other', value: 'text' }]");
assert!(val.patch(ops).is_err());
}
}