-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathenum.go
50 lines (46 loc) · 1.25 KB
/
enum.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"
)
// EnumSchema -
type EnumSchema struct {
Type Type `json:"type"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Aliases []string `json:"aliases,omitempty"`
Documentation string `json:"doc,omitempty"`
Symbols []string `json:"symbols"`
}
// TypeName -
func (t *EnumSchema) TypeName() Type {
return TypeEnum
}
func translateValueToEnumSchema(value *fastjson.Value) (Schema, error) {
if !value.Exists("symbols") {
return nil, ErrInvalidSchema
}
symbolsValues, err := value.Get("symbols").Array()
if err != nil {
return nil, ErrInvalidSchema
}
symbolsSchemas := make([]string, 0, len(symbolsValues))
for _, symbolValue := range symbolsValues {
symbolSchema, err := symbolValue.StringBytes()
if err != nil {
return nil, ErrInvalidSchema
}
symbolsSchemas = append(symbolsSchemas, string(symbolSchema))
}
namespace, name, documentation, aliases, err := translateValueToMetaFields(value)
if err != nil {
return nil, err
}
return &EnumSchema{
Type: TypeEnum,
Namespace: namespace,
Name: name,
Aliases: aliases,
Documentation: documentation,
Symbols: symbolsSchemas,
}, nil
}