Ensure object diffing works correctly when values are the same
This commit is contained in:
parent
dc22d4e40e
commit
c3cf0e5e24
1 changed files with 11 additions and 3 deletions
|
@ -7,7 +7,7 @@ impl Value {
|
|||
pub fn diff(&self, val: &Value, path: Idiom) -> Vec<Operation> {
|
||||
let mut ops: Vec<Operation> = vec![];
|
||||
match (self, val) {
|
||||
(Value::Object(a), Value::Object(b)) => {
|
||||
(Value::Object(a), Value::Object(b)) if a != b => {
|
||||
// Loop over old keys
|
||||
for (key, _) in a.value.iter() {
|
||||
if b.value.contains_key(key) == false {
|
||||
|
@ -33,7 +33,7 @@ impl Value {
|
|||
}
|
||||
}
|
||||
}
|
||||
(Value::Array(a), Value::Array(b)) => {
|
||||
(Value::Array(a), Value::Array(b)) if a != b => {
|
||||
let mut n = 0;
|
||||
while n < min(a.len(), b.len()) {
|
||||
let path = path.add(n.into());
|
||||
|
@ -61,7 +61,7 @@ impl Value {
|
|||
n += 1;
|
||||
}
|
||||
}
|
||||
(Value::Strand(a), Value::Strand(b)) => ops.push(Operation {
|
||||
(Value::Strand(a), Value::Strand(b)) if a != b => ops.push(Operation {
|
||||
op: Op::Change,
|
||||
path,
|
||||
value: {
|
||||
|
@ -89,6 +89,14 @@ mod tests {
|
|||
use crate::sql::array::Array;
|
||||
use crate::sql::test::Parse;
|
||||
|
||||
#[test]
|
||||
fn diff_none() {
|
||||
let old = Value::parse("{ test: true, text: 'text', other: { something: true } }");
|
||||
let now = Value::parse("{ test: true, text: 'text', other: { something: true } }");
|
||||
let res = Array::parse("[]");
|
||||
assert_eq!(res.to_operations().unwrap(), old.diff(&now, Idiom::default()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_add() {
|
||||
let old = Value::parse("{ test: true }");
|
||||
|
|
Loading…
Reference in a new issue