-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedarray.go
183 lines (138 loc) · 4.02 KB
/
fixedarray.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
package schemer
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"reflect"
)
type FixedArraySchema struct {
SchemaOptions
Length int
Element Schema
}
func (s *FixedArraySchema) GoType() reflect.Type {
retval := reflect.ArrayOf(s.Length, s.Element.GoType())
if s.Nullable() {
return reflect.PtrTo(retval)
}
return retval
}
func (s *FixedArraySchema) Valid() bool {
return s.Length >= 0
}
// Bytes encodes the schema in a portable binary format
func (s *FixedArraySchema) MarshalSchemer() ([]byte, error) {
// fixed length schemas are 1 byte long total
var schema []byte = []byte{FixedArrayByte}
// The most signifiant bit indicates whether or not the type is nullable
if s.Nullable() {
schema[0] |= NullMask
}
// encode array fixed length as a varint
buf := make([]byte, binary.MaxVarintLen64)
varIntByteLength := binary.PutVarint(buf, int64(s.Length))
schema = append(schema, buf[0:varIntByteLength]...)
// now encode the schema for the type of this array
m, ok := s.Element.(Marshaler)
if !ok {
return nil, fmt.Errorf("element does not implement MarshalSchemer")
}
elementData, err := m.MarshalSchemer()
if err != nil {
return nil, fmt.Errorf("element does not implement MarshalSchemer")
}
schema = append(schema, elementData...)
return schema, nil
}
func (s *FixedArraySchema) MarshalJSON() ([]byte, error) {
if !s.Valid() {
return nil, fmt.Errorf("invalid FixedArraySchema")
}
tmpMap := make(map[string]interface{}, 3)
tmpMap["type"] = "array"
tmpMap["length"] = s.Length
tmpMap["nullable"] = s.Nullable()
// now encode the schema for the element
t, ok := s.Element.(json.Marshaler)
if !ok {
return nil, fmt.Errorf("element does not implement MarshalJSON")
}
elementJSON, err := t.MarshalJSON()
if err != nil {
return nil, err
}
var elementMap map[string]interface{}
err = json.Unmarshal(elementJSON, &elementMap)
if err != nil {
return nil, err
}
tmpMap["element"] = elementMap
return json.Marshal(tmpMap)
}
// Encode uses the schema to write the encoded value of i to the output stream
func (s *FixedArraySchema) Encode(w io.Writer, i interface{}) error {
return s.EncodeValue(w, reflect.ValueOf(i))
}
// EncodeValue uses the schema to write the encoded value of v to the output stream
func (s *FixedArraySchema) EncodeValue(w io.Writer, v reflect.Value) error {
// just double check the schema they are using
if !s.Valid() {
return fmt.Errorf("cannot encode using invalid FixedArraySchema schema")
}
done, err := PreEncode(w, &v, s.Nullable())
if err != nil || done {
return err
}
t := v.Type()
k := t.Kind()
if k != reflect.Array {
return fmt.Errorf("FixedArraySchema can only encode fixed length arrays")
}
if s.Length != v.Len() {
return fmt.Errorf("source array size does not match schema size")
}
for i := 0; i < v.Len(); i++ {
s.Element.Encode(w, v.Index(i).Interface())
}
return nil
}
// Decode uses the schema to read the next encoded value from the input stream and store it in i
func (s *FixedArraySchema) Decode(r io.Reader, i interface{}) error {
if i == nil {
return fmt.Errorf("cannot decode to nil destination")
}
return s.DecodeValue(r, reflect.ValueOf(i))
}
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
func (s *FixedArraySchema) DecodeValue(r io.Reader, v reflect.Value) error {
// just double check the schema they are using
if !s.Valid() {
return fmt.Errorf("cannot decode using invalid FixedArraySchema schema")
}
done, err := PreDecode(r, &v, s.Nullable())
if err != nil || done {
return err
}
t := v.Type()
k := t.Kind()
if k == reflect.Interface {
v.Set(reflect.New(s.GoType()))
v = v.Elem().Elem()
t = v.Type()
k = t.Kind()
}
if k != reflect.Array {
return fmt.Errorf("FixedArraySchema can only encode fixed length arrays")
}
if s.Length != v.Len() {
return fmt.Errorf("source array size does not match schema size")
}
for i := 0; i < s.Length; i++ {
err := s.Element.DecodeValue(r, v.Index(i))
if err != nil {
return err
}
}
return nil
}