Skip to content

Commit 02d431a

Browse files
author
Kevin Wiesmueller
committed
update RecursiveDifference comment and extend tests
1 parent 73b21b9 commit 02d431a

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

fieldpath/set.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ func (s *Set) Difference(s2 *Set) *Set {
9898
// * appear in s
9999
// * do not appear in s2
100100
//
101-
// Compared to a regular difference, this recursively removes
102-
// all children from s
103-
// that are either children or members in s2
101+
// Compared to a regular difference,
102+
// this removes every field **and its children** from s that is contained in s2.
103+
//
104+
// For example, with s containing `a.b.c` and s2 containing `a.b`,
105+
// a RecursiveDifference will result in `a`, as the entire node `a.b` gets removed.
104106
func (s *Set) RecursiveDifference(s2 *Set) *Set {
105107
return &Set{
106108
Members: *s.Members.Difference(&s2.Members),
@@ -349,20 +351,19 @@ func (s *SetNodeMap) Difference(s2 *Set) *SetNodeMap {
349351

350352
// RecursiveDifference returns a SetNodeMap with members that appear in s but not in s2.
351353
//
352-
// Compared to a regular difference, this recursively removes
353-
// all children from s
354-
// that are either children or members in s2
354+
// Compared to a regular difference,
355+
// this removes every field **and its children** from s that is contained in s2.
356+
//
357+
// For example, with s containing `a.b.c` and s2 containing `a.b`,
358+
// a RecursiveDifference will result in `a`, as the entire node `a.b` gets removed.
355359
func (s *SetNodeMap) RecursiveDifference(s2 *Set) *SetNodeMap {
356360
out := &SetNodeMap{}
357361

358362
i, j := 0, 0
359363
for i < len(s.members) && j < len(s2.Children.members) {
360364
if s.members[i].pathElement.Less(s2.Children.members[j].pathElement) {
361365
if !s2.Members.Has(s.members[i].pathElement) {
362-
out.members = append(out.members, setNode{
363-
pathElement: s.members[i].pathElement,
364-
set: s.members[i].set,
365-
})
366+
out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: s.members[i].set})
366367
}
367368
i++
368369
} else {

fieldpath/set_test.go

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -453,66 +453,89 @@ func TestSetIntersectionDifference(t *testing.T) {
453453
})
454454
}
455455

456-
func TestSetRecursiveDifference(t *testing.T) {
456+
func TestSetDifference(t *testing.T) {
457457
table := []struct {
458-
name string
459-
a *Set
460-
b *Set
461-
expect *Set
458+
name string
459+
a *Set
460+
b *Set
461+
expectDifference *Set
462+
expectRecursiveDifference *Set
462463
}{
463464
{
464-
name: "removes simple path",
465-
a: NewSet(MakePathOrDie("a")),
466-
b: NewSet(MakePathOrDie("a")),
467-
expect: NewSet(),
465+
name: "removes simple path",
466+
a: NewSet(MakePathOrDie("a")),
467+
b: NewSet(MakePathOrDie("a")),
468+
expectDifference: NewSet(),
469+
expectRecursiveDifference: NewSet(),
468470
},
469471
{
470-
name: "removes direct path",
471-
a: NewSet(MakePathOrDie("a", "b", "c")),
472-
b: NewSet(MakePathOrDie("a", "b", "c")),
473-
expect: NewSet(),
472+
name: "removes direct path",
473+
a: NewSet(MakePathOrDie("a", "b", "c")),
474+
b: NewSet(MakePathOrDie("a", "b", "c")),
475+
expectDifference: NewSet(),
476+
expectRecursiveDifference: NewSet(),
474477
},
475478
{
476479
name: "only removes matching child",
477480
a: NewSet(
478481
MakePathOrDie("a", "b", "c"),
479482
MakePathOrDie("b", "b", "c"),
480483
),
481-
b: NewSet(MakePathOrDie("a", "b", "c")),
482-
expect: NewSet(MakePathOrDie("b", "b", "c")),
484+
b: NewSet(MakePathOrDie("a", "b", "c")),
485+
expectDifference: NewSet(MakePathOrDie("b", "b", "c")),
486+
expectRecursiveDifference: NewSet(MakePathOrDie("b", "b", "c")),
483487
},
484488
{
485-
name: "removes nested path",
486-
a: NewSet(MakePathOrDie("a", "b", "c")),
487-
b: NewSet(MakePathOrDie("a")),
488-
expect: NewSet(),
489+
name: "does not remove parent of specific path",
490+
a: NewSet(
491+
MakePathOrDie("a"),
492+
),
493+
b: NewSet(MakePathOrDie("a", "aa")),
494+
expectDifference: NewSet(MakePathOrDie("a")),
495+
expectRecursiveDifference: NewSet(MakePathOrDie("a")),
489496
},
490497
{
491-
name: "only removes nested path for matching children",
498+
name: "RecursiveDifference removes nested path",
499+
a: NewSet(MakePathOrDie("a", "b", "c")),
500+
b: NewSet(MakePathOrDie("a")),
501+
expectDifference: NewSet(MakePathOrDie("a", "b", "c")),
502+
expectRecursiveDifference: NewSet(),
503+
},
504+
{
505+
name: "RecursiveDifference only removes nested path for matching children",
492506
a: NewSet(
493507
MakePathOrDie("a", "aa", "aab"),
494508
MakePathOrDie("a", "ab", "aba"),
495509
),
496510
b: NewSet(MakePathOrDie("a", "aa")),
497-
expect: NewSet(
511+
expectDifference: NewSet(
512+
MakePathOrDie("a", "aa", "aab"),
498513
MakePathOrDie("a", "ab", "aba"),
499514
),
515+
expectRecursiveDifference: NewSet(MakePathOrDie("a", "ab", "aba")),
500516
},
501517
{
502-
name: "remove all matching children",
518+
name: "RecursiveDifference removes all matching children",
503519
a: NewSet(
504520
MakePathOrDie("a", "aa", "aab"),
505521
MakePathOrDie("a", "ab", "aba"),
506522
),
507-
b: NewSet(MakePathOrDie("a")),
508-
expect: NewSet(),
523+
b: NewSet(MakePathOrDie("a")),
524+
expectDifference: NewSet(
525+
MakePathOrDie("a", "aa", "aab"),
526+
MakePathOrDie("a", "ab", "aba"),
527+
),
528+
expectRecursiveDifference: NewSet(),
509529
},
510530
}
511531

512532
for _, c := range table {
513533
t.Run(c.name, func(t *testing.T) {
514-
if result := c.a.RecursiveDifference(c.b); !result.Equals(c.expect) {
515-
t.Fatalf("expected: \n%v\n, got: \n%v\n", c.expect, result)
534+
if result := c.a.Difference(c.b); !result.Equals(c.expectDifference) {
535+
t.Fatalf("Difference expected: \n%v\n, got: \n%v\n", c.expectDifference, result)
536+
}
537+
if result := c.a.RecursiveDifference(c.b); !result.Equals(c.expectRecursiveDifference) {
538+
t.Fatalf("RecursiveDifference expected: \n%v\n, got: \n%v\n", c.expectRecursiveDifference, result)
516539
}
517540
})
518541
}

0 commit comments

Comments
 (0)