This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder.go
179 lines (156 loc) · 3.37 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
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
// Copyright 2019 The go-daq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tdaq // import "github.com/go-daq/tdaq"
import (
"encoding/binary"
"fmt"
"io"
"math"
)
// Encoder encodes values to the underlying io.Writer, according to the TDAQ wire protocol.
type Encoder struct {
w io.Writer
err error
buf []byte
}
// NewEncoder creates a new encoder, connected to the provided io.Writer.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w, buf: make([]byte, 8)}
}
func (enc *Encoder) Err() error { return enc.err }
// Encode encodes the provided value to the underlying io.Writer.
func (enc *Encoder) Encode(v interface{}) error {
if enc.err != nil {
return enc.err
}
if v, ok := v.(Marshaler); ok {
raw, err := v.MarshalTDAQ()
if err != nil {
enc.err = err
return err
}
enc.WriteU64(uint64(len(raw)))
_, enc.err = enc.w.Write(raw)
return enc.err
}
switch v := v.(type) {
case bool:
enc.WriteBool(v)
case int8:
enc.WriteI8(v)
case int16:
enc.WriteI16(v)
case int32:
enc.WriteI32(v)
case int64:
enc.WriteI64(v)
case uint8:
enc.WriteU8(v)
case uint16:
enc.WriteU16(v)
case uint32:
enc.WriteU32(v)
case uint64:
enc.WriteU64(v)
case float32:
enc.WriteF32(v)
case float64:
enc.WriteF64(v)
case string:
enc.WriteStr(v)
default:
return fmt.Errorf("value type=%T not supported", v)
}
return enc.err
}
func (enc *Encoder) WriteBool(v bool) {
if enc.err != nil {
return
}
switch v {
case true:
enc.buf[0] = 1
default:
enc.buf[0] = 0
}
_, enc.err = enc.w.Write(enc.buf[:1])
}
func (enc *Encoder) WriteI8(v int8) {
if enc.err != nil {
return
}
enc.buf[0] = uint8(v)
_, enc.err = enc.w.Write(enc.buf[:1])
}
func (enc *Encoder) WriteI16(v int16) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint16(enc.buf[:2], uint16(v))
_, enc.err = enc.w.Write(enc.buf[:2])
}
func (enc *Encoder) WriteI32(v int32) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], uint32(v))
_, enc.err = enc.w.Write(enc.buf[:4])
}
func (enc *Encoder) WriteI64(v int64) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], uint64(v))
_, enc.err = enc.w.Write(enc.buf[:8])
}
func (enc *Encoder) WriteU8(v uint8) {
if enc.err != nil {
return
}
enc.buf[0] = v
_, enc.err = enc.w.Write(enc.buf[:1])
}
func (enc *Encoder) WriteU16(v uint16) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint16(enc.buf[:2], v)
_, enc.err = enc.w.Write(enc.buf[:2])
}
func (enc *Encoder) WriteU32(v uint32) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], v)
_, enc.err = enc.w.Write(enc.buf[:4])
}
func (enc *Encoder) WriteU64(v uint64) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], v)
_, enc.err = enc.w.Write(enc.buf[:8])
}
func (enc *Encoder) WriteF32(v float32) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], math.Float32bits(v))
_, enc.err = enc.w.Write(enc.buf[:4])
}
func (enc *Encoder) WriteF64(v float64) {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint64(enc.buf[:8], math.Float64bits(v))
_, enc.err = enc.w.Write(enc.buf[:8])
}
func (enc *Encoder) WriteStr(v string) {
n := int32(len(v))
enc.WriteI32(n)
if enc.err != nil {
return
}
_, enc.err = enc.w.Write([]byte(v[:n]))
}