Skip to content

Commit

Permalink
Common patch method.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Jun 17, 2022
1 parent 6ee0475 commit 027b386
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 146 deletions.
31 changes: 6 additions & 25 deletions lib/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,10 @@ func (b jsonBool) Patch(d Diff) (JsonNode, error) {
return patchAll(b, d)
}

func (b jsonBool) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if !pathAhead.isLeaf() {
return patchErrExpectColl(b, pathAhead[0])
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
return voidNode{}, nil
}
case strictPatchStrategy:
if !b.Equals(oldValue) {
return patchErrExpectValue(oldValue, b, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil
func (b jsonBool) patch(
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
return patch(b, pathBehind, pathAhead, oldValues, newValues, strategy)
}
32 changes: 6 additions & 26 deletions lib/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,10 @@ func (n jsonNull) Patch(d Diff) (JsonNode, error) {
return patchAll(n, d)
}

func (n jsonNull) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if !pathAhead.isLeaf() {
return patchErrExpectColl(n, pathAhead[0])
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
// Null deletes a node
return voidNode{}, nil
}
case strictPatchStrategy:
if !n.Equals(oldValue) {
return patchErrExpectValue(oldValue, n, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil
func (n jsonNull) patch(
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
return patch(n, pathBehind, pathAhead, oldValues, newValues, strategy)
}
32 changes: 6 additions & 26 deletions lib/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,10 @@ func (n jsonNumber) Patch(d Diff) (JsonNode, error) {
return patchAll(n, d)
}

func (n jsonNumber) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if !pathAhead.isLeaf() {
return patchErrExpectColl(n, pathAhead[0])
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
// Null deletes a node
return voidNode{}, nil
}
case strictPatchStrategy:
if !n.Equals(oldValue) {
return patchErrExpectValue(oldValue, n, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil
func (n jsonNumber) patch(
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
return patch(n, pathBehind, pathAhead, oldValues, newValues, strategy)
}
48 changes: 48 additions & 0 deletions lib/patch_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,54 @@ func patchAll(n JsonNode, d Diff) (JsonNode, error) {
return n, nil
}

func patch(
node JsonNode,
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
if !pathAhead.isLeaf() {
if strategy != mergePatchStrategy {
return patchErrExpectColl(node, pathAhead[0])
}
next, _, rest := pathAhead.next()
key, ok := next.(jsonString)
if !ok {
return nil, fmt.Errorf("Merge patch path must be composed of only strings. Found %v", next)
}
o := newJsonObject()
value, err := node.patch(append(pathBehind.clone(), key), rest, oldValues, newValues, strategy)
if err != nil {
return nil, err
}
o.properties[string(key)] = value
return o, nil
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
// Null deletes a node
return voidNode{}, nil
}
case strictPatchStrategy:
if !node.Equals(oldValue) {
return patchErrExpectValue(oldValue, node, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil

}

func singleValue(nodes []JsonNode) JsonNode {
if len(nodes) == 0 {
return voidNode{}
Expand Down
32 changes: 6 additions & 26 deletions lib/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,10 @@ func (s jsonString) Patch(d Diff) (JsonNode, error) {
return patchAll(s, d)
}

func (s jsonString) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if !pathAhead.isLeaf() {
return patchErrExpectColl(s, pathBehind[0])
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)
}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
switch strategy {
case mergePatchStrategy:
if !isVoid(oldValue) {
return patchErrMergeWithOldValue(pathBehind, oldValue)
}
if isNull(newValue) {
// Null deletes a node
return voidNode{}, nil
}
case strictPatchStrategy:
if !s.Equals(oldValue) {
return patchErrExpectValue(oldValue, s, pathBehind)
}
default:
return patchErrUnsupportedPatchStrategy(pathBehind, strategy)
}
return newValue, nil
func (s jsonString) patch(
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
return patch(s, pathBehind, pathAhead, oldValues, newValues, strategy)
}
49 changes: 6 additions & 43 deletions lib/void.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package jd

import "fmt"

type voidNode struct{}

var _ JsonNode = voidNode{}
Expand Down Expand Up @@ -81,45 +79,10 @@ func (v voidNode) Patch(d Diff) (JsonNode, error) {
return patchAll(v, d)
}

func (v voidNode) patch(pathBehind, pathAhead path, oldValues, newValues []JsonNode, strategy patchStrategy) (JsonNode, error) {
if !pathAhead.isLeaf() {
if strategy != mergePatchStrategy {
return patchErrExpectColl(v, pathAhead[0])
}
next, _, rest := pathAhead.next()
key, ok := next.(jsonString)
if !ok {
return nil, fmt.Errorf("Merge patch path must be composed of only strings. Found %v", next)
}
o := newJsonObject()
value, err := v.patch(append(pathBehind.clone(), key), rest, oldValues, newValues, strategy)
if err != nil {
return nil, err
}
o.properties[string(key)] = value
return o, nil
}
if len(oldValues) > 1 || len(newValues) > 1 {
return patchErrNonSetDiff(oldValues, newValues, pathBehind)

}
oldValue := singleValue(oldValues)
newValue := singleValue(newValues)
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
func (v voidNode) patch(
pathBehind, pathAhead path,
oldValues, newValues []JsonNode,
strategy patchStrategy,
) (JsonNode, error) {
return patch(v, pathBehind, pathAhead, oldValues, newValues, strategy)
}

0 comments on commit 027b386

Please sign in to comment.