-
Notifications
You must be signed in to change notification settings - Fork 10
/
record.field.go
66 lines (60 loc) · 1.73 KB
/
record.field.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
package avro
import (
"encoding/json"
"github.com/valyala/fastjson"
)
// RecordFieldSchema -
type RecordFieldSchema struct {
Name string `json:"name"`
Aliases []string `json:"aliases,omitempty"`
Documentation string `json:"doc,omitempty"`
Type Schema `json:"type"`
Default *json.RawMessage `json:"default,omitempty"`
Order Order `json:"order,omitempty"`
}
// Order - specifies how this field impacts sort ordering of this record (optional).
// Valid values are "ascending" (the default), "descending", or "ignore".
type Order string
const (
// Ascending -
Ascending Order = "ascending"
// Descending -
Descending Order = "descending"
// Ignore -
Ignore Order = "ignore"
)
func translateValueToRecordFieldSchema(value *fastjson.Value, additionalTypes ...Type) (*RecordFieldSchema, error) {
if !value.Exists("type") {
return nil, ErrInvalidSchema
}
anySchema, err := translateValue2AnySchema(value.Get("type"), additionalTypes...)
if err != nil {
return nil, err
}
var (
order Order
defaultValue *json.RawMessage
)
if value.Exists("order") {
order = Order(value.GetStringBytes("order"))
if order != Ascending && order != Ignore && order != Descending {
return nil, ErrInvalidSchema
}
}
if value.Exists("default") {
defaultValue = new(json.RawMessage)
*defaultValue = value.Get("default").MarshalTo(*defaultValue)
}
_, name, documentation, aliases, err := translateValueToMetaFields(value)
if err != nil {
return nil, err
}
return &RecordFieldSchema{
Name: name,
Aliases: aliases,
Documentation: documentation,
Type: anySchema,
Default: defaultValue,
Order: order,
}, nil
}