-
Notifications
You must be signed in to change notification settings - Fork 98
/
resolver.go
90 lines (74 loc) · 2.36 KB
/
resolver.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package avro
import (
"fmt"
"math/big"
"sync"
"time"
"github.com/modern-go/reflect2"
)
// TypeResolver resolves types by name.
type TypeResolver struct {
names sync.Map // map[string]reflect2.Type
types sync.Map // map[int][]string
}
// NewTypeResolver creates a new type resolver with all primitive types
// registered.
func NewTypeResolver() *TypeResolver {
r := &TypeResolver{}
// Register basic types
r.Register(string(Null), &null{})
r.Register(string(Int), int8(0))
r.Register(string(Int), int16(0))
r.Register(string(Int), int32(0))
r.Register(string(Int), int(0))
r.Register(string(Long), int(0))
r.Register(string(Long), int64(0))
r.Register(string(Float), float32(0))
r.Register(string(Double), float64(0))
r.Register(string(String), "")
r.Register(string(Bytes), []byte{})
r.Register(string(Boolean), true)
// Register logical types
r.Register(string(Int)+"."+string(Date), time.Time{})
r.Register(string(Int)+"."+string(TimeMillis), time.Duration(0))
r.Register(string(Long)+"."+string(TimestampMillis), time.Time{})
r.Register(string(Long)+"."+string(TimestampMicros), time.Time{})
r.Register(string(Long)+"."+string(TimeMicros), time.Duration(0))
r.Register(string(Bytes)+"."+string(Decimal), big.NewRat(1, 1))
r.Register(string(String)+"."+string(UUID), "")
return r
}
// Register registers names to their types for resolution.
func (r *TypeResolver) Register(name string, obj any) {
typ := reflect2.TypeOf(obj)
rtype := typ.RType()
r.names.Store(name, typ)
raw, ok := r.types.LoadOrStore(rtype, []string{name})
if !ok {
return
}
names := raw.([]string)
names = append(names, name)
r.types.Store(rtype, names)
}
// Name gets the name for a type, or an error.
func (r *TypeResolver) Name(typ reflect2.Type) ([]string, error) {
rtype := typ.RType()
names, ok := r.types.Load(rtype)
if !ok {
return nil, fmt.Errorf("avro: unable to resolve type %s", typ.String())
}
return names.([]string), nil
}
// Type gets the type for a name, or an error.
func (r *TypeResolver) Type(name string) (reflect2.Type, error) {
typ, ok := r.names.Load(name)
if !ok {
return nil, fmt.Errorf("avro: unable to resolve type with name %s", name)
}
return typ.(reflect2.Type), nil
}
// Register registers names to their types for resolution. All primitive types are pre-registered.
func Register(name string, obj any) {
DefaultConfig.Register(name, obj)
}