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{})
| 98 | // |
| 99 | // SortMaps can be used in conjunction with [EquateEmpty]. |
| 100 | func 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 | |
| 109 | type mapSorter struct { |
| 110 | in reflect.Type // T |