-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-recursive-sort.go
329 lines (296 loc) · 8.37 KB
/
go-recursive-sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package recursivesort
import (
"fmt"
"reflect"
"sort"
)
func swapValues(slice reflect.Value, tmp reflect.Value, i, j int) {
vi := slice.Index(i)
vj := slice.Index(j)
tmp.Elem().Set(vi)
vi.Set(vj)
vj.Set(tmp.Elem())
}
// TypePriorityLookupHelper is an interface to compare two types.
type TypePriorityLookupHelper interface {
CompareTypes(iv, ij reflect.Value) bool
CompareUnknownTypes(iv, ij reflect.Value) bool
}
// TypePriorityLookup maps types to integer priorities.
type TypePriorityLookup struct {
Priorities map[reflect.Type]int
}
// CompareTypes compars two known types based on their priorities.
//
// If priorities are not known, it delegates to `CompareUnknownTypes`.
func (lookup *TypePriorityLookup) CompareTypes(iv, jv reflect.Value) bool {
ivp, iok := lookup.Priorities[iv.Type()]
jvp, jok := lookup.Priorities[jv.Type()]
if !(iok && jok) {
return lookup.CompareUnknownTypes(iv, jv)
}
return ivp < jvp
}
// CompareUnknownTypes compares two types, of which at least one is not known,
// based on their string name.
func (lookup *TypePriorityLookup) CompareUnknownTypes(iv, jv reflect.Value) bool {
// order based on the type name
return iv.Type().String() < jv.Type().String()
}
// FromTypes builds a `TypePriorityLookup` priority lookup table
// based on the order of the passed types
func (lookup TypePriorityLookup) FromTypes(order ...reflect.Type) *TypePriorityLookup {
priorities := make(map[reflect.Type]int)
for idx, typ := range order {
priorities[typ] = idx
}
return &TypePriorityLookup{
Priorities: priorities,
}
}
// FromValues builds a `TypePriorityLookup` priority lookup table
// based on the order of the passed values
func (lookup TypePriorityLookup) FromValues(order ...interface{}) *TypePriorityLookup {
types := make([]reflect.Type, len(order))
for idx, i := range order {
v := reflect.ValueOf(i)
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
types[idx] = v.Type()
}
return lookup.FromTypes(types...)
}
type sortSliceOfInterfaces struct {
TypePriorityLookupHelper
v reflect.Value
tmp reflect.Value
size int
structSortField string
mapSortKey reflect.Value
strict bool
}
func (s sortSliceOfInterfaces) Len() int { return s.size }
func (s sortSliceOfInterfaces) Swap(i, j int) {
swapValues(s.v, s.tmp, i, j)
}
func getMapKey(v reflect.Value, key reflect.Value) (reflect.Value, bool) {
for _, mapKey := range v.MapKeys() {
if mapKey.Interface() == key.Interface() {
return v.MapIndex(mapKey), true
}
}
return reflect.Value{}, false
}
func (s sortSliceOfInterfaces) compareMap(iv, jv reflect.Value) bool {
if s.mapSortKey.Kind() == reflect.Invalid {
// compare by type
return s.CompareTypes(iv, jv)
}
// compare by specific map key if present
ivKey, iok := getMapKey(iv, s.mapSortKey)
jvKey, jok := getMapKey(jv, s.mapSortKey)
if !(iok && jok) {
if s.strict {
panic("has no such map key")
}
return s.CompareTypes(iv, jv)
}
return s.compareValues(ivKey, jvKey)
}
func (s sortSliceOfInterfaces) compareStruct(iv, jv reflect.Value) bool {
// compare by specific field if present
fi, fiok := iv.Type().FieldByName(s.structSortField)
fj, fjok := jv.Type().FieldByName(s.structSortField)
if !(fiok && fjok) {
if s.strict {
panic("no such field")
}
if iv.NumField() < 1 || jv.NumField() < 1 {
// cannot compare empty slices
panic("cannot compare empty slices")
}
// compare values of first exported field
for i := 0; i < iv.NumField(); i++ {
fiv := iv.Field(i)
fjv := jv.Field(i)
if fiv.CanInterface() && fjv.CanInterface() {
fivi := reflect.ValueOf(fiv.Interface())
fjvi := reflect.ValueOf(fjv.Interface())
return s.compareValues(fivi, fjvi)
}
}
panic("cannot compare struct with no exported fields")
}
fiv := iv.Field(fi.Index[0])
fjv := jv.Field(fj.Index[0])
fivi := reflect.ValueOf(fiv.Interface())
fjvi := reflect.ValueOf(fjv.Interface())
return s.compareValues(fivi, fjvi)
}
func comparePrimitiveInt(iv, jv reflect.Value) bool {
ivi := iv.Interface()
jvi := jv.Interface()
switch ivi.(type) {
case uint:
return ivi.(uint) < jvi.(uint)
case uint8: // also covers byte
return ivi.(uint8) < jvi.(uint8)
case uint16:
return ivi.(uint16) < jvi.(uint16)
case uint32:
return ivi.(uint32) < jvi.(uint32)
case uint64:
return ivi.(uint64) < jvi.(uint64)
case int:
return ivi.(int) < jvi.(int)
case int8:
return ivi.(int8) < jvi.(int8)
case int16:
return ivi.(int16) < jvi.(int16)
case int32: // also covers rune
return ivi.(int32) < jvi.(int32)
case int64:
return ivi.(int64) < jvi.(int64)
default:
panic(fmt.Sprintf("not implemented: %v", iv.Kind()))
}
}
func comparePrimitive(iv, jv reflect.Value) bool {
ivi := iv.Interface()
jvi := jv.Interface()
switch ivi.(type) {
// bool
case bool:
if ivi.(bool) == false && jvi.(bool) == true {
return true
}
return false
// string
case string:
return ivi.(string) < jvi.(string)
// integers
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
return comparePrimitiveInt(iv, jv)
// float numbers
case float32:
return ivi.(float32) < jvi.(float32)
case float64:
return ivi.(float64) < jvi.(float64)
// complex numbers
// note: there is no total ordering of complex numbers
// we resort to lexicographic ordering
case complex64:
i := ivi.(complex64)
j := jvi.(complex64)
if real(i) < real(j) {
return true
}
return imag(i) < imag(j)
case complex128:
i := ivi.(complex128)
j := jvi.(complex128)
if real(i) < real(j) {
return true
}
return imag(i) < imag(j)
default:
}
panic(fmt.Sprintf("not implemented: %v", iv.Kind()))
}
func (s sortSliceOfInterfaces) compareSameKind(iv, jv reflect.Value) bool {
switch iv.Kind() {
case reflect.Map:
return s.compareMap(iv, jv)
case reflect.Struct:
return s.compareStruct(iv, jv)
default:
return comparePrimitive(iv, jv)
}
}
func (s sortSliceOfInterfaces) compareValues(iv, jv reflect.Value) bool {
// Indirect through pointers and interfaces
for iv.Kind() == reflect.Ptr || iv.Kind() == reflect.Interface {
iv = iv.Elem()
}
for jv.Kind() == reflect.Ptr || jv.Kind() == reflect.Interface {
jv = jv.Elem()
}
// compare directly if of same type
if iv.Kind() == jv.Kind() {
return s.compareSameKind(iv, jv)
}
// otherwise sort based on type priority
return s.CompareTypes(iv, jv)
}
func (s sortSliceOfInterfaces) Less(i, j int) bool {
iv, jv := s.v.Index(i), s.v.Index(j)
return s.compareValues(iv, jv)
}
// RecursiveSort implements a recursive sort interface for arbitrary types.
type RecursiveSort struct {
TypePriorityLookupHelper
// MapSortKey specifies the key of maps to use as the sorting key if available
MapSortKey interface{}
// StructSortField specifies the field of structs to use as the sorting key if available
StructSortField string
// Strict forces using `StructSortField` and `MapSortKey`.
//
// Note: if the key or field does not exist, this will panic.
Strict bool
}
func (rec *RecursiveSort) sortMap(v reflect.Value) {
for _, k := range v.MapKeys() {
rec.sort(v.MapIndex(k))
}
}
// Sort recursively sorts an interface.
func Sort(v interface{}) {
sort := &RecursiveSort{}
sort.Sort(v)
}
// Sort recursively sorts an interface.
func (rec *RecursiveSort) Sort(v interface{}) {
rec.sort(reflect.ValueOf(v))
}
func (rec *RecursiveSort) sort(v reflect.Value) {
if rec.TypePriorityLookupHelper == nil {
rec.TypePriorityLookupHelper = TypePriorityLookup{}.FromTypes()
}
if !v.CanInterface() {
// not exported, skip
return
}
// Indirect through pointers and interfaces
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
switch v.Kind() {
case reflect.Array, reflect.Slice:
// sort slice elements first
for i := 0; i < v.Len(); i++ {
rec.sort(v.Index(i))
}
sortFunc := sortSliceOfInterfaces{
v: v,
tmp: reflect.New(v.Type().Elem()),
size: v.Len(),
mapSortKey: reflect.ValueOf(rec.MapSortKey),
structSortField: rec.StructSortField,
strict: rec.Strict,
TypePriorityLookupHelper: rec.TypePriorityLookupHelper,
}
sort.Sort(sortFunc)
case reflect.Map:
for _, k := range v.MapKeys() {
rec.sort(v.MapIndex(k))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
rec.sort(field)
}
default:
// ignore for now
}
}