MCPcopy
hub / github.com/google/go-cmp / SortMaps

Function SortMaps

cmp/cmpopts/sort.go:119–126  ·  view source on GitHub ↗

SortMaps returns a [cmp.Transformer] option that flattens map[K]V types to be a sorted []struct{K, V}. The lessOrCompareFunc function must be either a less function of the form "func(T, T) bool" or a compare function of the format "func(T, T) int" which is used to sort any map with key K that is ass

(lessOrCompareFunc interface{})

Source from the content-addressed store, hash-verified

117//
118// SortMaps can be used in conjunction with [EquateEmpty].
119func SortMaps(lessOrCompareFunc interface{}) cmp.Option {
120 vf := reflect.ValueOf(lessOrCompareFunc)
121 if (!function.IsType(vf.Type(), function.Less) && !function.IsType(vf.Type(), function.Compare)) || vf.IsNil() {
122 panic(fmt.Sprintf("invalid less or compare function: %T", lessOrCompareFunc))
123 }
124 ms := mapSorter{vf.Type().In(0), vf}
125 return cmp.FilterValues(ms.filter, cmp.Transformer("cmpopts.SortMaps", ms.sort))
126}
127
128type mapSorter struct {
129 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