-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_map_keys.go
46 lines (41 loc) · 983 Bytes
/
sort_map_keys.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
package jsoniterpb
import (
"reflect"
"strconv"
jsoniter "github.com/json-iterator/go"
)
func (e *ProtoExtension) updateMapEncoderConstructorForSortMapKeys(v *jsoniter.MapEncoderConstructor) {
if e.SortMapKeysAsString {
return
}
v.DecorateFunc = func(mapEncoder jsoniter.ValEncoder) jsoniter.ValEncoder {
enc, ok := mapEncoder.(*jsoniter.SortKeysMapEncoder)
if !ok {
return mapEncoder
}
// protobuf-go GenericKeyOrder
switch v.MapType.Key().Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
enc.KeyLess = func(x, y string) bool {
xi, _ := strconv.ParseInt(x, 10, 64)
yi, _ := strconv.ParseInt(y, 10, 64)
return xi < yi
}
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
enc.KeyLess = func(x, y string) bool {
xi, _ := strconv.ParseUint(x, 10, 64)
yi, _ := strconv.ParseUint(y, 10, 64)
return xi < yi
}
}
return enc
}
}