forked from zubairhamed/betwixt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeval.go
435 lines (357 loc) · 8.13 KB
/
typeval.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
package betwixt
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"time"
)
type ValueTypeCode byte
const (
VALUETYPE_EMPTY ValueTypeCode = 0
VALUETYPE_MULTIPLE ValueTypeCode = 1
VALUETYPE_STRING ValueTypeCode = 2
VALUETYPE_BYTE ValueTypeCode = 3
VALUETYPE_INTEGER ValueTypeCode = 4
VALUETYPE_INTEGER32 ValueTypeCode = 5
VALUETYPE_INTEGER64 ValueTypeCode = 6
VALUETYPE_FLOAT ValueTypeCode = 7
VALUETYPE_FLOAT64 ValueTypeCode = 8
VALUETYPE_BOOLEAN ValueTypeCode = 9
VALUETYPE_OPAQUE ValueTypeCode = 10
VALUETYPE_TIME ValueTypeCode = 11
VALUETYPE_OBJECTLINK ValueTypeCode = 12
VALUETYPE_OBJECT ValueTypeCode = 13
VALUETYPE_RESOURCE ValueTypeCode = 14
VALUETYPE_MULTIRESOURCE ValueTypeCode = 15
)
// ResponseValue interface represents response to a server request
// Typical response could be plain text, TLV Binary, TLV JSON
type Value interface {
GetBytes() []byte
GetType() ValueTypeCode
GetContainedType() ValueTypeCode
GetValue() interface{}
GetStringValue() string
}
type MultipleValue struct {
values []Value
containedType ValueTypeCode
}
func (v *MultipleValue) GetContainedType() ValueTypeCode {
return v.containedType
}
func (v *MultipleValue) GetBytes() []byte {
return []byte("")
}
func (v *MultipleValue) GetType() ValueTypeCode {
return VALUETYPE_MULTIPLE
}
func (v *MultipleValue) GetValue() interface{} {
return v.values
}
func (v *MultipleValue) GetStringValue() string {
return ""
}
type StringValue struct {
value string
}
func (v *StringValue) GetBytes() []byte {
return []byte(v.value)
}
func (v *StringValue) GetType() ValueTypeCode {
return VALUETYPE_STRING
}
func (v *StringValue) GetContainedType() ValueTypeCode {
return VALUETYPE_STRING
}
func (v *StringValue) GetValue() interface{} {
return v.value
}
func (v *StringValue) GetStringValue() string {
return v.value
}
type IntegerValue struct {
value int
}
func (v *IntegerValue) GetBytes() []byte {
sz, _ := GetValueByteLength(v.value)
buf := new(bytes.Buffer)
if sz == 1 {
binary.Write(buf, binary.LittleEndian, int8(v.value))
} else if sz == 2 {
binary.Write(buf, binary.LittleEndian, int16(v.value))
} else if sz == 4 {
binary.Write(buf, binary.LittleEndian, int32(v.value))
} else if sz == 8 {
binary.Write(buf, binary.LittleEndian, int64(v.value))
}
return buf.Bytes()
}
func (v *IntegerValue) GetType() ValueTypeCode {
return VALUETYPE_INTEGER
}
func (v *IntegerValue) GetContainedType() ValueTypeCode {
return VALUETYPE_INTEGER
}
func (v *IntegerValue) GetValue() interface{} {
return v.value
}
func (v *IntegerValue) GetStringValue() string {
return strconv.Itoa(v.value)
}
type TimeValue struct {
value time.Time
}
func (v *TimeValue) GetBytes() []byte {
return []byte(strconv.FormatInt(v.value.Unix(), 10))
}
func (v *TimeValue) GetType() ValueTypeCode {
return VALUETYPE_TIME
}
func (v *TimeValue) GetContainedType() ValueTypeCode {
return VALUETYPE_TIME
}
func (v *TimeValue) GetValue() interface{} {
return v.value
}
func (v *TimeValue) GetStringValue() string {
return strconv.FormatInt(v.value.Unix(), 10)
}
type FloatValue struct {
value float32
}
func (v *FloatValue) GetBytes() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, v.value)
return buf.Bytes()
}
func (v *FloatValue) GetType() ValueTypeCode {
return VALUETYPE_FLOAT
}
func (v *FloatValue) GetContainedType() ValueTypeCode {
return VALUETYPE_FLOAT
}
func (v *FloatValue) GetValue() interface{} {
return v.value
}
func (v *FloatValue) GetStringValue() string {
return strconv.FormatFloat(float64(v.value), 'g', 1, 32)
}
// Float64
type Float64Value struct {
value float64
}
func (v *Float64Value) GetBytes() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, v.value)
return buf.Bytes()
}
func (v *Float64Value) GetType() ValueTypeCode {
return VALUETYPE_FLOAT64
}
func (v *Float64Value) GetContainedType() ValueTypeCode {
return VALUETYPE_FLOAT64
}
func (v *Float64Value) GetValue() interface{} {
return v.value
}
func (v *Float64Value) GetStringValue() string {
return strconv.FormatFloat(v.value, 'g', 1, 32)
}
// Boolean
type BooleanValue struct {
value bool
}
func (v *BooleanValue) GetBytes() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, v.value)
return buf.Bytes()
}
func (v *BooleanValue) GetType() ValueTypeCode {
return VALUETYPE_BOOLEAN
}
func (v *BooleanValue) GetContainedType() ValueTypeCode {
return VALUETYPE_BOOLEAN
}
func (v *BooleanValue) GetValue() interface{} {
return v.value
}
func (v *BooleanValue) GetStringValue() string {
if v.value {
return "1"
} else {
return "0"
}
}
func Empty() Value {
return &EmptyValue{}
}
type EmptyValue struct {
}
func (v *EmptyValue) GetBytes() []byte {
return []byte("")
}
func (v *EmptyValue) GetType() ValueTypeCode {
return VALUETYPE_EMPTY
}
func (v *EmptyValue) GetContainedType() ValueTypeCode {
return VALUETYPE_EMPTY
}
func (v *EmptyValue) GetValue() interface{} {
return ""
}
func (v *EmptyValue) GetStringValue() string {
return ""
}
func String(v ...string) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, String(o))
}
return Multiple(VALUETYPE_STRING, vs...)
} else {
return &StringValue{
value: v[0],
}
}
}
func Integer(v ...int) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, Integer(o))
}
return Multiple(VALUETYPE_INTEGER, vs...)
} else {
return &IntegerValue{
value: v[0],
}
}
}
func Time(v ...time.Time) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, Time(o))
}
return Multiple(VALUETYPE_TIME, vs...)
} else {
return &TimeValue{
value: v[0],
}
}
}
func Float(v ...float32) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, Float(o))
}
return Multiple(VALUETYPE_FLOAT, vs...)
} else {
return &FloatValue{
value: v[0],
}
}
}
func Float64(v ...float64) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, Float64(o))
}
return Multiple(VALUETYPE_FLOAT64, vs...)
} else {
return &Float64Value{
value: v[0],
}
}
}
func Boolean(v ...bool) Value {
if len(v) > 1 {
vs := []Value{}
for _, o := range v {
vs = append(vs, Boolean(o))
}
return Multiple(VALUETYPE_BOOLEAN, vs...)
} else {
return &BooleanValue{
value: v[0],
}
}
}
func Multiple(ct ValueTypeCode, v ...Value) Value {
return &MultipleValue{
values: v,
containedType: ct,
}
}
func MultipleIntegers(v ...Value) Value {
return &MultipleValue{
values: v,
containedType: VALUETYPE_INTEGER,
}
}
func ValueByType(t ValueTypeCode, val []byte) Value {
var value Value
switch t {
case VALUETYPE_STRING:
value = String(string(val))
break
}
return value
}
func GetValueByteLength(val interface{}) (uint32, error) {
if _, ok := val.(int); ok {
v := val.(int)
if v > 127 || v < -128 {
if v > 32767 || v < -32768 {
if v > 2147483647 || v < -2147483648 {
return 8, nil
} else {
return 4, nil
}
} else {
return 2, nil
}
} else {
return 1, nil
}
} else if _, ok := val.(bool); ok {
return 1, nil
} else if _, ok := val.(string); ok {
v := val.(string)
return uint32(len(v)), nil
} else if _, ok := val.(float64); ok {
v := val.(float64)
if v > +3.4E+38 || v < -3.4E+38 {
return 8, nil
} else {
return 4, nil
}
} else if _, ok := val.(time.Time); ok {
return 8, nil
} else if _, ok := val.([]byte); ok {
v := val.([]byte)
return uint32(len(v)), nil
} else {
return 0, errors.New("Unknown type")
}
}
func BytesToIntegerValue(b []byte) (conv Value) {
intLen := len(b)
if intLen == 1 {
conv = Integer(int(b[0]))
} else if intLen == 2 {
conv = Integer(int(b[1]) | (int(b[0]) << 8))
} else if intLen == 4 {
conv = Integer(int(b[3]) | (int(b[2]) << 8) | (int(b[1]) << 16) | (int(b[0]) << 24))
} else if intLen == 8 {
conv = Integer(int(b[7]) | (int(b[6]) << 8) | (int(b[5]) << 16) | (int(b[4]) << 24) | (int(b[3]) << 32) | (int(b[2]) << 40) | (int(b[1]) << 48) | (int(b[0]) << 56))
} else {
// Error
}
return
}