-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
132 lines (110 loc) · 2.79 KB
/
encoder.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
package bx
import (
"bytes"
"fmt"
"time"
)
// E is the buffer to encode collected values to one byte slice
type E struct {
values []ValueType
}
// Encoder creates a new encoder
func Encoder() *E {
e := E{
values: []ValueType{},
}
return &e
}
// Write writes any value which implements ValueType to the encoder
func (e *E) Write(vt ValueType) *E {
if e.values == nil {
e.values = []ValueType{}
}
e.values = append(e.values, vt)
return e
}
// WriteSlice writes any slice of value which implements ValueType
func (e *E) WriteSlice(vt []ValueType) *E {
if e.values == nil {
e.values = []ValueType{}
}
e.Int64(int64(len(vt)))
e.values = append(e.values, vt...)
return e
}
// Int8 writes a int8 value to the encoder
func (e *E) Int8(val int8) *E {
return e.Write(&Number{value: val})
}
// Int16 writes a int16 value to the encoder
func (e *E) Int16(val int16) *E {
return e.Write(&Number{value: val})
}
// Int32 writes a int32 value to the encoder
func (e *E) Int32(val int32) *E {
return e.Write(&Number{value: val})
}
// Int64 writes a int64 value to the encoder
func (e *E) Int64(val int64) *E {
return e.Write(&Number{value: val})
}
// Uint8 writes a uint8 value to the encoder
func (e *E) Uint8(val uint8) *E {
return e.Write(&Number{value: val})
}
// Uint16 writes a uint16 value to the encoder
func (e *E) Uint16(val uint16) *E {
return e.Write(&Number{value: val})
}
// Uint32 writes a uint32 value to the encoder
func (e *E) Uint32(val uint32) *E {
return e.Write(&Number{value: val})
}
// Uint64 writes a uint64 value to the encoder
func (e *E) Uint64(val uint64) *E {
return e.Write(&Number{value: val})
}
// Float32 writes a float32 value to the encoder
func (e *E) Float32(val float32) *E {
return e.Write(&Number{value: val})
}
// Float64 writes a float64 value to the encoder
func (e *E) Float64(val float64) *E {
return e.Write(&Number{value: val})
}
// String writes a string value to the encoder
func (e *E) String(val string) *E {
return e.Write(&Bytes{value: []byte(val)})
}
// Bytes writes a byte slice value to the encoder
func (e *E) Bytes(val []byte) *E {
return e.Write(&Bytes{value: val})
}
// Strings writes a string slice value to the encoder
func (e *E) Strings(values []string) *E {
n := len(values)
e.Uint64(uint64(n))
for _, val := range values {
e.String(val)
}
return e
}
// Time writes a time value to the encoder
func (e *E) Time(val time.Time) *E {
return e.Int64(val.UnixNano())
}
// Encode encodes all written values
func (e *E) Encode() ([]byte, error) {
buff := bytes.Buffer{}
for _, vt := range e.values {
raw, err := vt.Encode()
if err != nil {
return nil, fmt.Errorf("can encode value type: %w", err)
}
_, err = buff.Write(raw)
if err != nil {
return nil, fmt.Errorf("can't write to buffer: %w", err)
}
}
return buff.Bytes(), nil
}