-
Notifications
You must be signed in to change notification settings - Fork 10
/
record.go
50 lines (46 loc) · 1.39 KB
/
record.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
package avro
import (
"github.com/valyala/fastjson"
)
// RecordSchema has fields
type RecordSchema struct {
Type Type `json:"type"`
Namespace string `json:"namespace,omitempty"`
Name string `json:"name"`
Aliases []string `json:"aliases,omitempty"`
Documentation string `json:"doc,omitempty"`
Fields []RecordFieldSchema `json:"fields"`
}
// TypeName -
func (t *RecordSchema) TypeName() Type {
return TypeRecord
}
func translateValueToRecordSchema(value *fastjson.Value, additionalTypes ...Type) (Schema, error) {
if !value.Exists("fields") {
return nil, ErrInvalidSchema
}
fieldValues, err := value.Get("fields").Array()
if err != nil {
return nil, ErrInvalidSchema
}
namespace, name, documentation, aliases, err := translateValueToMetaFields(value)
if err != nil {
return nil, err
}
fieldSchemas := make([]RecordFieldSchema, 0, len(fieldValues))
for _, fieldValue := range fieldValues {
fieldSchema, err := translateValueToRecordFieldSchema(fieldValue, append(additionalTypes, Type(name))...)
if err != nil {
return nil, err
}
fieldSchemas = append(fieldSchemas, *fieldSchema)
}
return &RecordSchema{
Type: TypeRecord,
Namespace: namespace,
Name: name,
Aliases: aliases,
Documentation: documentation,
Fields: fieldSchemas,
}, nil
}