-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalue_collate.go
146 lines (127 loc) · 2.64 KB
/
value_collate.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
// transform golang native value into binary collated encoding.
// cnf: NumberKind, doMissing, arrayLenPrefix, propertyLenPrefix
package gson
import "fmt"
import "strconv"
import "encoding/json"
func gson2collate(obj interface{}, code []byte, config *Config) int {
if obj == nil {
code[0], code[1] = TypeNull, Terminator
return 2
}
switch value := obj.(type) {
case bool:
if value {
code[0] = TypeTrue
} else {
code[0] = TypeFalse
}
code[1] = Terminator
return 2
case float64:
n := 0
code[n] = TypeNumber
n++
n += collateFloat64(value, code[n:])
code[n] = Terminator
n++
return n
case float32:
n := 0
code[n] = TypeNumber
n++
n += collateFloat64(float64(value), code[n:])
code[n] = Terminator
n++
return n
case int64:
n := 0
code[n] = TypeNumber
n++
n += collateInt64(value, code[n:], config)
code[n] = Terminator
n++
return n
case uint64:
n := 0
code[n] = TypeNumber
n++
n += collateUint64(value, code[n:], config)
code[n] = Terminator
n++
return n
case int:
n := 0
code[n] = TypeNumber
n++
n += collateInt64(int64(value), code[n:], config)
code[n] = Terminator
n++
return n
case json.Number:
n := 0
code[n] = TypeNumber
n++
n += collateJsonNumber(string(value), code[n:], config)
code[n] = Terminator
n++
return n
case Missing:
if config.doMissing && MissingLiteral.Equal(string(value)) {
code[0], code[1] = TypeMissing, Terminator
return 2
}
panic("collate missing not configured")
case string:
return collateString(value, code, config)
case []byte:
n := 0
code[n] = TypeBinary
n++
m := copy(code[n:], value)
n += m
code[n] = Terminator
n++
return n
case []interface{}:
n := 0
code[n] = TypeArray
n++
if config.arrayLenPrefix {
n += collateLength(len(value), code[n:])
}
for _, val := range value {
n += gson2collate(val, code[n:], config)
}
code[n] = Terminator
n++
return n
case map[string]interface{}:
n := 0
code[n] = TypeObj
n++
if config.propertyLenPrefix {
n += collateLength(len(value), code[n:])
}
mkeys := config.mkeysh.getmkeys(len(value))
for _, key := range mkeys.sortProps1(value) {
n += collateString(key, code[n:], config) // encode key
n += gson2collate(value[key], code[n:], config) // encode value
}
code[n] = Terminator
n++
config.mkeysh.putmkeys(mkeys)
return n
}
panic(fmt.Errorf("collate invalid golang type %T", obj))
}
func collateLength(length int, code []byte) (n int) {
var num [64]byte
code[n] = TypeLength
n++
bs := strconv.AppendInt(num[:0], int64(length), 10)
n += collateInt(bs, code[n:])
code[n] = Terminator
n++
return n
}