forked from dugancathal/dynago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire.go
224 lines (202 loc) · 4.38 KB
/
wire.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
package dynago
import (
"encoding/base64"
"strconv"
"time"
)
func wireEncode(value interface{}) interface{} {
// This is somewhat optimized based on what we expect are the most common types.
switch v := value.(type) {
case string:
return &wireString{v}
case int:
return &wireNumber{strconv.Itoa(v)}
case int64:
return &wireNumber{strconv.FormatInt(v, 10)}
case bool:
return &wireBool{v}
case float64:
return &wireNumber{strconv.FormatFloat(v, 'g', -1, 64)}
case Number:
return &wireNumber{string(v)}
case Document:
output := make(map[string]interface{}, len(v))
for key, val := range v {
output[key] = wireEncode(val)
}
return &wireMap{output}
case []byte:
return &wireBinary{v}
case StringSet:
return &wireStringSet{v}
case NumberSet:
return &wireNumberSet{v}
case BinarySet:
return &wireBinarySet{v}
case List:
encList := make([]interface{}, len(v))
for i, raw := range v {
encList[i] = wireEncode(raw)
}
return &wireList{encList}
case map[string]interface{}:
return wireEncode(Document(v))
case int32, int16, int8:
return &wireNumber{strconv.FormatInt(anyInt(v), 10)}
case uint, uint64, uint32, uint16, uint8:
return &wireNumber{strconv.FormatUint(anyUint(v), 10)}
case nil:
return &wireNull{true}
case time.Time:
return wireEncodeTime(v)
case *time.Time:
return wireEncodeTime(*v)
default:
panic(v)
}
}
func wireEncodeTime(t time.Time) interface{} {
if t.Location() != time.UTC {
panic("Times must be provided as UTC")
}
return &wireString{t.Format(iso8601compact)}
}
type wireString struct {
S string
}
type wireStringSet struct {
SS []string
}
type wireNumber struct {
N string
}
type wireNumberSet struct {
NS []string
}
type wireBool struct {
BOOL bool `json:"BOOL"`
}
// Bonus! we don't have to do anything at all for binary. base64 is done by encoding/json.
type wireBinary struct {
B []byte
}
type wireBinarySet struct {
BS [][]byte
}
type wireList struct {
L []interface{}
}
type wireMap struct {
M map[string]interface{}
}
type wireNull struct {
NULL bool
}
func wireDecode(original interface{}) interface{} {
vv, ok := original.(map[string]interface{})
if !ok {
panic(original) // XXX DEBUG TODO
}
for typeCode, val := range vv {
// TODO decode all the various types coming back.
switch typeCode {
case "S", "BOOL":
return val
case "NULL":
return nil
case "N":
return Number(val.(string))
case "NS":
return wireDecodeNumberSet(val)
case "SS":
return wireDecodeStringSet(val)
case "L":
return wireDecodeList(val)
case "M":
return wireDecodeMap(val)
case "B":
return wireDecodeBinary(val)
case "BS":
return wireDecodeBinarySet(val)
}
}
return nil
}
func wireDecodeNumberSet(val interface{}) interface{} {
valSlice := val.([]interface{})
resultSlice := make(NumberSet, len(valSlice))
for i, v := range valSlice {
resultSlice[i] = v.(string)
}
return resultSlice
}
func wireDecodeStringSet(val interface{}) interface{} {
valSlice := val.([]interface{})
resultSlice := make(StringSet, len(valSlice))
for i, v := range valSlice {
resultSlice[i] = v.(string)
}
return resultSlice
}
func wireDecodeList(val interface{}) interface{} {
valSlice := val.([]interface{})
resultSlice := make(List, len(valSlice))
for i, v := range valSlice {
resultSlice[i] = wireDecode(v)
}
return resultSlice
}
func wireDecodeMap(val interface{}) interface{} {
m := val.(map[string]interface{})
output := make(Document, len(m))
for key, val := range m {
output[key] = wireDecode(val)
}
return output
}
func wireDecodeBinary(val interface{}) []byte {
s := val.(string)
buf, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return buf
}
func wireDecodeBinarySet(val interface{}) interface{} {
valSlice := val.([]interface{})
resultSlice := make(BinarySet, len(valSlice))
for i, v := range valSlice {
resultSlice[i] = wireDecodeBinary(v)
}
return resultSlice
}
func anyInt(input interface{}) int64 {
switch v := input.(type) {
case int:
return int64(v)
case int64:
return v
case int32:
return int64(v)
case int16:
return int64(v)
case int8:
return int64(v)
}
panic("Unknown int type")
}
func anyUint(input interface{}) uint64 {
switch v := input.(type) {
case uint:
return uint64(v)
case uint64:
return v
case uint32:
return uint64(v)
case uint16:
return uint64(v)
case uint8:
return uint64(v)
}
panic("unknown uint type")
}