forked from andreyvit/telegramapi
/
schema.go
207 lines (169 loc) · 4.1 KB
/
schema.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package tlschema
import (
"fmt"
)
type Schema struct {
combs []*Comb
funcs []*Comb
tagsToCombs map[uint32]*Comb
namesToCombs map[string]*Comb
types []*Type
namesToTypes map[string]*Type
}
func (sch *Schema) Combs() []*Comb {
return sch.combs
}
func (sch *Schema) Funcs() []*Comb {
return sch.funcs
}
func (sch *Schema) ByTag(tag uint32) *Comb {
return sch.tagsToCombs[tag]
}
func (sch *Schema) ByName(name string) *Comb {
return sch.namesToCombs[name]
}
func (sch *Schema) Types() []*Type {
return sch.types
}
func (sch *Schema) Type(name string) *Type {
return sch.namesToTypes[name]
}
func MustParse(text string) *Schema {
sch := new(Schema)
err := sch.Parse(text, ParseOptions{
FixZeroTags: true,
})
if err != nil {
panic(err)
}
return sch
}
type Priority int
const (
PriorityNormal Priority = 0
PriorityJustADefault = -10
PriorityOverride = 10
)
type ParseOptions struct {
Origin string
Priority Priority
FixZeroTags bool
FixZeroTagsIn map[string]bool
AllowZeroTags bool
MarkInternal bool
Alterations *Alterations
}
func (sch *Schema) Parse(text string, options ParseOptions) error {
defs, err := Parse(text)
if err != nil {
return err
}
if sch.tagsToCombs == nil {
sch.tagsToCombs = make(map[uint32]*Comb)
sch.namesToCombs = make(map[string]*Comb)
sch.namesToTypes = make(map[string]*Type)
}
for _, def := range defs {
if def.Tag == 0 {
if options.FixZeroTags || (options.FixZeroTagsIn != nil && options.FixZeroTagsIn[def.CombName.Full()]) {
err = def.FixTag()
if err != nil {
return err
}
} else if !options.AllowZeroTags {
return fmt.Errorf("zero tag in %q from %s", def.OriginalStr, options.Origin)
}
}
if options.Alterations != nil {
def.Alter(options.Alterations)
}
err = def.Simplify()
if err != nil {
return err
}
if options.MarkInternal {
def.IsInternal = true
}
err = sch.addComb(&Comb{Def: def, Origin: options.Origin, Priority: options.Priority})
if err != nil {
return err
}
}
return nil
}
func (sch *Schema) addComb(comb *Comb) error {
name := comb.CombName.Full()
if name != "" {
if alt := sch.namesToCombs[name]; alt != nil {
if alt.Priority == comb.Priority {
return fmt.Errorf("duplicate combinators: %s#%08x in %s and %s#%08x in %s", name, alt.Tag, alt.Origin, name, comb.Tag, comb.Origin)
} else if alt.Priority > comb.Priority {
return nil
} else {
sch.removeComb(alt)
}
}
}
if comb.Tag != 0 {
if alt := sch.tagsToCombs[comb.Tag]; alt != nil {
if alt.Priority == comb.Priority {
return fmt.Errorf("tag %08x conflict between %s (in %s) and %s (in %s)", comb.Tag, alt.CombName.String(), alt.Origin, comb.CombName.String(), comb.Origin)
} else if alt.Priority > comb.Priority {
return nil
} else {
sch.removeComb(alt)
}
}
}
sch.combs = append(sch.combs, comb)
if comb.IsFunc {
sch.funcs = append(sch.funcs, comb)
}
if comb.Tag != 0 {
sch.tagsToCombs[comb.Tag] = comb
}
if name != "" {
sch.namesToCombs[name] = comb
}
if !comb.IsFunc && comb.ResultType.IsJustTypeName() {
comb.TypeStr = comb.ResultType.Name.Full()
typ := sch.namesToTypes[comb.TypeStr]
if typ == nil {
if comb.ResultType.Name.IsBare() {
return fmt.Errorf("bare result type: %v", comb.ResultType.String())
}
typ = &Type{Name: comb.ResultType.Name, Origin: comb.Origin}
sch.types = append(sch.types, typ)
sch.namesToTypes[comb.TypeStr] = typ
}
typ.Ctors = append(typ.Ctors, comb)
}
return nil
}
func (sch *Schema) removeComb(comb *Comb) {
name := comb.CombName.Full()
if comb.Tag != 0 {
delete(sch.tagsToCombs, comb.Tag)
}
if name != "" {
delete(sch.namesToCombs, name)
}
for i, c := range sch.combs {
if c == comb {
sch.combs = append(sch.combs[:i], sch.combs[i+1:]...)
break
}
}
if !comb.IsFunc && comb.ResultType.IsJustTypeName() {
typ := sch.namesToTypes[comb.TypeStr]
if typ != nil {
delete(sch.namesToTypes, comb.TypeStr)
for i, t := range sch.types {
if t == typ {
sch.types = append(sch.types[:i], sch.types[i+1:]...)
break
}
}
}
}
}