This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
marshal.go
485 lines (448 loc) · 10.5 KB
/
marshal.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Public Domain (-) 2012-2013 The Go DynamoDB Authors.
// See the Go DynamoDB UNLICENSE file for details.
package dynamodb
import (
"bytes"
"encoding/base64"
"fmt"
"reflect"
"strconv"
"sync"
"time"
"unicode"
"unicode/utf8"
)
const (
binaryField int = iota
binarySetField
boolField
boolSetField
intField
intSetField
int64Field
int64SetField
stringField
stringSetField
timeField
uintField
uintSetField
uint64Field
uint64SetField
)
var kindMap = [...]string{
binaryField: "B",
binarySetField: "BS",
boolField: "N",
boolSetField: "NS",
intField: "N",
intSetField: "NS",
int64Field: "N",
int64SetField: "NS",
stringField: "S",
stringSetField: "SS",
timeField: "N",
uintField: "N",
uintSetField: "NS",
uint64Field: "N",
uint64SetField: "NS",
}
var (
mutex sync.RWMutex
timeType = reflect.TypeOf(time.Time{})
typeInfo = map[reflect.Type][]*fieldInfo{}
)
type fieldInfo struct {
kind int
index int
name string
}
func encode(v interface{}, buf *bytes.Buffer) {
if item, ok := v.(Item); ok {
item.Encode(buf)
return
}
rv := reflect.ValueOf(v)
if !rv.IsValid() {
panic("cannot encode invalid value")
}
rt := rv.Type()
mutex.RLock()
fields, present := typeInfo[rt]
mutex.RUnlock()
if !present {
fields = compile(rt)
}
close := `{"`
last := len(fields) - 1
rv = rv.Elem()
written := false
for idx, field := range fields {
dbKind := kindMap[field.kind]
prefix := `"`
suffix := `"`
if len(dbKind) == 2 {
prefix = "["
suffix = "]"
}
fmt.Fprintf(buf, `%s%s":{"%s":%s`, close, field.name, dbKind, prefix)
comma := ","
if idx == last {
comma = ""
}
close = fmt.Sprintf(`%s}%s"`, suffix, comma)
written = true
fv := rv.Field(field.index)
switch field.kind {
case binaryField:
buf.WriteString(base64.StdEncoding.EncodeToString(fv.Interface().([]byte)))
case binarySetField:
elems := fv.Interface().([][]byte)
for j, elem := range elems {
buf.WriteByte('"')
buf.WriteString(base64.StdEncoding.EncodeToString(elem))
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case boolField:
if fv.Interface().(bool) {
buf.WriteByte('1')
} else {
buf.WriteByte('0')
}
case boolSetField:
elems := fv.Interface().([]bool)
for j, elem := range elems {
buf.WriteByte('"')
if elem {
buf.WriteByte('1')
} else {
buf.WriteByte('0')
}
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case stringField:
toJSON(fv.Interface().(string), buf)
case stringSetField:
elems := fv.Interface().([]string)
for j, elem := range elems {
buf.WriteByte('"')
toJSON(elem, buf)
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case intField:
buf.WriteString(strconv.FormatInt(int64(fv.Interface().(int)), 10))
case intSetField:
elems := fv.Interface().([]int)
for j, elem := range elems {
buf.WriteByte('"')
buf.WriteString(strconv.FormatInt(int64(elem), 10))
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case int64Field:
buf.WriteString(strconv.FormatInt(fv.Interface().(int64), 10))
case int64SetField:
elems := fv.Interface().([]int64)
for j, elem := range elems {
buf.WriteByte('"')
buf.WriteString(strconv.FormatInt(elem, 10))
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case uintField:
buf.WriteString(strconv.FormatUint(uint64(fv.Interface().(uint)), 10))
case uintSetField:
elems := fv.Interface().([]uint)
for j, elem := range elems {
buf.WriteByte('"')
buf.WriteString(strconv.FormatUint(uint64(elem), 10))
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case uint64Field:
buf.WriteString(strconv.FormatUint(fv.Interface().(uint64), 10))
case uint64SetField:
elems := fv.Interface().([]uint64)
for j, elem := range elems {
buf.WriteByte('"')
buf.WriteString(strconv.FormatUint(elem, 10))
if j == len(elems)-1 {
buf.WriteByte('"')
} else {
buf.WriteString(`",`)
}
}
case timeField:
buf.WriteString(strconv.FormatInt(fv.Interface().(time.Time).UnixNano(), 10))
}
}
if written {
fmt.Fprintf(buf, "%s}", close[:len(close)-1])
}
}
func decode(v interface{}, data map[string]map[string]interface{}) {
if item, ok := v.(Item); ok {
item.Decode(data)
return
}
rv := reflect.ValueOf(v)
if !rv.IsValid() {
panic("cannot decode into invalid value")
}
rt := rv.Type()
mutex.RLock()
fields, present := typeInfo[rt]
mutex.RUnlock()
if !present {
fields = compile(rt)
}
rv = rv.Elem()
for _, field := range fields {
switch field.kind {
case binaryField, boolField, intField, int64Field, stringField, timeField, uintField, uint64Field:
if val, ok := data[field.name][kindMap[field.kind]].(string); ok {
switch field.kind {
case binaryField:
tmp, _ := base64.StdEncoding.DecodeString(val)
rv.Field(field.index).SetBytes(tmp)
case boolField:
if val == "1" {
rv.Field(field.index).SetBool(true)
} else if val == "0" {
rv.Field(field.index).SetBool(false)
}
case stringField:
rv.Field(field.index).SetString(val)
case intField:
tmp, _ := strconv.ParseInt(val, 10, 64)
rv.Field(field.index).SetInt(tmp)
case int64Field:
tmp, _ := strconv.ParseInt(val, 10, 64)
rv.Field(field.index).SetInt(tmp)
case uintField:
tmp, _ := strconv.ParseUint(val, 10, 64)
rv.Field(field.index).SetUint(tmp)
case uint64Field:
tmp, _ := strconv.ParseUint(val, 10, 64)
rv.Field(field.index).SetUint(tmp)
case timeField:
tmp, _ := strconv.ParseInt(val, 10, 64)
bin, err := time.Unix(0, tmp).MarshalBinary()
if err == nil {
if tobj, ok := rv.Field(field.index).Interface().(time.Time); ok {
tobj.UnmarshalBinary(bin)
rv.Field(field.index).Set(reflect.ValueOf(tobj))
}
}
}
}
case binarySetField, boolSetField, intSetField, int64SetField, stringSetField, uintSetField, uint64SetField:
if svals, ok := data[field.name][kindMap[field.kind]].([]interface{}); ok {
vals := make([]string, len(svals))
for k, val := range svals {
vals[k] = val.(string)
}
switch field.kind {
case binarySetField:
nv := make([][]byte, len(vals))
for j, val := range vals {
tmp, _ := base64.StdEncoding.DecodeString(val)
nv[j] = tmp
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
case boolSetField:
nv := make([]bool, len(vals))
for j, val := range vals {
if val == "1" {
nv[j] = true
} else if val == "0" {
nv[j] = false
}
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
case stringSetField:
rv.Field(field.index).Set(reflect.ValueOf(vals))
case intSetField:
nv := make([]int, len(vals))
for j, val := range vals {
tmp, _ := strconv.ParseInt(val, 10, 64)
nv[j] = int(tmp)
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
case int64SetField:
nv := make([]int64, len(vals))
for j, val := range vals {
tmp, _ := strconv.ParseInt(val, 10, 64)
nv[j] = tmp
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
case uintSetField:
nv := make([]uint, len(vals))
for j, val := range vals {
tmp, _ := strconv.ParseUint(val, 10, 64)
nv[j] = uint(tmp)
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
case uint64SetField:
nv := make([]uint64, len(vals))
for j, val := range vals {
tmp, _ := strconv.ParseUint(val, 10, 64)
nv[j] = tmp
}
rv.Field(field.index).Set(reflect.ValueOf(nv))
}
}
}
}
}
func compile(it reflect.Type) []*fieldInfo {
if it.Kind() != reflect.Ptr {
panic("dynamodb: can only encode/decode pointers to struct types")
}
rt := it.Elem()
if rt.Kind() != reflect.Struct {
panic("dynamodb: can only encode/decode pointers to struct types")
}
fields := []*fieldInfo{}
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
if field.Anonymous {
continue
}
name := ""
if tag := field.Tag.Get("ddb"); tag != "" {
if tag == "-" {
continue
}
name = tag
}
if name == "" {
name = field.Name
rune, _ := utf8.DecodeRuneInString(name)
if !unicode.IsUpper(rune) {
continue
}
}
kind := -1
switch field.Type.Kind() {
case reflect.String:
kind = stringField
case reflect.Slice:
switch field.Type.Elem().Kind() {
case reflect.Uint8:
kind = binaryField
case reflect.String:
kind = stringSetField
case reflect.Int:
kind = intSetField
case reflect.Int64:
kind = int64SetField
case reflect.Slice:
if field.Type.Elem().Elem().Kind() == reflect.Uint8 {
kind = binarySetField
}
case reflect.Uint:
kind = uintSetField
case reflect.Uint64:
kind = uint64SetField
case reflect.Bool:
kind = boolSetField
}
case reflect.Int:
kind = intField
case reflect.Int64:
kind = int64Field
case reflect.Struct:
if field.Type == timeType {
kind = timeField
}
case reflect.Uint:
kind = uintField
case reflect.Uint64:
kind = uint64Field
case reflect.Bool:
kind = boolField
}
if kind == -1 {
panic("dynamodb: unsupported field type: " + field.Type.Elem().Kind().String())
}
fields = append(fields, &fieldInfo{
kind: kind,
index: i,
name: name,
})
}
mutex.Lock()
typeInfo[it] = fields
mutex.Unlock()
return fields
}
// Adapted from the encoding/json package in the standard
// library.
const hexstr = "0123456789abcdef"
func toJSON(s string, buf *bytes.Buffer) {
start := 0
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
i++
continue
}
if start < i {
buf.WriteString(s[start:i])
}
switch b {
case '\\', '"':
buf.WriteByte('\\')
buf.WriteByte(b)
case '\n':
buf.WriteByte('\\')
buf.WriteByte('n')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('r')
default:
buf.WriteString(`\u00`)
buf.WriteByte(hexstr[b>>4])
buf.WriteByte(hexstr[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
buf.WriteString(s[start:i])
}
buf.WriteString(`\ufffd`)
i += size
start = i
continue
}
i += size
}
if start < len(s) {
buf.WriteString(s[start:])
}
}