-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
480 lines (419 loc) · 11.4 KB
/
schema.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package jsonpack
import (
"encoding/json"
"reflect"
"strings"
"sync"
"github.com/pkg/errors"
)
// ByteOrder represents the byte order of numeric type that will be encoded to and decoded from.
// Possible values are LittleEndian and BigEndian
type ByteOrder int
// byte order for decode/encode,
const (
// little endian byte order.
LittleEndian ByteOrder = 0
// big endian byte order.
BigEndian ByteOrder = 1
)
// Schema represents a compiled jsonpack schema instance which created by jsonpack.AddSchema function
type Schema struct {
// schema name
Name string
// rawData stores schema definition from user, can be map, struct, string or slice of byte
rawData interface{}
// textData stores text json format of schema definition
textData []byte
rootOp *operation
structOpCache *sync.Map
encodeBufSize int64
byteOrder ByteOrder
}
// SchemaDef represents a schema definition that defines the structure of JSON document.
//
// Example:
// schDef := SchemaDef{
// Type: "object",
// Properties: map[string]*jsonpack.SchemaDef{
// "name": {Type: "string"},
// "area": {Type: "uint32le"},
// },
// Order: []string{"name", "area"},
// }
type SchemaDef struct {
Type string `json:"type"`
Properties map[string]*SchemaDef `json:"properties,omitempty"`
Items *SchemaDef `json:"items,omitempty"`
Order []string `json:"order,omitempty"`
}
// GetSchemaDef returns a schema definition instance,
// returns nil and error if error occurs.
func (s *Schema) GetSchemaDef() (*SchemaDef, error) {
schDef := SchemaDef{}
err := json.Unmarshal(s.textData, &schDef)
if err != nil {
return nil, err
}
return &schDef, nil
}
// GetSchemaDefText returns JSON document of schema definition that represented as []byte type.
func (s *Schema) GetSchemaDefText() []byte {
return s.textData
}
// newSchema returns a schema instance
func newSchema(name string, v ...interface{}) *Schema {
var rawData interface{}
var byteOrder ByteOrder = LittleEndian
if len(v) == 1 {
rawData = v[0]
} else if len(v) >= 2 {
rawData = v[0]
byteOrder = v[1].(ByteOrder)
}
instance := Schema{
Name: name,
rawData: rawData,
textData: nil,
rootOp: newOperation("", &nullOp{}, nullOpType),
structOpCache: &sync.Map{},
encodeBufSize: 512,
byteOrder: byteOrder,
}
return &instance
}
func (s *Schema) build() error {
var ok bool
rawDataType := reflect.TypeOf(s.rawData)
// check if raw data is SchemaDef or pointer of SchemaDef
switch schDef := s.rawData.(type) {
case SchemaDef:
textData, err := json.Marshal(schDef)
if err != nil {
return err
}
return s.buildFromTextDef(textData)
case *SchemaDef:
textData, err := json.Marshal(*schDef)
if err != nil {
return err
}
return s.buildFromTextDef(textData)
}
switch rawDataType.Kind() {
case reflect.Map:
var schemaMap map[string]interface{}
schemaMap, ok = s.rawData.(map[string]interface{})
if !ok {
return errors.New("the map type of schema definition needs to be map[string]interface{}")
}
return s.buildFromMap(schemaMap)
case reflect.Struct:
return s.buildFromStruct(rawDataType)
case reflect.String:
textDef := []byte(s.rawData.(string))
return s.buildFromTextDef(textDef)
case reflect.Slice, reflect.Array:
elemType := rawDataType.Elem()
switch elemType.Kind() {
case reflect.Uint8: // []byte
return s.buildFromTextDef(s.rawData.([]byte))
default:
return errors.New("the slice type of schema definition needs to be []byte")
}
default:
return errors.New("schema definition invalid, need to be a struct, map, []bytes or string type")
}
}
func (s *Schema) buildFromStruct(sType reflect.Type) error {
var err error
st := SchemaDef{}
err = s._buildFromStruct(&st, sType)
if err != nil {
return err
}
var textData []byte
textData, err = json.Marshal(st)
if err != nil {
return err
}
return s.buildFromTextDef(textData)
}
func (s *Schema) _buildFromStruct(st *SchemaDef, sType reflect.Type) error {
var err error
sKind := sType.Kind()
switch sKind {
case reflect.Slice, reflect.Array:
st.Type = "array"
st.Items = &SchemaDef{}
err = s._buildFromStruct(st.Items, sType.Elem())
if err != nil {
return err
}
case reflect.Struct:
st.Type = "object"
st.Properties = make(map[string]*SchemaDef)
st.Order = make([]string, sType.NumField())
for i := 0; i < sType.NumField(); i++ {
field := sType.Field(i)
var fieldName string
// lookup field name
jsonTag, ok := field.Tag.Lookup("json")
if !ok {
fieldName = field.Name
} else {
// this field is omitted, skip
if jsonTag == "-" {
continue
}
tagParts := strings.Split(jsonTag, ",")
if tagParts[0] != "" {
fieldName = tagParts[0]
} else {
fieldName = field.Name
}
}
st.Properties[fieldName] = &SchemaDef{}
err = s._buildFromStruct(st.Properties[fieldName], field.Type)
if err != nil {
return err
}
st.Order[i] = fieldName
}
case reflect.Ptr:
return s._buildFromStruct(st, sType.Elem())
default:
st.Type = s.getTypeFromKind(sKind)
if st.Type == "" {
return errors.WithStack(&UnknownTypeError{sType.String()})
}
}
return nil
}
func (s *Schema) buildFromTextDef(textDef []byte) error {
var err error
schema := make(map[string]interface{})
err = json.Unmarshal(textDef, &schema)
if err != nil {
return err
}
return s.buildFromMap(schema)
}
func (s *Schema) buildFromMap(schema map[string]interface{}) error {
var err error
schType, ok := schema["type"].(string)
if !ok {
return errors.New("Need 'type' property in top-level schema definition")
}
schType = strings.ToLower(schType)
switch schType {
case "object":
err = s.compileSchemaObject(schema, s.rootOp)
if err != nil {
return err
}
s.rootOp.handler = &objectOp{}
s.rootOp.handlerType = objectOpType
case "array":
err = s.compileSchemaArray(schema, s.rootOp)
if err != nil {
return err
}
s.rootOp.handler = _sliceOp
s.rootOp.handlerType = sliceOpType
default:
return errors.New("type property needs to be 'object' or 'array' in top-level schema definition")
}
// parse schema map object into JSON encoded text data
s.textData, err = json.Marshal(schema)
if err != nil {
return err
}
return nil
}
func _getProperties(prop interface{}) map[string]interface{} {
data := make(map[string]interface{})
switch prop := prop.(type) {
case map[string]interface{}:
return prop
case map[string]string:
for k, v := range prop {
data[k] = v
}
return data
}
return nil
}
func _getOrder(order interface{}) []string {
switch order := order.(type) {
case []interface{}:
data := make([]string, len(order))
for i, v := range order {
data[i] = v.(string)
}
return data
case []string:
return order
}
return nil
}
func (s *Schema) compileSchemaObject(schema map[string]interface{}, curOp *operation) error {
var err error
err = checkObjectProperties(schema)
if err != nil {
return err
}
var newOp *operation
properties := _getProperties(schema["properties"])
if properties == nil {
return errPropertiesProp
}
order := _getOrder(schema["order"])
if order == nil {
return errOrderProp
}
for _, fieldName := range order {
prop := properties[fieldName].(map[string]interface{})
propType, ok := prop["type"].(string)
if !ok {
return errors.New("Object type of schema definition requires valid 'type' field")
}
propType = strings.ToLower(propType)
if propType == "object" {
// create and append object operation to child operation list
newOp = newOperation(fieldName, &objectOp{}, objectOpType)
curOp.children = append(curOp.children, newOp)
err = s.compileSchemaObject(prop, newOp)
} else if propType == "array" {
// create and append object operation to child operation list
newOp = newOperation(fieldName, _sliceOp, sliceOpType)
curOp.children = append(curOp.children, newOp)
err = s.compileSchemaArray(prop, newOp)
} else if isBuiltinType(&propType) {
handler := builtinTypes[propType]
newOp = newOperation(fieldName, handler, builtinOpHandlerTypes[propType])
curOp.children = append(curOp.children, newOp)
} else {
return errors.WithStack(&UnknownTypeError{propType})
}
if err != nil {
return err
}
}
return err
}
func (s *Schema) compileSchemaArray(schema map[string]interface{}, curOp *operation) error {
var err error
err = checkArrayProperties(schema)
if err != nil {
return err
}
items := schema["items"].(map[string]interface{})
itemType := items["type"].(string)
itemType = strings.ToLower(itemType)
var newOp *operation
if itemType == "object" {
// create and append object operation to child operation list
newOp = newOperation("", &objectOp{}, objectOpType)
curOp.children = append(curOp.children, newOp)
err = s.compileSchemaObject(items, newOp)
} else if itemType == "array" {
newOp = newOperation("", _sliceOp, sliceOpType)
curOp.children = append(curOp.children, newOp)
err = s.compileSchemaArray(items, newOp)
} else if isBuiltinType(&itemType) {
handler := builtinTypes[itemType]
newOp = newOperation("", handler, builtinOpHandlerTypes[itemType])
curOp.children = append(curOp.children, newOp)
} else {
return errors.WithStack(&UnknownTypeError{itemType})
}
return err
}
const uintSize = 32 << (^uint(0) >> 32 & 1)
func (s *Schema) getTypeFromKind(kind reflect.Kind) string {
switch kind {
case reflect.Bool:
return "boolean"
case reflect.String:
return "string"
case reflect.Int:
if uintSize == 32 {
return s.getTypeEndian("int32")
}
return s.getTypeEndian("int64")
case reflect.Int8:
return "uint8"
case reflect.Int16:
return s.getTypeEndian("int16")
case reflect.Int32:
return s.getTypeEndian("int32")
case reflect.Int64:
return s.getTypeEndian("int64")
case reflect.Uint:
if uintSize == 32 {
return s.getTypeEndian("uint32")
}
return s.getTypeEndian("uint64")
case reflect.Uint8:
return "uint8"
case reflect.Uint16:
return s.getTypeEndian("uint16")
case reflect.Uint32:
return s.getTypeEndian("uint32")
case reflect.Uint64:
return s.getTypeEndian("uint64")
case reflect.Float32:
return s.getTypeEndian("float32")
case reflect.Float64:
return s.getTypeEndian("float64")
default:
return ""
}
}
func (s *Schema) getTypeEndian(typ string) string {
if s.byteOrder == BigEndian {
return typ + "BE"
}
return typ + "LE"
}
// SetEncodeBufSize sets default allocation byte size of encode buffer.
//
// The encode buffer will be allocated with this value when calling Encode/Marshall method,
// and will be re-allocate and growed automatically if necessary.
// The encoder will also adjust this value by latest encoded result.
//
// Sets a proper default allocation size might help to reduce re-allocation frequency and saves memory usage.
func (s *Schema) SetEncodeBufSize(size int64) {
s.encodeBufSize = size
}
func maxInt64(x, y int64) int64 {
if x > y {
return x
}
return y
}
func checkObjectProperties(schema map[string]interface{}) error {
var okIface, okStr bool
_, okIface = schema["properties"].(map[string]interface{})
_, okStr = schema["properties"].(map[string]string)
if !okIface && !okStr {
return errPropertiesProp
}
_, okIface = schema["order"].([]interface{})
_, okStr = schema["order"].([]string)
if !okIface && !okStr {
return errOrderProp
}
return nil
}
func checkArrayProperties(schema map[string]interface{}) error {
items, ok := schema["items"].(map[string]interface{})
if !ok {
return errItemsProp
}
_, ok = items["type"].(string)
if !ok {
return errTypeProp
}
return nil
}