MCPcopy Create free account
hub / github.com/google/go-cmp / SortMaps

Function SortMaps

cmp/cmpopts/sort.go:100–107  ·  view source on GitHub ↗

SortMaps returns a [cmp.Transformer] option that flattens map[K]V types to be a sorted []struct{K, V}. The less function must be of the form "func(T, T) bool" which is used to sort any map with key K that is assignable to T. Flattening the map into a slice has the property that [cmp.Equal] is able

(lessFunc interface{})

Source from the content-addressed store, hash-verified

98//
99// SortMaps can be used in conjunction with [EquateEmpty].
100func SortMaps(lessFunc interface{}) cmp.Option {
101 vf := reflect.ValueOf(lessFunc)
102 if !function.IsType(vf.Type(), function.Less) || vf.IsNil() {
103 panic(fmt.Sprintf("invalid less function: %T", lessFunc))
104 }
105 ms := mapSorter{vf.Type().In(0), vf}
106 return cmp.FilterValues(ms.filter, cmp.Transformer("cmpopts.SortMaps", ms.sort))
107}
108
109type mapSorter struct {
110 in reflect.Type // T

Callers 1

TestOptionsFunction · 0.85

Calls 5

IsTypeFunction · 0.92
FilterValuesFunction · 0.92
TransformerFunction · 0.92
IsNilMethod · 0.80
TypeMethod · 0.65

Tested by 1

TestOptionsFunction · 0.68