Skip to content

Commit

Permalink
don't mutate the map when comparing it
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed May 8, 2024
1 parent 7d4fe30 commit d34abd9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions log/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,9 @@ func (v Value) Equal(w Value) bool {
case KindSlice:
return slices.EqualFunc(v.asSlice(), w.asSlice(), Value.Equal)
case KindMap:
sv := v.asMap()
slices.SortFunc(sv, func(a, b KeyValue) int {
return cmp.Compare(a.Key, b.Key)
})
sw := w.asMap()
slices.SortFunc(sw, func(a, b KeyValue) int {
return cmp.Compare(a.Key, b.Key)
})
return slices.EqualFunc(v.asMap(), w.asMap(), KeyValue.Equal)
sv := sortMap(v.asMap())
sw := sortMap(w.asMap())
return slices.EqualFunc(sv, sw, KeyValue.Equal)
case KindBytes:
return bytes.Equal(v.asBytes(), w.asBytes())
case KindEmpty:
Expand All @@ -276,6 +270,16 @@ func (v Value) Equal(w Value) bool {
}
}

func sortMap(m []KeyValue) []KeyValue {
sm := make([]KeyValue, len(m))
copy(sm, m)
slices.SortFunc(sm, func(a, b KeyValue) int {
return cmp.Compare(a.Key, b.Key)
})

return sm
}

// String returns Value's value as a string, formatted like [fmt.Sprint].
//
// The returned string is meant for debugging;
Expand Down

0 comments on commit d34abd9

Please sign in to comment.