-
Notifications
You must be signed in to change notification settings - Fork 0
/
varobject.go
219 lines (167 loc) · 4.44 KB
/
varobject.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
package schemer
import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
)
type VarObjectSchema struct {
SchemaOptions
Key Schema
Value Schema
}
func (s *VarObjectSchema) GoType() reflect.Type {
if s.Key == nil || s.Value == nil {
return nil
}
retval := reflect.MapOf(s.Key.GoType(), s.Value.GoType())
if s.Nullable() {
retval = reflect.PtrTo(retval)
}
return retval
}
func (s *VarObjectSchema) MarshalJSON() ([]byte, error) {
tmpMap := make(map[string]interface{}, 1)
tmpMap["type"] = "object"
tmpMap["nullable"] = s.Nullable()
m := s.Key.(json.Marshaler)
// now encode the schema for the key
keyJSON, err := m.MarshalJSON()
if err != nil {
return nil, err
}
var keyMap map[string]interface{}
err = json.Unmarshal(keyJSON, &keyMap)
if err != nil {
return nil, err
}
tmpMap["key"] = keyMap
tmp, ok := s.Value.(json.Marshaler)
if !ok {
return nil, fmt.Errorf("json.marshaler assertion failed")
}
// now encode the schema for the value
ValueJSON, err := tmp.MarshalJSON()
if err != nil {
return nil, err
}
var valueMap map[string]interface{}
err = json.Unmarshal(ValueJSON, &valueMap)
if err != nil {
return nil, err
}
tmpMap["value"] = valueMap
return json.Marshal(tmpMap)
}
// Bytes encodes the schema in a portable binary format
func (s *VarObjectSchema) MarshalSchemer() ([]byte, error) {
// string schemas are 1 byte long
var schema []byte = []byte{VarObjectByte}
// The most signifiant bit indicates whether or not the type is nullable
if s.Nullable() {
schema[0] |= NullMask
}
// bit 3 is clear from above, indicating this is a var length string
k := s.Key.(Marshaler)
key, err := k.MarshalSchemer()
if err != nil {
return nil, err
}
v := s.Value.(Marshaler)
value, err := v.MarshalSchemer()
if err != nil {
return nil, err
}
schema = append(schema, key...)
schema = append(schema, value...)
return schema, nil
}
// Encode uses the schema to write the encoded value of i to the output stream
func (s *VarObjectSchema) 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 streamm
func (s *VarObjectSchema) 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.Map {
return fmt.Errorf("varObjectSchema can only encode maps")
}
err = WriteUvarint(w, uint64(v.Len()))
if err != nil {
return errors.New("cannot encode var string length as var int")
}
for _, mapKey := range v.MapKeys() {
mapValue := v.MapIndex(mapKey)
err := s.Key.Encode(w, mapKey.Interface()) // encode key
if err != nil {
return err
}
err = s.Value.Encode(w, mapValue.Interface()) // encode value
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 *VarObjectSchema) 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 *VarObjectSchema) 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 {
var mapType = s.GoType()
v.Set(reflect.MakeMap(mapType))
v = v.Elem()
t = v.Type()
k = t.Kind()
}
if k != reflect.Map {
return fmt.Errorf("VarObjectSchema can only decode to maps")
}
// we wrote the number of entries in the map as a var int
// when we did the encoding
expectedNumEntries, err := ReadUvarint(r)
if err != nil {
return err
}
if v.IsNil() {
if !v.CanSet() {
return errors.New("v not settable")
}
var mapType = reflect.MapOf(t.Key(), t.Elem())
v.Set(reflect.MakeMap(mapType))
}
// else: we have an existing map
// right now by default, we will just keep their entries
// but we have to decide if this behavior is OK
for i := 0; i < int(expectedNumEntries); i++ {
key := reflect.New(t.Key())
val := reflect.New(t.Elem())
err = s.Key.DecodeValue(r, key) // decode key
if err != nil {
return err
}
err = s.Value.DecodeValue(r, val) // decode value
if err != nil {
return err
}
v.SetMapIndex(reflect.Indirect(key), reflect.Indirect(val))
}
return nil
}