-
Notifications
You must be signed in to change notification settings - Fork 2
/
qt_meta.qtpl.go
344 lines (301 loc) · 11 KB
/
qt_meta.qtpl.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Code generated by qtc from "qt_meta.qtpl". DO NOT EDIT.
// See https://github.com/valyala/quicktemplate for details.
package tlcodegen
import "fmt"
import (
qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate"
)
var (
_ = qtio422016.Copy
_ = qt422016.AcquireByteBuffer
)
func (gen *Gen2) streamgenerateMeta(qw422016 *qt422016.Writer) {
qw422016.N().S(HeaderComment)
qw422016.N().S(`
`)
typeWrappers := gen.generatedTypesList
qw422016.N().S(`package `)
qw422016.E().S(MetaGoPackageName)
qw422016.N().S(`
import (
"fmt"
`)
qw422016.N().Q(gen.BasicPackageNameFull)
qw422016.N().S(`
"`)
qw422016.N().S(gen.options.TLPackageNameFull)
qw422016.N().S(`/internal"
)
// We can create only types which have zero type arguments and zero nat arguments
type Object interface {
TLName() string // returns type's TL name. For union, returns constructor name depending on actual union value
TLTag() uint32 // returns type's TL tag. For union, returns constructor tag depending on actual union value
String() string // returns type's representation for debugging (JSON for now)
`)
if gen.options.GenerateRandomCode {
qw422016.N().S(` FillRandom(rg *basictl.RandGenerator)
`)
}
qw422016.N().S(` Read(w []byte) ([]byte, error) // reads type's bare TL representation by consuming bytes from the start of w and returns remaining bytes, plus error
ReadBoxed(w []byte) ([]byte, error) // same as Read, but reads/checks TLTag first (this method is general version of Write, use it only when you are working with interface)
WriteGeneral(w []byte) ([]byte, error) // appends bytes of type's bare TL representation to the end of w and returns it, plus error
WriteBoxedGeneral(w []byte) ([]byte, error) // same as Write, but writes TLTag first (this method is general version of WriteBoxed, use it only when you are working with interface)
MarshalJSON() ([]byte, error) // returns type's JSON representation, plus error
UnmarshalJSON([]byte) error // reads type's JSON representation
`)
if gen.options.GenerateLegacyJsonRead {
qw422016.N().S(` ReadJSONLegacy(legacyTypeNames bool, j interface{}) error
`)
}
qw422016.N().S(` ReadJSON(legacyTypeNames bool, in *basictl.JsonLexer) error
WriteJSONGeneral(w []byte) ([]byte, error) // like MarshalJSON, but appends to w and returns it (this method is general version of WriteBoxed, use it only when you are working with interface)
}
type Function interface {
Object
ReadResultWriteResultJSON(r []byte, w []byte) ([]byte, []byte, error) // combination of ReadResult(r) + WriteResultJSON(w). Returns new r, new w, plus error
ReadResultJSONWriteResult(r []byte, w []byte) ([]byte, []byte, error) // combination of ReadResultJSON(r) + WriteResult(w). Returns new r, new w, plus error
// For transcoding short-long version during Long ID and newTypeNames transition
ReadResultWriteResultJSONOpt(newTypeNames bool, short bool, r []byte, w []byte) ([]byte, []byte, error)
}
func GetAllTLItems() []TLItem {
var allItems []TLItem
for _, item := range itemsByName {
if item != nil {
allItems = append(allItems, *item)
}
}
return allItems
}
// for quick one-liners
func GetTLName(tag uint32, notFoundName string) string {
if item := FactoryItemByTLTag(tag); item != nil {
return item.TLName()
}
return notFoundName
}
func CreateFunction(tag uint32) Function {
if item := FactoryItemByTLTag(tag); item != nil && item.createFunction != nil {
return item.createFunction()
}
return nil
}
func CreateObject(tag uint32) Object {
if item := FactoryItemByTLTag(tag); item != nil && item.createObject != nil {
return item.createObject()
}
return nil
}
// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateFunctionFromName(name string) Function {
if item := FactoryItemByTLName(name); item != nil && item.createFunction != nil {
return item.createFunction()
}
return nil
}
// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateObjectFromName(name string) Object {
if item := FactoryItemByTLName(name); item != nil && item.createObject != nil {
return item.createObject()
}
return nil
}
`)
if gen.options.GenerateLegacyJsonRead {
qw422016.N().S(`// legacy wrapper, will be removed soon
func ReadJSONLegacy(item Object, legacyTypeNames bool, b []byte) error {
j, err := internal.JsonBytesToInterface(b)
if err != nil {
return internal.ErrorInvalidJSON(item.TLName(), err.Error())
}
if err = item.ReadJSONLegacy(legacyTypeNames, j); err != nil {
return internal.ErrorInvalidJSON(item.TLName(), err.Error())
}
return nil
}
`)
}
qw422016.N().S(`
type TLItem struct {
tag uint32
annotations uint32
tlName string
createFunction func() Function
createFunctionLong func() Function
createObject func() Object
}
func (item TLItem) TLTag() uint32 { return item.tag }
func (item TLItem) TLName() string { return item.tlName }
func (item TLItem) CreateObject() Object { return item.createObject() }
func (item TLItem) IsFunction() bool { return item.createFunction != nil }
func (item TLItem) CreateFunction() Function { return item.createFunction() }
// For transcoding short-long version during Long ID transition
func (item TLItem) HasFunctionLong() bool { return item.createFunctionLong != nil }
func (item TLItem) CreateFunctionLong() Function { return item.createFunctionLong() }
// Annotations
`)
for bit, name := range gen.allAnnotations {
goName := ToUpperFirst(name)
qw422016.N().S(`func (item TLItem) Annotation`)
qw422016.N().S(goName)
qw422016.N().S(`() bool { return item.annotations & `)
qw422016.N().S(fmt.Sprintf("%#x", 1<<bit))
qw422016.N().S(` != 0 }
`)
}
qw422016.N().S(`
// TLItem serves as a single type for all enum values
func (item *TLItem) Reset() {}
`)
if gen.options.GenerateRandomCode {
qw422016.N().S(` func (item *TLItem) FillRandom(rg *basictl.RandGenerator) {}
`)
}
qw422016.N().S(`func (item *TLItem) Read(w []byte) ([]byte, error) { return w, nil }
func (item *TLItem) WriteGeneral(w []byte) ([]byte, error) { return w, nil }
func (item *TLItem) Write(w []byte) []byte { return w }
func (item *TLItem) ReadBoxed(w []byte) ([]byte, error) { return basictl.NatReadExactTag(w, item.tag) }
func (item *TLItem) WriteBoxedGeneral(w []byte) ([]byte, error) { return basictl.NatWrite(w, item.tag), nil }
func (item *TLItem) WriteBoxed(w []byte) []byte { return basictl.NatWrite(w, item.tag) }
func (item TLItem) String() string {
return string(item.WriteJSON(nil))
}
`)
if gen.options.GenerateLegacyJsonRead {
qw422016.N().S(`func (item *TLItem) ReadJSONLegacy(legacyTypeNames bool, j interface{}) error {
_jm, _ok := j.(map[string]interface{})
if j != nil && !_ok {
return internal.ErrorInvalidJSON(item.tlName, "expected json object")
}
for k := range _jm {
return internal.ErrorInvalidJSONExcessElement(item.tlName, k)
}
return nil
}
`)
}
qw422016.N().S(`func (item *TLItem) ReadJSON(legacyTypeNames bool, in *basictl.JsonLexer) error {
in.Delim('{')
if !in.Ok() {
return in.Error()
}
for !in.IsDelim('}') {
return internal.ErrorInvalidJSONExcessElement(item.tlName, in.UnsafeFieldName(true))
}
in.Delim('}')
if !in.Ok() {
return in.Error()
}
return nil
}
func (item *TLItem) WriteJSONGeneral(w []byte) (_ []byte, err error) {
return item.WriteJSON(w), nil
}
func (item *TLItem) WriteJSON(w []byte) []byte {
w = append(w, '{')
return append(w, '}')
}
func (item *TLItem) MarshalJSON() ([]byte, error) {
return item.WriteJSON(nil), nil
}
func (item *TLItem) UnmarshalJSON(b []byte) error {
if err := item.ReadJSON(true, &basictl.JsonLexer{Data: b}); err != nil {
return internal.ErrorInvalidJSON(item.tlName, err.Error())
}
return nil
}
func FactoryItemByTLTag(tag uint32) *TLItem {
return itemsByTag[tag]
}
func FactoryItemByTLName(name string) *TLItem {
return itemsByName[name]
}
var itemsByTag = map[uint32]*TLItem {}
var itemsByName = map[string]*TLItem {}
func SetGlobalFactoryCreateForFunction(itemTag uint32, createObject func() Object, createFunction func() Function, createFunctionLong func() Function) {
item := itemsByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find function tag #%08x to set", itemTag))
}
item.createObject = createObject
item.createFunction = createFunction
item.createFunctionLong = createFunctionLong
}
func SetGlobalFactoryCreateForObject(itemTag uint32, createObject func() Object) {
item := itemsByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find item tag #%08x to set", itemTag))
}
item.createObject = createObject
}
func SetGlobalFactoryCreateForEnumElement(itemTag uint32) {
item := itemsByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find enum tag #%08x to set", itemTag))
}
item.createObject = func() Object { return item }
}
func pleaseImportFactoryObject() Object {
panic("factory functions are not linked to reduce code bloat, please import 'gen/factory' instead of 'gen/meta'.")
}
func pleaseImportFactoryFunction() Function {
panic("factory functions are not linked to reduce code bloat, please import 'gen/factory' instead of 'gen/meta'.")
}
func fillObject(n1 string, n2 string, item *TLItem) {
itemsByTag[item.tag] = item
itemsByName[item.tlName] = item
itemsByName[n1] = item
itemsByName[n2] = item
item.createObject = pleaseImportFactoryObject
// code below is as fast, but allocates some extra strings which are already in binary const segment due to JSON code
// itemsByName[fmt.Sprintf("%s#%08x", item.tlName, item.tag)] = item
// itemsByName[fmt.Sprintf("#%08x", item.tag)] = item
}
func fillFunction(n1 string, n2 string, item *TLItem) {
fillObject(n1, n2, item)
item.createFunction = pleaseImportFactoryFunction
}
func init() {
`)
for _, wr := range typeWrappers {
if wr.tlTag == 0 || !wr.IsTopLevel() {
continue
}
if fun, ok := wr.trw.(*TypeRWStruct); ok && len(wr.NatParams) == 0 {
if fun.ResultType != nil {
qw422016.N().S(`fillFunction(`)
} else {
qw422016.N().S(`fillObject(`)
}
qw422016.N().S(`"`)
wr.tlName.StreamString(qw422016)
qw422016.N().S(`#`)
qw422016.N().S(fmt.Sprintf("%08x", wr.tlTag))
qw422016.N().S(`",`)
qw422016.N().Q(fmt.Sprintf("#%08x", wr.tlTag))
qw422016.N().S(`,&TLItem{tag:`)
qw422016.N().S(fmt.Sprintf("%#x", wr.tlTag))
qw422016.N().S(`, annotations:`)
qw422016.N().S(fmt.Sprintf("%#x", wr.AnnotationsMask()))
qw422016.N().S(`, tlName: "`)
wr.tlName.StreamString(qw422016)
qw422016.N().S(`"})`)
}
qw422016.N().S(`
`)
}
qw422016.N().S(`}
`)
}
func (gen *Gen2) writegenerateMeta(qq422016 qtio422016.Writer) {
qw422016 := qt422016.AcquireWriter(qq422016)
gen.streamgenerateMeta(qw422016)
qt422016.ReleaseWriter(qw422016)
}
func (gen *Gen2) generateMeta() string {
qb422016 := qt422016.AcquireByteBuffer()
gen.writegenerateMeta(qb422016)
qs422016 := string(qb422016.B)
qt422016.ReleaseByteBuffer(qb422016)
return qs422016
}