-
Notifications
You must be signed in to change notification settings - Fork 0
/
vararray.go
164 lines (125 loc) · 3.34 KB
/
vararray.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
package schemer
import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
)
type VarArraySchema struct {
SchemaOptions
Element Schema
}
func (s *VarArraySchema) GoType() reflect.Type {
retval := reflect.SliceOf(s.Element.GoType())
if s.Nullable() {
retval = reflect.PtrTo(retval)
}
return retval
}
// Bytes encodes the schema in a portable binary format
func (s *VarArraySchema) MarshalSchemer() ([]byte, error) {
// fixed length schemas are 1 byte long total
var schema []byte = []byte{VarArrayByte}
// The most signifiant bit indicates whether or not the type is nullable
if s.Nullable() {
schema[0] |= NullMask
}
m := s.Element.(Marshaler)
tmp, err := m.MarshalSchemer()
if err != nil {
return nil, err
}
schema = append(schema, tmp...)
return schema, nil
}
func (s *VarArraySchema) MarshalJSON() ([]byte, error) {
tmpMap := make(map[string]interface{}, 2)
tmpMap["type"] = "array"
tmpMap["nullable"] = s.Nullable()
m := s.Element.(json.Marshaler)
// now encode the schema for the element
elementJSON, err := m.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 *VarArraySchema) 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 *VarArraySchema) EncodeValue(w io.Writer, v reflect.Value) error {
done, err := PreEncode(w, &v, s.Nullable())
if err != nil || done {
return err
}
t := v.Type()
k := t.Kind()
if k != reflect.Slice {
return fmt.Errorf("VarArraySchema can only encode slices")
}
err = WriteUvarint(w, uint64(v.Len()))
if err != nil {
return errors.New("cannot encode var string length as var int")
}
for i := 0; i < v.Len(); i++ {
err := s.Element.Encode(w, v.Index(i).Interface())
if err != nil {
return err
}
}
return nil
}
// Decode uses the schema to read the next encoded value from the input stream and store it in i
func (s *VarArraySchema) 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 *VarArraySchema) DecodeValue(r io.Reader, v reflect.Value) error {
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.Slice {
return fmt.Errorf("VarArraySchema can only decode to slices")
}
expectedLen, err := ReadUvarint(r)
if err != nil {
return err
}
if v.IsNil() {
if !v.CanSet() {
return errors.New("v not settable")
}
v.Set(reflect.MakeSlice(t, int(expectedLen), int(expectedLen)))
}
// else we have an existing slice
// right now by default, we will just keep their entries
// but we have to decide if this behavior is OK??
for i := 0; i < v.Len(); i++ {
err := s.Element.DecodeValue(r, v.Index(i))
if err != nil {
return err
}
}
return nil
}