Skip to content

Commit

Permalink
Void merge strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Apr 25, 2022
1 parent e20ae8e commit 1c36706
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/void.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,29 @@ func (v voidNode) Patch(d Diff) (JsonNode, error) {
}

func (v voidNode) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if len(pathAhead) != 0 {
if !pathAhead.isLeaf() {
return patchErrExpectColl(v, pathBehind[len(pathBehind)-1])
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
if !v.Equals(oldValue) {
return patchErrExpectValue(oldValue, v, pathBehind)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
// Null deletes a node
return voidNode{}, nil
}
case strictPatchStrategy:
if !v.Equals(oldValue) {
return patchErrExpectValue(oldValue, v, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil
}
7 changes: 7 additions & 0 deletions lib/void_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func TestVoidPatch(t *testing.T) {
checkPatch(ctx, `1`, ``,
`@ []`,
`- 1`)
checkPatch(ctx, ``, `1`,
`@ [["merge"]]`,
`+ 1`)
// Null deletes a node
checkPatch(ctx, ``, ``,
`@ [["merge"]]`,
`+ null`)
}

func TestVoidPatchError(t *testing.T) {
Expand Down

0 comments on commit 1c36706

Please sign in to comment.