File tree Expand file tree Collapse file tree 2 files changed +25
-8
lines changed
Expand file tree Collapse file tree 2 files changed +25
-8
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
5562func (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,
You can’t perform that action at this time.
0 commit comments