Skip to content

Commit 3fd7b43

Browse files
author
Antoine Pelisse
committed
Change equal for path element
1 parent a27a92b commit 3fd7b43

File tree

3 files changed

+87
-28
lines changed

3 files changed

+87
-28
lines changed

fieldpath/element.go

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,90 @@ type PathElement struct {
4949

5050
// Less provides an order for path elements.
5151
func (e PathElement) Less(rhs PathElement) bool {
52+
return e.Compare(rhs) < 0
53+
}
54+
55+
// Compare provides an order for path elements.
56+
func (e PathElement) Compare(rhs PathElement) int {
5257
if e.FieldName != nil {
5358
if rhs.FieldName == nil {
54-
return true
59+
return -1
5560
}
56-
return *e.FieldName < *rhs.FieldName
61+
return strings.Compare(*e.FieldName, *rhs.FieldName)
5762
} else if rhs.FieldName != nil {
58-
return false
63+
return 1
5964
}
6065

6166
if e.Key != nil {
6267
if rhs.Key == nil {
63-
return true
68+
return -1
6469
}
65-
return e.Key.Less(*rhs.Key)
70+
return e.Key.Compare(*rhs.Key)
6671
} else if rhs.Key != nil {
67-
return false
72+
return 1
6873
}
6974

7075
if e.Value != nil {
7176
if rhs.Value == nil {
72-
return true
77+
return -1
7378
}
74-
return value.Less(*e.Value, *rhs.Value)
79+
return value.Compare(*e.Value, *rhs.Value)
7580
} else if rhs.Value != nil {
76-
return false
81+
return 1
7782
}
7883

7984
if e.Index != nil {
8085
if rhs.Index == nil {
81-
return true
86+
return -1
8287
}
83-
return *e.Index < *rhs.Index
88+
if *e.Index < *rhs.Index {
89+
return -1
90+
} else if *e.Index == *rhs.Index {
91+
return 0
92+
}
93+
return 1
8494
} else if rhs.Index != nil {
85-
// Yes, I know the next statement is the same. But this way
86-
// the obvious way of extending the function wil be bug-free.
87-
return false
95+
return 1
8896
}
8997

90-
return false
98+
return 0
9199
}
92100

93101
// Equals returns true if both path elements are equal.
94102
func (e PathElement) Equals(rhs PathElement) bool {
95-
return !e.Less(rhs) && !rhs.Less(e)
103+
if e.FieldName != nil {
104+
if rhs.FieldName == nil {
105+
return false
106+
}
107+
return *e.FieldName == *rhs.FieldName
108+
} else if rhs.FieldName != nil {
109+
return false
110+
}
111+
if e.Key != nil {
112+
if rhs.Key == nil {
113+
return false
114+
}
115+
return e.Key.Equals(*rhs.Key)
116+
} else if rhs.Key != nil {
117+
return false
118+
}
119+
if e.Value != nil {
120+
if rhs.Value == nil {
121+
return false
122+
}
123+
return value.Equals(*e.Value, *rhs.Value)
124+
} else if rhs.Value != nil {
125+
return false
126+
}
127+
if e.Index != nil {
128+
if rhs.Index == nil {
129+
return false
130+
}
131+
return *e.Index == *rhs.Index
132+
} else if rhs.Index != nil {
133+
return false
134+
}
135+
return true
96136
}
97137

98138
// String presents the path element as a human-readable string.

fieldpath/path.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,35 @@ func (fp Path) String() string {
3737

3838
// Equals returns true if the two paths are equivalent.
3939
func (fp Path) Equals(fp2 Path) bool {
40-
return !fp.Less(fp2) && !fp2.Less(fp)
40+
if len(fp) != len(fp2) {
41+
return false
42+
}
43+
for i := range fp {
44+
if !fp[i].Equals(fp2[i]) {
45+
return false
46+
}
47+
}
48+
return true
4149
}
4250

4351
// Less provides a lexical order for Paths.
44-
func (fp Path) Less(rhs Path) bool {
52+
func (fp Path) Compare(rhs Path) int {
4553
i := 0
4654
for {
4755
if i >= len(fp) && i >= len(rhs) {
4856
// Paths are the same length and all items are equal.
49-
return false
57+
return 0
5058
}
5159
if i >= len(fp) {
5260
// LHS is shorter.
53-
return true
61+
return -1
5462
}
5563
if i >= len(rhs) {
5664
// RHS is shorter.
57-
return false
65+
return 1
5866
}
59-
if fp[i].Less(rhs[i]) {
60-
// LHS is less; return
61-
return true
62-
}
63-
if rhs[i].Less(fp[i]) {
64-
// RHS is less; return
65-
return false
67+
if c := fp[i].Compare(rhs[i]); c != 0 {
68+
return c
6669
}
6770
// The items are equal; continue.
6871
i++

value/fields.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@ func (f FieldList) Compare(rhs FieldList) int {
7979
i++
8080
}
8181
}
82+
83+
// Equals returns true if the two fieldslist are equals, false otherwise.
84+
func (f FieldList) Equals(rhs FieldList) bool {
85+
if len(f) != len(rhs) {
86+
return false
87+
}
88+
for i := range f {
89+
if f[i].Name != rhs[i].Name {
90+
return false
91+
}
92+
if !Equals(f[i].Value, rhs[i].Value) {
93+
return false
94+
}
95+
}
96+
return true
97+
}

0 commit comments

Comments
 (0)