-
Notifications
You must be signed in to change notification settings - Fork 10
/
metadata.go
51 lines (48 loc) · 1.45 KB
/
metadata.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
package avro
import "github.com/valyala/fastjson"
func translateValueToMetaFields(value *fastjson.Value) (namespace, name, documentation string, aliases []string, err error) {
if !value.Exists("name") {
return "", "", "", nil, ErrInvalidSchema
}
nameBytes, err := value.Get("name").StringBytes()
if err != nil {
return "", "", "", nil, ErrInvalidSchema
}
name = string(nameBytes)
if value.Exists("namespace") {
namespaceBytes, err := value.Get("namespace").StringBytes()
if err != nil {
return "", "", "", nil, ErrInvalidSchema
}
namespace = string(namespaceBytes)
}
documentation, err = translateValueToDocumentation(value)
if err != nil {
return "", "", "", nil, err
}
if value.Exists("aliases") {
aliasValues, err := value.Get("aliases").Array()
if err != nil {
return "", "", "", nil, ErrInvalidSchema
}
aliases = make([]string, 0, len(aliasValues))
for _, aliasValue := range aliasValues {
aliasStringBytes, err := aliasValue.StringBytes()
if err != nil {
return "", "", "", nil, ErrInvalidSchema
}
aliases = append(aliases, string(aliasStringBytes))
}
}
return namespace, name, documentation, aliases, nil
}
func translateValueToDocumentation(value *fastjson.Value) (documentation string, err error) {
if value.Exists("doc") {
documentationBytes, err := value.Get("doc").StringBytes()
if err != nil {
return "", ErrInvalidSchema
}
documentation = string(documentationBytes)
}
return documentation, nil
}