Skip to content

Commit

Permalink
typed: Replace duplicates rather than merging
Browse files Browse the repository at this point in the history
  • Loading branch information
apelisse committed Oct 19, 2023
1 parent c9d20e7 commit 4daa91c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions typed/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
if pe.Equals(rhsPEs[rI]) {
// merge LHS & RHS items
mergedRHS.Insert(pe, struct{}{})
lChild, _ := observedLHS.Get(pe)
lChild, _ := observedLHS.Get(pe) // may be nil if the PE is duplicaated.
rChild, _ := observedRHS.Get(pe)
mergeOut, errs := w.mergeListItem(t, pe, lChild, rChild)
errs = append(errs, errs...)
Expand All @@ -237,7 +237,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
if lI < lLen {
pe := lhsPEs[lI]
if _, ok := observedRHS.Get(pe); !ok {
// take LHS item
// take LHS item using At to make sure we get the right item (observed may not contain the right item).
lChild := lhs.AtUsing(w.allocator, lI)
mergeOut, errs := w.mergeListItem(t, pe, lChild, nil)
errs = append(errs, errs...)
Expand All @@ -255,7 +255,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
// Take the RHS item, merge with matching LHS item if possible
pe := rhsPEs[rI]
mergedRHS.Insert(pe, struct{}{})
lChild, _ := observedLHS.Get(pe) // may be nil
lChild, _ := observedLHS.Get(pe) // may be nil if absent or duplicaated.
rChild, _ := observedRHS.Get(pe)
mergeOut, errs := w.mergeListItem(t, pe, lChild, rChild)
errs = append(errs, errs...)
Expand Down Expand Up @@ -305,6 +305,9 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List, a
continue
} else if !found {
observed.Insert(pe, child)
} else {
// Duplicated items are not merged with the new value, make them nil.
observed.Insert(pe, value.NewValueInterface(nil))
}
pes = append(pes, pe)
}
Expand Down
2 changes: 1 addition & 1 deletion typed/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ var mergeCases = []mergeTestCase{{
}, {
`{"list":[{"key":"a","id":1,"bv":true},{"key":"b","id":2},{"key":"a","id":1,"bv":false,"nv":2}]}`,
`{"list":[{"key":"a","id":1,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,
`{"list":[{"key":"a","id":1,"bv":true,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,
`{"list":[{"key":"a","id":1,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,
}, {
`{"list":[{"key":"a","id":1,"nv":1},{"key":"a","id":1,"nv":2}]}`,
`{"list":[]}`,
Expand Down

0 comments on commit 4daa91c

Please sign in to comment.