Skip to content

Commit

Permalink
Merge strategy for new object elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed May 20, 2022
1 parent d78a368 commit 8488135
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ func (o1 jsonObject) diff(n JsonNode, path path, metadata []Metadata, strategy p
v2 := o2.properties[k2]
if _, ok := o1.properties[k2]; !ok {
// O1 missing key
e := DiffElement{
Path: append(path, jsonString(k2)).clone(),
OldValues: nodeList(),
NewValues: nodeList(v2),
var e DiffElement
switch strategy {
case mergePatchStrategy:
e = DiffElement{
Path: append(path, jsonString(k2)).clone().prependMetadataMerge(),
OldValues: nodeList(),
NewValues: nodeList(v2),
}
default:
e = DiffElement{
Path: append(path, jsonString(k2)).clone(),
OldValues: nodeList(),
NewValues: nodeList(v2),
}
}
d = append(d, e)
}
Expand Down
10 changes: 10 additions & 0 deletions lib/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ func TestObjectDiff(t *testing.T) {
`+`,
),
ctx: newTestContext(t).withMetadata(MERGE),
}, {
a: `{"a":1}`,
b: `{"b":1}`,
diff: ss(
`@ [["merge"],"a"]`,
`+`,
`@ [["merge"],"b"]`,
`+ 1`,
),
ctx: newTestContext(t).withMetadata(MERGE),
}}

for _, tt := range tests {
Expand Down

0 comments on commit 8488135

Please sign in to comment.