Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions fieldpath/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ func (s *Set) Difference(s2 *Set) *Set {
}
}

// RecursiveDifference returns a Set containing elements which:
// * appear in s
// * do not appear in s2
//
// Compared to a regular difference,
// this removes every field **and its children** from s that is contained in s2.
//
// For example, with s containing `a.b.c` and s2 containing `a.b`,
// a RecursiveDifference will result in `a`, as the entire node `a.b` gets removed.
func (s *Set) RecursiveDifference(s2 *Set) *Set {
return &Set{
Members: *s.Members.Difference(&s2.Members),
Children: *s.Children.RecursiveDifference(s2),
}
}

// Size returns the number of members of the set.
func (s *Set) Size() int {
return s.Members.Size() + s.Children.Size()
Expand Down Expand Up @@ -333,6 +349,48 @@ func (s *SetNodeMap) Difference(s2 *Set) *SetNodeMap {
return out
}

// RecursiveDifference returns a SetNodeMap with members that appear in s but not in s2.
//
// Compared to a regular difference,
// this removes every field **and its children** from s that is contained in s2.
//
// For example, with s containing `a.b.c` and s2 containing `a.b`,
// a RecursiveDifference will result in `a`, as the entire node `a.b` gets removed.
func (s *SetNodeMap) RecursiveDifference(s2 *Set) *SetNodeMap {
out := &SetNodeMap{}

i, j := 0, 0
for i < len(s.members) && j < len(s2.Children.members) {
if s.members[i].pathElement.Less(s2.Children.members[j].pathElement) {
if !s2.Members.Has(s.members[i].pathElement) {
out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: s.members[i].set})
}
i++
} else {
if !s2.Children.members[j].pathElement.Less(s.members[i].pathElement) {
if !s2.Members.Has(s.members[i].pathElement) {
diff := s.members[i].set.RecursiveDifference(s2.Children.members[j].set)
if !diff.Empty() {
out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: diff})
}
}
i++
}
j++
}
}

if i < len(s.members) {
for _, c := range s.members[i:] {
if !s2.Members.Has(c.pathElement) {
out.members = append(out.members, c)
}
}
}

return out
}

// Iterate calls f for each PathElement in the set.
func (s *SetNodeMap) Iterate(f func(PathElement)) {
for _, n := range s.members {
Expand Down
94 changes: 94 additions & 0 deletions fieldpath/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ func BenchmarkFieldSet(b *testing.B) {
randOperand().Difference(randOperand())
}
})
b.Run(fmt.Sprintf("recursive-difference-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
randOperand().RecursiveDifference(randOperand())
}
})
}
}

Expand Down Expand Up @@ -447,6 +453,94 @@ func TestSetIntersectionDifference(t *testing.T) {
})
}

func TestSetDifference(t *testing.T) {
table := []struct {
name string
a *Set
b *Set
expectDifference *Set
expectRecursiveDifference *Set
}{
{
name: "removes simple path",
a: NewSet(MakePathOrDie("a")),
b: NewSet(MakePathOrDie("a")),
expectDifference: NewSet(),
expectRecursiveDifference: NewSet(),
},
{
name: "removes direct path",
a: NewSet(MakePathOrDie("a", "b", "c")),
b: NewSet(MakePathOrDie("a", "b", "c")),
expectDifference: NewSet(),
expectRecursiveDifference: NewSet(),
},
{
name: "only removes matching child",
a: NewSet(
MakePathOrDie("a", "b", "c"),
MakePathOrDie("b", "b", "c"),
),
b: NewSet(MakePathOrDie("a", "b", "c")),
expectDifference: NewSet(MakePathOrDie("b", "b", "c")),
expectRecursiveDifference: NewSet(MakePathOrDie("b", "b", "c")),
},
{
name: "does not remove parent of specific path",
a: NewSet(
MakePathOrDie("a"),
),
b: NewSet(MakePathOrDie("a", "aa")),
expectDifference: NewSet(MakePathOrDie("a")),
expectRecursiveDifference: NewSet(MakePathOrDie("a")),
},
{
name: "RecursiveDifference removes nested path",
a: NewSet(MakePathOrDie("a", "b", "c")),
b: NewSet(MakePathOrDie("a")),
expectDifference: NewSet(MakePathOrDie("a", "b", "c")),
expectRecursiveDifference: NewSet(),
},
{
name: "RecursiveDifference only removes nested path for matching children",
a: NewSet(
MakePathOrDie("a", "aa", "aab"),
MakePathOrDie("a", "ab", "aba"),
),
b: NewSet(MakePathOrDie("a", "aa")),
expectDifference: NewSet(
MakePathOrDie("a", "aa", "aab"),
MakePathOrDie("a", "ab", "aba"),
),
expectRecursiveDifference: NewSet(MakePathOrDie("a", "ab", "aba")),
},
{
name: "RecursiveDifference removes all matching children",
a: NewSet(
MakePathOrDie("a", "aa", "aab"),
MakePathOrDie("a", "ab", "aba"),
),
b: NewSet(MakePathOrDie("a")),
expectDifference: NewSet(
MakePathOrDie("a", "aa", "aab"),
MakePathOrDie("a", "ab", "aba"),
),
expectRecursiveDifference: NewSet(),
},
}

for _, c := range table {
t.Run(c.name, func(t *testing.T) {
if result := c.a.Difference(c.b); !result.Equals(c.expectDifference) {
t.Fatalf("Difference expected: \n%v\n, got: \n%v\n", c.expectDifference, result)
}
if result := c.a.RecursiveDifference(c.b); !result.Equals(c.expectRecursiveDifference) {
t.Fatalf("RecursiveDifference expected: \n%v\n, got: \n%v\n", c.expectRecursiveDifference, result)
}
})
}
}

func TestSetNodeMapIterate(t *testing.T) {
set := &SetNodeMap{}
toAdd := 5
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/json-iterator/go v1.1.6
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
)

go 1.13
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sigs.k8s.io/structured-merge-diff v1.0.1 h1:LOs1LZWMsz1xs77Phr/pkB4LFaavH7IVq/3+WTN9XTA=
sigs.k8s.io/structured-merge-diff v1.0.1/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
sigs.k8s.io/structured-merge-diff v1.0.2 h1:WiMoyniAVAYm03w+ImfF9IE2G23GLR/SwDnQyaNZvPk=
6 changes: 4 additions & 2 deletions internal/fixture/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ type TestCase struct {
Managed fieldpath.ManagedFields
// Set to true if the test case needs the union behavior enabled.
RequiresUnions bool
// IgnoredFields containing the set to ignore for every version
IgnoredFields map[fieldpath.APIVersion]*fieldpath.Set
}

// Test runs the test-case using the given parser and a dummy converter.
Expand Down Expand Up @@ -436,7 +438,7 @@ func (tc TestCase) PreprocessOperations(parser Parser) error {
// actually passes..
func (tc TestCase) BenchWithConverter(parser Parser, converter merge.Converter) error {
state := State{
Updater: &merge.Updater{Converter: converter},
Updater: &merge.Updater{Converter: converter, IgnoredFields: tc.IgnoredFields},
Parser: parser,
}
if tc.RequiresUnions {
Expand All @@ -456,7 +458,7 @@ func (tc TestCase) BenchWithConverter(parser Parser, converter merge.Converter)
// TestWithConverter runs the test-case using the given parser and converter.
func (tc TestCase) TestWithConverter(parser Parser, converter merge.Converter) error {
state := State{
Updater: &merge.Updater{Converter: converter},
Updater: &merge.Updater{Converter: converter, IgnoredFields: tc.IgnoredFields},
Parser: parser,
}
if tc.RequiresUnions {
Expand Down
Loading