Skip to content

Commit 974d6fb

Browse files
authored
fix Delta.DifferentAt behavior via changes to Path.Contains (#20)
Fixes issue aws-controllers-k8s/community#808
1 parent 8191f60 commit 974d6fb

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

pkg/compare/delta_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ func TestDifferentAt(t *testing.T) {
5353
d = compare.NewDelta()
5454
d.Add("Bar", a.Bar, b.Bar)
5555
require.True(d.DifferentAt("Bar"))
56-
require.False(d.DifferentAt("Baz"))
56+
require.False(d.DifferentAt("Baz")) // diff exists but was not added to Delta
5757

5858
d = compare.NewDelta()
5959
d.Add("Baz.Y", a.Baz.Y, b.Baz.Y)
6060
require.True(d.DifferentAt("Baz"))
61-
require.True(d.DifferentAt("Y"))
62-
require.False(d.DifferentAt("Bar"))
61+
require.True(d.DifferentAt("Baz.Y"))
62+
require.False(d.DifferentAt("Y")) // there is no diff for top-level field "Y"
63+
require.False(d.DifferentAt("Bar")) // diff exists but it was not added to Delta
64+
require.False(d.DifferentAt("Baz.Y.Z")) // subject length exceeds length of diff Path
65+
require.False(d.DifferentAt("Baz.Z")) // matches Path top-level field but not sub-field
6366
}

pkg/compare/path.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,28 @@ func (p Path) Pop() {
5151
}
5252
}
5353

54-
// Contains returns true if the supplied string appears within the Path
54+
// Contains returns true if the supplied string, delimited on ".", matches
55+
// p.parts up to the length of the supplied string.
56+
// e.g. if the Path p represents "A.B":
57+
// subject "A" -> true
58+
// subject "A.B" -> true
59+
// subject "A.B.C" -> false
60+
// subject "B" -> false
61+
// subject "A.C" -> false
5562
func (p Path) Contains(subject string) bool {
56-
for _, p := range p.parts {
57-
if p == subject {
58-
return true
63+
subjectSplit := strings.Split(subject, ".")
64+
65+
if len(subjectSplit) > len(p.parts) {
66+
return false
67+
}
68+
69+
for i, s := range subjectSplit {
70+
if p.parts[i] != s {
71+
return false
5972
}
6073
}
61-
return false
74+
75+
return true
6276
}
6377

6478
// NewPath returns a new Path struct pointer from a dotted-notation string,

0 commit comments

Comments
 (0)