Skip to content

Commit

Permalink
fix Delta.DifferentAt behavior via changes to Path.Contains (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
echen-98 authored Jun 8, 2021
1 parent 8191f60 commit 974d6fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 6 additions & 3 deletions pkg/compare/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ func TestDifferentAt(t *testing.T) {
d = compare.NewDelta()
d.Add("Bar", a.Bar, b.Bar)
require.True(d.DifferentAt("Bar"))
require.False(d.DifferentAt("Baz"))
require.False(d.DifferentAt("Baz")) // diff exists but was not added to Delta

d = compare.NewDelta()
d.Add("Baz.Y", a.Baz.Y, b.Baz.Y)
require.True(d.DifferentAt("Baz"))
require.True(d.DifferentAt("Y"))
require.False(d.DifferentAt("Bar"))
require.True(d.DifferentAt("Baz.Y"))
require.False(d.DifferentAt("Y")) // there is no diff for top-level field "Y"
require.False(d.DifferentAt("Bar")) // diff exists but it was not added to Delta
require.False(d.DifferentAt("Baz.Y.Z")) // subject length exceeds length of diff Path
require.False(d.DifferentAt("Baz.Z")) // matches Path top-level field but not sub-field
}
24 changes: 19 additions & 5 deletions pkg/compare/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,28 @@ func (p Path) Pop() {
}
}

// Contains returns true if the supplied string appears within the Path
// Contains returns true if the supplied string, delimited on ".", matches
// p.parts up to the length of the supplied string.
// e.g. if the Path p represents "A.B":
// subject "A" -> true
// subject "A.B" -> true
// subject "A.B.C" -> false
// subject "B" -> false
// subject "A.C" -> false
func (p Path) Contains(subject string) bool {
for _, p := range p.parts {
if p == subject {
return true
subjectSplit := strings.Split(subject, ".")

if len(subjectSplit) > len(p.parts) {
return false
}

for i, s := range subjectSplit {
if p.parts[i] != s {
return false
}
}
return false

return true
}

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

0 comments on commit 974d6fb

Please sign in to comment.