-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathderivedprimitive.go
72 lines (68 loc) · 1.77 KB
/
derivedprimitive.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
package avro
import "github.com/valyala/fastjson"
// DerivedPrimitiveSchema -
type DerivedPrimitiveSchema struct {
Type Type `json:"type"`
Documentation string `json:"doc,omitempty"`
LogicalType LogicalType `json:"logicalType"`
Precision *int `json:"precision,omitempty"`
Scale *int `json:"scale,omitempty"`
}
// TypeName -
func (t *DerivedPrimitiveSchema) TypeName() Type {
return Type(t.LogicalType)
}
func translateValue2DerivedPrimitiveSchema(typeName Type, value *fastjson.Value) (Schema, error) {
if !value.Exists("logicalType") {
return nil, ErrInvalidSchema
}
doc, err := translateValueToDocumentation(value)
if err != nil {
return nil, err
}
logicalType := LogicalType(value.GetStringBytes("logicalType"))
switch logicalType {
case LogicalTypeDate, LogicalTypeTime, LogicalTypeTimestamp:
switch typeName {
case TypeInt32, TypeInt64:
return &DerivedPrimitiveSchema{
Type: typeName,
Documentation: doc,
LogicalType: logicalType,
}, nil
default:
return nil, ErrInvalidSchema
}
case LogicalTypeDecimal:
if !value.Exists("precision") {
return nil, ErrInvalidSchema
}
precision, err := value.Get("precision").Int()
if err != nil {
return nil, ErrInvalidSchema
}
if precision < 0 {
return nil, ErrInvalidSchema
}
var scale *int
if value.Exists("scale") {
scaleInt, err := value.Get("scale").Int()
if err != nil {
return nil, ErrInvalidSchema
}
if scaleInt < 0 {
return nil, ErrInvalidSchema
}
scale = &scaleInt
}
return &DerivedPrimitiveSchema{
Type: typeName,
Documentation: doc,
LogicalType: logicalType,
Precision: &precision,
Scale: scale,
}, nil
default:
return nil, ErrInvalidSchema
}
}