-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
244 lines (227 loc) · 5.59 KB
/
encode.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
package tt
import (
"bytes"
"encoding/binary"
"reflect"
"runtime"
v2 "github.com/jaicewizard/tt/v2"
)
const (
version1 = 1
version2 = 2
version3 = 3
)
//Encodev2 encodes a tt.Data into a bytebuffer using ttv2
func Encodev2(d Data, values *bytes.Buffer) {
values.WriteByte(version2)
tv := v2.Ikeytype(0)
firstChilds, err := encodemapv1(values, d, &tv)
if err != nil {
panic(err)
}
v2.AddValue(values, &v2.Value{
Children: firstChilds,
Vtype: v2.FinalValueT,
})
//this is bigEndian because then it is compatible with version one of tt
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(tv+1))
values.Write(buf[:])
}
func encodemapv1(values *bytes.Buffer, d Data, nextValue *v2.Ikeytype) ([]v2.Ikeytype, error) {
createdObjects := make([]v2.Ikeytype, len(d))
i := 0
for k := range d {
err := encodevaluev1(values, d[k], k, nextValue)
if err != nil {
return nil, err
}
createdObjects[i] = *nextValue
i++
*nextValue++
}
return createdObjects, nil
}
func encodevaluev1(values *bytes.Buffer, d interface{}, k interface{}, nextValue *v2.Ikeytype) error {
var value v2.Value
var KeepAlive interface{}
var KeepAlive1 interface{}
if k != nil {
switch v := k.(type) { //making this s seperate function will decrese performance, it won't be able to inline and make more allocations
case string:
value.Key.Value = v2.StringToBytes(v)
value.Key.Vtype = v2.StringT
case []byte:
value.Key.Value = v
value.Key.Vtype = v2.BytesT
case float64:
v2.Float64ToBytes(&v, &value.Key.Value)
value.Key.Vtype = v2.Float64T
KeepAlive = &v
case float32:
v2.Float32ToBytes3(&v, &value.Key.Value)
value.Key.Vtype = v2.Float32T
KeepAlive = &v
case int64:
var buf [8]byte
v2.Int64ToBytes(v, &buf)
value.Key.Value = buf[:]
value.Key.Vtype = v2.Int64T
case int32:
value.Key.Value = v2.Int32ToBytes(v)
value.Key.Vtype = v2.Int32T
case int16:
value.Key.Value = v2.Int16ToBytes(v)
value.Key.Vtype = v2.Int16T
case int8:
v2.Int8ToBytes(&v, &value.Key.Value)
value.Key.Vtype = v2.Int8T
KeepAlive = &v
case uint64:
value.Key.Value = v2.Uint64ToBytes(v)
value.Key.Vtype = v2.Uint64T
case uint32:
value.Key.Value = v2.Uint32ToBytes(v)
value.Key.Vtype = v2.Uint32T
case uint16:
value.Key.Value = v2.Uint16ToBytes(v)
value.Key.Vtype = v2.Uint16T
case uint8:
v2.Uint8ToBytes(&v, &value.Key.Value)
value.Key.Vtype = v2.Uint8T
KeepAlive = &v
case int:
var buf [8]byte
v2.Int64ToBytes(int64(v), &buf)
value.Key.Value = buf[:]
value.Key.Vtype = v2.Int64T
case uint:
value.Key.Value = v2.Uint64ToBytes(uint64(v))
value.Key.Vtype = v2.BoolT
KeepAlive = &v
case bool:
v2.BoolToBytes(&v, &value.Key.Value)
value.Key.Vtype = v2.BoolT
KeepAlive = &v
}
}
//this sets value.Value, it does al the basic types and some more
switch v := d.(type) {
case string:
value.Value = v2.StringToBytes(v)
value.Vtype = v2.StringT
case []byte:
value.Value = v
value.Vtype = v2.BytesT
case float64:
v2.Float64ToBytes(&v, &value.Value)
value.Vtype = v2.Float64T
KeepAlive1 = &v
case float32:
v2.Float32ToBytes3(&v, &value.Value)
value.Vtype = v2.Float32T
KeepAlive1 = &v
case int64:
var buf [8]byte
v2.Int64ToBytes(v, &buf)
value.Value = buf[:]
value.Vtype = v2.Int64T
case int32:
value.Value = v2.Int32ToBytes(v)
value.Vtype = v2.Int32T
case int16:
value.Value = v2.Int16ToBytes(v)
value.Vtype = v2.Int16T
case int8:
v2.Int8ToBytes(&v, &value.Value)
value.Vtype = v2.Int8T
KeepAlive1 = &v
case uint64:
value.Value = v2.Uint64ToBytes(v)
value.Vtype = v2.Uint64T
case uint32:
value.Value = v2.Uint32ToBytes(v)
value.Vtype = v2.Uint32T
case uint16:
value.Value = v2.Uint16ToBytes(v)
value.Vtype = v2.Uint16T
case uint8:
v2.Uint8ToBytes(&v, &value.Value)
value.Vtype = v2.Uint8T
KeepAlive1 = &v
case int:
var buf [8]byte
v2.Int64ToBytes(int64(v), &buf)
value.Value = buf[:]
value.Vtype = v2.Int64T
case uint:
value.Value = v2.Uint64ToBytes(uint64(v))
value.Vtype = v2.Uint64T
KeepAlive1 = &v
case bool:
v2.BoolToBytes(&v, &value.Value)
value.Vtype = v2.BoolT
case []interface{}:
value.Children = make([]v2.Ikeytype, len(v))
for i := 0; i < len(v); i++ {
err := encodevaluev1(values, v[i], nil, nextValue)
if err != nil {
return err
}
value.Children[i] = *nextValue
*nextValue++
}
value.Vtype = v2.ArrT
case map[string]interface{}:
value.Children = make([]v2.Ikeytype, len(v))
i := 0
for k := range v {
encodevaluev1(values, v[k], k, nextValue)
value.Children[i] = *nextValue
i++
*nextValue++
}
value.Vtype = v2.MapT
case map[interface{}]interface{}:
childs, err := encodemapv1(values, Data(v), nextValue)
if err != nil {
return err
}
value.Children = childs
value.Vtype = v2.MapT
case Data:
childs, err := encodemapv1(values, v, nextValue)
if err != nil {
return err
}
value.Children = childs
value.Vtype = v2.MapT
default:
if val := reflect.ValueOf(d); val.Kind() == reflect.Array {
value.Children = make([]v2.Ikeytype, val.Len())
for i := 0; i < val.Len(); i++ {
e := val.Index(i).Interface()
err := encodevaluev1(values, e, nil, nextValue)
if err != nil {
return err
}
value.Children[i] = *nextValue
*nextValue++
}
value.Vtype = v2.ArrT
} else if v, ok := d.(Transmitter); ok {
var err error
value.Value, err = v.Encode()
if err != nil {
return err
}
value.Vtype = v.GetCode()
} else {
return v2.ErrInvalidInput
}
}
v2.AddValue(values, &value)
runtime.KeepAlive(KeepAlive)
runtime.KeepAlive(KeepAlive1)
return nil
}