-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype_registry.go
118 lines (104 loc) · 2.82 KB
/
datatype_registry.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package mtx
import "fmt"
type TypeRegistry struct {
class string
knownTypes map[string]Datatype
encodedTypes map[string]Datatype
trace bool
}
func NewTypeRegistry(class string) *TypeRegistry {
return &TypeRegistry{
class: class,
knownTypes: map[string]Datatype{},
encodedTypes: map[string]Datatype{},
}
}
func (r *TypeRegistry) Trace() { r.trace = true }
func (r *TypeRegistry) Class() string { return r.class }
// MappedAttributeType returns the best matching known or encoded type.
// Properties set on the argument type are copied into the new result
func (r *TypeRegistry) MappedAttributeType(basicType Datatype) Datatype {
if !basicType.HasName() {
if r.trace {
trace("registry", r.class, "basicType", basicType, "msg", "no name")
}
return r.knownTypes["any"]
}
// check encoded types
et, ok := r.encodedTypes[basicType.Name]
if ok {
if r.trace {
trace("registry", r.class, "basicType", basicType, "encodedType", et)
}
return et.WithCopiedPropertiesFrom(basicType)
}
// check known types
for _, each := range r.knownTypes {
if dt := each.BasicDatatype; dt != nil && dt.Name == basicType.Name {
return each.WithCopiedPropertiesFrom(basicType)
}
}
// fallback
a, ok := r.knownTypes["any"] // must have an any
if !ok {
panic("warning: missing any in " + r.class)
}
if r.trace {
trace("registry", r.class, "basicType", basicType, "fallback to", a)
}
return a.WithCopiedPropertiesFrom(basicType)
}
func (r *TypeRegistry) EncodeAs(attrType Datatype, encodedType Datatype) {
r.encodedTypes[attrType.Name] = encodedType
}
func (r *TypeRegistry) Add(d Datatype) Datatype {
if d.Class != r.class {
panic("wrong class")
}
r.knownTypes[d.GetName()] = d
return d
}
func (r *TypeRegistry) Register(typename string, isUserDefined bool) Datatype {
dt := Datatype{
Named: N(r.class, typename),
IsUserDefined: isUserDefined,
}
return r.Add(dt)
}
func (r *TypeRegistry) Type(typename string) Datatype {
dt, ok := r.knownTypes[typename]
if ok {
return dt
}
return r.RegisterType(typename, Unknown)
}
func (r *TypeRegistry) RegisterType(typename string, attrType Datatype) Datatype {
dt := Datatype{
Named: N(r.class, typename),
BasicDatatype: &attrType,
IsUserDefined: true,
}
return r.Add(dt)
}
// TODO change to used slice of Prop
func (r *TypeRegistry) Standard(typename string, attrType Datatype, props ...any) Datatype {
dt := Datatype{
Named: N(r.class, typename),
BasicDatatype: &attrType,
}
for i := 0; i < len(props); i = i + 2 {
key := props[i].(string)
value := props[i+1]
dt = dt.Set(key, value)
}
return r.Add(dt)
}
func trace(params ...any) {
fmt.Print("[trace] ")
fmt.Println(params...)
}
type Prop struct {
Key string
Value any
}
func Property(key string, value any) Prop { return Prop{Key: key, Value: value} }