-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoder.go
138 lines (113 loc) · 2.63 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
133
134
135
136
137
138
package enkodo
import "io"
func newEncoder(w io.Writer) *Encoder {
var e Encoder
e.w = w
return &e
}
// Encoder helps to Marshal data
type Encoder struct {
bs []byte
w io.Writer
// Total written bytes
written int64
}
func (e *Encoder) flush() (err error) {
if e.w == nil {
return
}
var n int
if n, err = e.w.Write(e.bs); err == nil {
e.written += int64(n)
}
e.bs = e.bs[:0]
return
}
// Encode will encode an encodee
func (e *Encoder) teardown() {
e.bs = nil
e.w = nil
}
// Uint encodes a uint type
func (e *Encoder) Uint(v uint) (err error) {
e.bs = encodeUint(e.bs, v)
return e.flush()
}
// Uint8 encodes a uint8 type
func (e *Encoder) Uint8(v uint8) (err error) {
e.bs = encodeUint8(e.bs, v)
return e.flush()
}
// Uint16 encodes a uint16 type
func (e *Encoder) Uint16(v uint16) (err error) {
e.bs = encodeUint16(e.bs, v)
return e.flush()
}
// Uint32 encodes a uint32 type
func (e *Encoder) Uint32(v uint32) (err error) {
e.bs = encodeUint32(e.bs, v)
return e.flush()
}
// Uint64 encodes a uint64 type
func (e *Encoder) Uint64(v uint64) (err error) {
e.bs = encodeUint64(e.bs, v)
return e.flush()
}
// Int encodes an int type
func (e *Encoder) Int(v int) (err error) {
e.bs = encodeInt(e.bs, v)
return e.flush()
}
// Int8 encodes an int8 type
func (e *Encoder) Int8(v int8) (err error) {
e.bs = encodeInt8(e.bs, v)
return e.flush()
}
// Int16 encodes an int16 type
func (e *Encoder) Int16(v int16) (err error) {
e.bs = encodeInt16(e.bs, v)
return e.flush()
}
// Int32 encodes an int32 type
func (e *Encoder) Int32(v int32) (err error) {
e.bs = encodeInt32(e.bs, v)
return e.flush()
}
// Int64 encodes an int64 type
func (e *Encoder) Int64(v int64) (err error) {
e.bs = encodeInt64(e.bs, v)
return e.flush()
}
// Float32 encodes an float32 type
func (e *Encoder) Float32(v float32) (err error) {
e.bs = encodeFloat32(e.bs, v)
return e.flush()
}
// Float64 encodes an float64 type
func (e *Encoder) Float64(v float64) (err error) {
e.bs = encodeFloat64(e.bs, v)
return e.flush()
}
// Bytes will encode a byteslice to the writer
func (e *Encoder) Bytes(v []byte) (err error) {
e.bs = encodeBytes(e.bs, v)
return e.flush()
}
// String will encode a string to the writer
func (e *Encoder) String(v string) (err error) {
e.bs = encodeString(e.bs, v)
return e.flush()
}
// Bool will encode a boolean value to the writer
func (e *Encoder) Bool(v bool) (err error) {
e.bs = encodeBool(e.bs, v)
return e.flush()
}
// Encode will encode an encodee
func (e *Encoder) Encode(v Encodee) (err error) {
return v.MarshalEnkodo(e)
}
// Encodee is a data structure to be encoded
type Encodee interface {
MarshalEnkodo(*Encoder) error
}