-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.go
236 lines (220 loc) · 5.51 KB
/
size.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
package tt
import (
"reflect"
"runtime"
v2 "github.com/jaicewizard/tt/v2"
)
//TODO: maybe dont copy all the data over etc, it might be faster do just return the length and use that
//Size gives an accurate size value for the final size of buffer needed by encoding
func (d Data) Size() (int, error) {
var size = 1 //the version ID is included
var tv = v2.Ikeytype(0)
firstChilds, err := sizeMapv2(&size, d, &tv)
if err != nil {
return 0, err
}
//see encodev1 for reference of why this is needed
size += (&v2.Value{
Children: firstChilds,
Vtype: v2.FinalValueT,
}).Len()
size += 4
return size, nil
}
func sizeMapv2(size *int, d Data, nextValue *v2.Ikeytype) ([]v2.Ikeytype, error) {
createdObjects := make([]v2.Ikeytype, len(d))
i := 0
for k := range d {
err := sizeValuev2(size, d[k], k, nextValue)
if err != nil {
return nil, err
}
createdObjects[i] = *nextValue
i++
*nextValue++
}
return createdObjects, nil
}
func sizeValuev2(size *int, 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.Uint64T
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 := sizeValuev2(size, 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 {
sizeValuev2(size, v[k], k, nextValue)
value.Children[i] = *nextValue
i++
*nextValue++
}
value.Vtype = v2.MapT
case map[interface{}]interface{}:
childs, err := sizeMapv2(size, Data(v), nextValue)
if err != nil {
return err
}
value.Children = childs
value.Vtype = v2.MapT
case Data:
childs, err := sizeMapv2(size, 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 := sizeValuev2(size, 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
}
}
*size += value.Len()
runtime.KeepAlive(KeepAlive)
runtime.KeepAlive(KeepAlive1)
return nil
}