|
1
|
|
|
package decode |
|
2
|
|
|
|
|
3
|
|
|
import "io" |
|
4
|
|
|
|
|
5
|
|
|
func decodeMap(reader io.Reader, v interface{}) error { |
|
6
|
1 |
|
sz, rSize, err := readSize(reader) |
|
7
|
1 |
|
if err != nil { |
|
8
|
|
|
return err |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
1 |
|
cnt, rCount, err := readSize(reader) |
|
12
|
1 |
|
if err != nil { |
|
13
|
|
|
return err |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
1 |
|
return decodeMapItems(reader, v, sz, rSize+rCount, cnt) |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
func decodeMapItems(reader io.Reader, v interface{}, size int, wasRead readLen, items int) error { |
|
20
|
1 |
|
readItems := 0 |
|
21
|
1 |
|
readPosition := wasRead |
|
22
|
|
|
|
|
23
|
1 |
|
for readItems < items && readPosition < readLen(size) { |
|
24
|
1 |
|
key, read, err := readMapKey(reader) |
|
25
|
1 |
|
if err != nil { |
|
26
|
|
|
return err |
|
27
|
|
|
} |
|
28
|
1 |
|
readPosition += read |
|
29
|
|
|
|
|
30
|
1 |
|
t, read, err := readType(reader) |
|
31
|
1 |
|
if err != nil { |
|
32
|
|
|
return err |
|
33
|
|
|
} |
|
34
|
1 |
|
readPosition += read |
|
35
|
|
|
|
|
36
|
1 |
|
val, err := readValue(t, reader) |
|
37
|
1 |
|
if err != nil { |
|
38
|
|
|
return err |
|
39
|
|
|
} |
|
40
|
1 |
|
readPosition += readLen(len(val)) |
|
41
|
|
|
|
|
42
|
1 |
|
err = addMapItem(key, t, val, v) |
|
43
|
1 |
|
if err != nil { |
|
44
|
|
|
return err |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
readItems++ |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return nil |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
func readMapKey(reader io.Reader) (int, readLen, error) { |
|
54
|
1 |
|
var bk = make([]byte, 4) |
|
55
|
1 |
|
n, err := reader.Read(bk) |
|
56
|
1 |
|
if err != nil { |
|
57
|
|
|
return 0, 0, err |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
if n != 4 { |
|
61
|
|
|
return 0, readLen(n), ErrIncompleteRead |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return int(Int32(bk)), 4, nil |
|
65
|
|
|
} |
|
66
|
|
|
|