Skip to content

Commit bc29f6d

Browse files
author
Antoine Pelisse
committed
Add generic implementation for MapEquals
1 parent 328fea4 commit bc29f6d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

value/map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Map interface {
3030
// Delete removes the key from the map.
3131
Delete(key string)
3232
// Equals compares the two maps, and return true if they are the same, false otherwise.
33+
// Implementations can use MapEquals as a general implementation for this methods.
3334
Equals(other Map) bool
3435
// Iterate runs the given function for each key/value in the
3536
// map. Returning false in the closure prematurely stops the
@@ -87,3 +88,24 @@ func MapCompare(lhs, rhs Map) int {
8788
i++
8889
}
8990
}
91+
92+
// MapEquals returns true if lhs == rhs, false otherwise. This function
93+
// acts on generic types and should not be used by callers, but can help
94+
// implement Map.Equals.
95+
func MapEquals(lhs, rhs Map) bool {
96+
if lhs.Length() != rhs.Length() {
97+
return false
98+
}
99+
return lhs.Iterate(func(k string, v Value) bool {
100+
vo, ok := rhs.Get(k)
101+
if !ok {
102+
return false
103+
}
104+
if !Equals(v, vo) {
105+
vo.Recycle()
106+
return false
107+
}
108+
vo.Recycle()
109+
return true
110+
})
111+
}

0 commit comments

Comments
 (0)