-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.go
181 lines (166 loc) · 3.98 KB
/
v2.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
package tt
import (
"encoding/binary"
"errors"
v2 "github.com/jaicewizard/tt/v2"
)
//Decodev2 decodes a ttv2 encoded byte-slice into tt.Data
func Decodev2(b []byte, d *Data) (err error) {
vlen := binary.BigEndian.Uint32(b[len(b)-4:])
locs := make([]uint64, vlen)
v2.GetLocs(b, locs, vlen)
//decoding the actual values
var v v2.Value
v.FromBytes(b[locs[vlen-1]:])
if *d == nil {
*d = make(Data, len(v.Children)*1)
}
data := d
childs := v.Children
for ck := range childs {
var err error
v.FromBytes(b[locs[childs[ck]]:])
err = valueToMapv2(&v, *data, locs, b)
if err != nil {
return err
}
}
return nil
}
func valueToMapv2(v *v2.Value, dat Data, locs []uint64, buf []byte) (err error) {
key := v.Key.Export()
if key == nil {
return v2.ErrInvalidInput
}
switch v.Vtype { //!make sure to update types in interfaceFromValuev1 as well!
//standard types
case v2.StringT:
dat[key] = v2.StringFromBytes(v.Value)
case v2.BytesT:
dat[key] = v.Value
case v2.Float64T:
dat[key] = v2.Float64FromBytes(v.Value)
case v2.Float32T:
dat[key] = v2.Float32FromBytes(v.Value)
case v2.Int64T:
dat[key] = v2.Int64FromBytes(v.Value)
case v2.Int32T:
dat[key] = v2.Int32FromBytes(v.Value)
case v2.Int16T:
dat[key] = v2.Int16FromBytes(v.Value)
case v2.Int8T:
dat[key] = v2.Int8FromBytes(v.Value)
case v2.Uint64T:
dat[key] = v2.Uint64FromBytes(v.Value)
case v2.Uint32T:
dat[key] = v2.Uint32FromBytes(v.Value)
case v2.Uint16T:
dat[key] = v2.Uint16FromBytes(v.Value)
case v2.Uint8T:
dat[key] = v2.Uint8FromBytes(v.Value[0])
case v2.BoolT:
dat[key] = v2.BoolFromBytes(v.Value)
// special types
case v2.MapT:
val := make(Data, len(v.Children))
childs := v.Children
for ck := range childs {
var err error
v.FromBytes(buf[locs[childs[ck]]:])
err = valueToMapv2(v, val, locs, buf)
if err != nil {
return err
}
}
dat[key] = val
case v2.ArrT:
val := make([]interface{}, len(v.Children))
childs := v.Children
for i := range childs {
var err error
v.FromBytes(buf[locs[childs[i]]:])
err = interfaceFromValuev2(v, &val[i], locs, buf)
if err != nil {
return err
}
}
dat[key] = val
default:
t := transmitters[v.Vtype]
if t == nil {
return errors.New("no transmitter for type:" + string(v.Vtype))
}
var err error
dat[key], err = t.Decode(v.Value)
if err != nil {
return err
}
}
return err
}
//interfaceFromValuev1 converts a value into an interface{}, you should pass &interface{}
func interfaceFromValuev2(v *v2.Value, ret *interface{}, locs []uint64, buf []byte) error {
switch v.Vtype {
//standard types
case v2.StringT:
*ret = v2.StringFromBytes(v.Value)
case v2.BytesT:
*ret = v.Value
case v2.Float64T:
*ret = v2.Float64FromBytes(v.Value)
case v2.Float32T:
*ret = v2.Float32FromBytes(v.Value)
case v2.Int64T:
*ret = v2.Int64FromBytes(v.Value)
case v2.Int32T:
*ret = v2.Int32FromBytes(v.Value)
case v2.Int16T:
*ret = v2.Int16FromBytes(v.Value)
case v2.Int8T:
*ret = v2.Int8FromBytes(v.Value)
case v2.Uint64T:
*ret = v2.Uint64FromBytes(v.Value)
case v2.Uint32T:
*ret = v2.Uint32FromBytes(v.Value)
case v2.Uint16T:
*ret = v2.Uint16FromBytes(v.Value)
case v2.Uint8T:
*ret = v2.Uint8FromBytes(v.Value[0])
case v2.BoolT:
*ret = v2.BoolFromBytes(v.Value)
// special types
case v2.MapT:
val := make(Data, len(v.Children))
childs := v.Children
for ck := range childs {
var err error
v.FromBytes(buf[locs[childs[ck]]:])
err = valueToMapv2(v, val, locs, buf)
if err != nil {
return err
}
}
*ret = val
case v2.ArrT:
val := make([]interface{}, len(v.Children))
childs := v.Children
for i := range childs {
var err error
v.FromBytes(buf[locs[childs[i]]:])
err = interfaceFromValuev2(v, &val[i], locs, buf)
if err != nil {
return err
}
}
*ret = val
default:
t := transmitters[v.Vtype]
if t == nil {
return errors.New("no transmitter for type:" + string(v.Vtype))
}
var err error
*ret, err = t.Decode(v.Value)
return err
}
return nil
}