-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
262 lines (241 loc) · 5.77 KB
/
types.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
package y3
import (
"fmt"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
// ToObject decode bytes to interface
func ToObject(v []byte, output interface{}) error {
_, err := newStructDecoder(output).Decode(v) // nolint
return err
}
// ToInt32 decode bytes to int32
func ToInt32(v []byte) (int32, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToInt32()
if err != nil {
return 0, nil
}
return value, nil
}
// ToInt32Slice decode bytes to []int32
func ToInt32Slice(v []byte) ([]int32, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]int32, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToInt32()
result = append(result, v)
}
return result, nil
}
// ToUInt32 decode bytes to uint32
func ToUInt32(v []byte) (uint32, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToUInt32()
if err != nil {
return 0, nil
}
return value, nil
}
// ToUInt32Slice decode bytes to []uint32
func ToUInt32Slice(v []byte) ([]uint32, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]uint32, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToUInt32()
result = append(result, v)
}
return result, nil
}
// ToInt64 decode bytes to int64
func ToInt64(v []byte) (int64, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToInt64()
if err != nil {
return 0, nil
}
return value, nil
}
// ToInt64Slice decode bytes to []int64
func ToInt64Slice(v []byte) ([]int64, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]int64, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToInt64()
result = append(result, v)
}
return result, nil
}
// ToUInt64 decode bytes to uint64
func ToUInt64(v []byte) (uint64, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToUInt64()
if err != nil {
return 0, nil
}
return value, nil
}
// ToUInt64Slice decode bytes to []uint64
func ToUInt64Slice(v []byte) ([]uint64, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]uint64, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToUInt64()
result = append(result, v)
}
return result, nil
}
// ToFloat32 decode bytes to float32
func ToFloat32(v []byte) (float32, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToFloat32()
if err != nil {
return 0, nil
}
return value, nil
}
// ToFloat32Slice decode bytes to []float32
func ToFloat32Slice(v []byte) ([]float32, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]float32, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToFloat32()
result = append(result, v)
}
return result, nil
}
// ToFloat64 decode bytes to float64
func ToFloat64(v []byte) (float64, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return 0, err
}
value, err := primitivePacket.ToFloat64()
if err != nil {
return 0, nil
}
return value, nil
}
// ToFloat64Slice decode bytes to []float64
func ToFloat64Slice(v []byte) ([]float64, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]float64, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToFloat64()
result = append(result, v)
}
return result, nil
}
// ToBool decode bytes to bool
func ToBool(v []byte) (bool, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return false, err
}
value, err := primitivePacket.ToBool()
if err != nil {
return false, nil
}
return value, nil
}
// ToBoolSlice decode bytes to []bool
func ToBoolSlice(v []byte) ([]bool, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]bool, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToBool()
result = append(result, v)
}
return result, nil
}
// ToUTF8String decode bytes to string
func ToUTF8String(v []byte) (string, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return "", err
}
value, err := primitivePacket.ToUTF8String()
if err != nil {
return "", nil
}
return value, nil
}
// ToUTF8StringSlice decode bytes to []string
func ToUTF8StringSlice(v []byte) ([]string, error) {
packet, _, err := DecodeNodePacket(v)
if err != nil {
return nil, err
}
if !packet.IsSlice() {
return nil, fmt.Errorf("v not a slice: %v", utils.FormatBytes(v))
}
result := make([]string, 0)
for _, p := range packet.PrimitivePackets {
v, _ := p.ToUTF8String()
result = append(result, v)
}
return result, nil
}
func ToBytes(v []byte) ([]byte, error) {
primitivePacket, _, _, err := DecodePrimitivePacket(v)
if err != nil {
return nil, err
}
return primitivePacket.valBuf, nil
}