Skip to content

Commit

Permalink
include testing for absence in checked diff
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Dec 18, 2018
1 parent 4a1e756 commit 90228d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions patch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ func (d Diff) calculate(left, right interface{}, tokens []Token) []Op {
)
}
} else { // add new
testOpTokens := append([]Token{}, newTokens...)
testOpTokens = append(testOpTokens, KeyToken{Key: fmt.Sprintf("%s", k)})
newTokens = append(newTokens, KeyToken{Key: fmt.Sprintf("%s", k), Optional: true})
ops = append(ops, ReplaceOp{Path: NewPointer(newTokens), Value: typedRight[k]})
ops = append(ops,
TestOp{Path: NewPointer(testOpTokens), Absent: true},
ReplaceOp{Path: NewPointer(newTokens), Value: typedRight[k]},
)
}
}
return ops
Expand All @@ -87,8 +92,13 @@ func (d Diff) calculate(left, right interface{}, tokens []Token) []Op {
)
// keep actualIndex the same
case i >= len(typedLeft): // add new
testOpTokens := append([]Token{}, newTokens...)
testOpTokens = append(testOpTokens, IndexToken{Index: i}) // use actual index
newTokens = append(newTokens, AfterLastIndexToken{})
ops = append(ops, ReplaceOp{Path: NewPointer(newTokens), Value: typedRight[i]})
ops = append(ops,
TestOp{Path: NewPointer(testOpTokens), Absent: true},
ReplaceOp{Path: NewPointer(newTokens), Value: typedRight[i]},
)
actualIndex++
default:
newTokens = append(newTokens, IndexToken{Index: actualIndex})
Expand Down
5 changes: 5 additions & 0 deletions patch/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var _ = Describe("Diff.Calculate", func() {
ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124},
TestOp{Path: MustNewPointerFromString("/b"), Value: 456},
RemoveOp{Path: MustNewPointerFromString("/b")},
TestOp{Path: MustNewPointerFromString("/c"), Absent: true},
ReplaceOp{Path: MustNewPointerFromString("/c?"), Value: 456},
},
)
Expand Down Expand Up @@ -130,6 +131,7 @@ var _ = Describe("Diff.Calculate", func() {
ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124},
TestOp{Path: MustNewPointerFromString("/b/b"), Value: 4056},
RemoveOp{Path: MustNewPointerFromString("/b/b")},
TestOp{Path: MustNewPointerFromString("/b/c"), Absent: true},
ReplaceOp{Path: MustNewPointerFromString("/b/c?"), Value: 4056},
},
)
Expand Down Expand Up @@ -169,7 +171,9 @@ var _ = Describe("Diff.Calculate", func() {
[]Op{
TestOp{Path: MustNewPointerFromString("/0"), Value: "a"},
ReplaceOp{Path: MustNewPointerFromString("/0"), Value: "b"},
TestOp{Path: MustNewPointerFromString("/1"), Absent: true},
ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 123},
TestOp{Path: MustNewPointerFromString("/2"), Absent: true},
ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 456},
},
)
Expand Down Expand Up @@ -204,6 +208,7 @@ var _ = Describe("Diff.Calculate", func() {
[]Op{
TestOp{Path: MustNewPointerFromString("/1"), Value: 456},
ReplaceOp{Path: MustNewPointerFromString("/1"), Value: "a"},
TestOp{Path: MustNewPointerFromString("/2"), Absent: true},
ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 456},
},
)
Expand Down

0 comments on commit 90228d4

Please sign in to comment.