-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
208 lines (194 loc) · 4.99 KB
/
encoding.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
/*
* Copyright (c) 2023, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package schema
import (
"encoding/binary"
"errors"
"fmt"
"math"
"strconv"
"strings"
)
type SchemaType interface {
[]byte | bool | string | int16 | int32 | int64 | uint16 |
uint32 | uint64 | float32 | float64
}
// EncodeType takes a SchemaType and returns the byte slice representing the
// data in a format the database expects
func EncodeType[T SchemaType](v T) ([]byte, error) {
var formatted []byte
switch t := any(v).(type) {
case []byte:
return t, nil
case bool:
var b uint8
b = 0
if t {
b = 1
}
formatted = append(formatted, b)
return formatted, nil
case string:
return []byte(t), nil
case int16:
return binary.LittleEndian.AppendUint16(formatted, uint16(t)), nil
case int32:
return binary.LittleEndian.AppendUint32(formatted, uint32(t)), nil
case int64:
return binary.LittleEndian.AppendUint64(formatted, uint64(t)), nil
case uint16:
return binary.LittleEndian.AppendUint16(formatted, t), nil
case uint32:
return binary.LittleEndian.AppendUint32(formatted, t), nil
case uint64:
return binary.LittleEndian.AppendUint64(formatted, t), nil
case float32:
return binary.LittleEndian.AppendUint32(formatted, math.Float32bits(t)), nil
case float64:
return binary.LittleEndian.AppendUint64(formatted, math.Float64bits(t)), nil
}
panic("We should not get here")
}
func DecodeStringForSchema(input []byte, s Object) (string, error) {
switch t := s.(type) {
case *Type:
switch t.Name {
case "string":
return string(input), nil
case "binary":
return fmt.Sprintf("...%d bytes...", len(input)), nil
case "boolean":
if input[0] == 0 {
return "false", nil
}
return "true", nil
case "uint8":
return fmt.Sprintf("%d", input[0]), nil
case "uint16":
return fmt.Sprintf("%d", binary.LittleEndian.Uint16(input)), nil
case "uint32":
return fmt.Sprintf("%d", binary.LittleEndian.Uint32(input)), nil
case "uint64":
return fmt.Sprintf("%d", binary.LittleEndian.Uint64(input)), nil
case "int16":
return fmt.Sprintf("%d", int16(binary.LittleEndian.Uint16(input))), nil
case "int32":
return fmt.Sprintf("%d", int32(binary.LittleEndian.Uint32(input))), nil
case "int64":
return fmt.Sprintf("%d", int64(binary.LittleEndian.Uint64(input))), nil
case "float32":
return fmt.Sprintf("%f", math.Float32frombits(binary.LittleEndian.Uint32(input))), nil
case "float64":
return fmt.Sprintf("%f", math.Float64frombits(binary.LittleEndian.Uint64(input))), nil
}
case *Array:
var output string
for i := 0; i < t.Length; i++ {
width := t.Type.Size()
e, err := DecodeStringForSchema(input[i*width:(i+1)*width], &t.Type)
if err != nil {
return "", err
}
output += e
if i < t.Length-1 {
output += ", "
}
}
return output, nil
case *Composite:
// FIXME: Implement
}
return "", errors.New("unknown schema")
}
// EncodeStringForSchema takes an input string and a Object, and returns
// a byte slice representing that string.
func EncodeStringForSchema(input string, s Object) ([]byte, error) {
var formatted []byte
switch t := s.(type) {
case *Type:
switch t.Name {
case "string":
return []byte(input), nil
case "binary":
return []byte(input), nil
case "boolean":
var b uint8
b = 1
if input == "false" {
b = 0
}
formatted = append(formatted, b)
return formatted, nil
case "int16":
i, err := strconv.ParseInt(input, 10, 16)
if err != nil {
return nil, err
}
return EncodeType(int16(i))
case "int32":
i, err := strconv.ParseInt(input, 10, 32)
if err != nil {
return nil, err
}
return EncodeType(int32(i))
case "int64":
i, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return nil, err
}
return EncodeType(i)
case "uint16":
i, err := strconv.ParseUint(input, 10, 16)
if err != nil {
return nil, err
}
return EncodeType(uint16(i))
case "uint32":
i, err := strconv.ParseUint(input, 10, 32)
if err != nil {
return nil, err
}
return EncodeType(uint32(i))
case "uint64":
i, err := strconv.ParseUint(input, 10, 64)
if err != nil {
return nil, err
}
return EncodeType(i)
case "float32":
f, err := strconv.ParseFloat(input, 32)
if err != nil {
return nil, err
}
return EncodeType(float32(f))
case "float64":
f, err := strconv.ParseFloat(input, 64)
if err != nil {
return nil, err
}
return EncodeType(f)
}
case *Array:
var array []string
array = strings.Split(input, ",")
// Basic bounds checking
if len(array) != t.Length {
return nil, fmt.Errorf("schema expects %d elements, you provided %d", t.Length, len(array))
}
// For each value in the array, pack it into formatted
for _, v := range array {
b, err := EncodeStringForSchema(strings.Trim(v, " \t"), &t.Type)
if err != nil {
return nil, err
}
formatted = append(formatted, b...)
}
return formatted, nil
case *Composite:
// FIXME: Implement
}
return formatted, nil
}