-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_rw_union.go
148 lines (124 loc) · 5.32 KB
/
type_rw_union.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
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package tlcodegen
import (
"fmt"
)
type TypeRWUnion struct {
wr *TypeRWWrapper
Fields []Field
IsEnum bool
fieldsDec Deconflicter // TODO - add all generated methods here
fieldsDecCPP Deconflicter // TODO - add all generated methods here
}
func (trw *TypeRWUnion) typeString2(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, isLocal bool, skipAlias bool) string {
if isLocal {
return addBytes(trw.wr.goLocalName, bytesVersion)
}
return trw.wr.ins.Prefix(directImports, ins) + addBytes(trw.wr.goGlobalName, bytesVersion)
}
func (trw *TypeRWUnion) markHasBytesVersion(visitedNodes map[*TypeRWWrapper]bool) bool {
result := false
for _, f := range trw.Fields {
result = result || f.t.MarkHasBytesVersion(visitedNodes)
}
return result
}
func (trw *TypeRWUnion) markWriteHasError(visitedNodes map[*TypeRWWrapper]bool) bool {
result := false
for _, f := range trw.Fields {
result = result || f.t.MarkWriteHasError(visitedNodes)
}
return result
}
func (trw *TypeRWUnion) fillRecursiveUnwrap(visitedNodes map[*TypeRWWrapper]bool) {
}
func (trw *TypeRWUnion) markWantsBytesVersion(visitedNodes map[*TypeRWWrapper]bool) {
for _, f := range trw.Fields {
f.t.MarkWantsBytesVersion(visitedNodes)
}
}
func (trw *TypeRWUnion) BeforeCodeGenerationStep1() {
}
func (trw *TypeRWUnion) BeforeCodeGenerationStep2() {
for i, f := range trw.Fields {
visitedNodes := map[*TypeRWWrapper]bool{}
f.t.trw.fillRecursiveChildren(visitedNodes)
trw.Fields[i].recursive = visitedNodes[trw.wr]
}
//if trw.wr.gen.options.Language == "cpp" { // Temporary solution to benchmark combined tl
// var nf []Field
// for _, f := range trw.Fields {
// if !f.recursive {
// nf = append(nf, f)
// panic("recursive field in union " + trw.wr.tlName.String())
//}
//}
//trw.Fields = nf
//}
}
func (trw *TypeRWUnion) fillRecursiveChildren(visitedNodes map[*TypeRWWrapper]bool) {
//if trw.wr.gen.options.Language == "cpp" { // Temporary solution to benchmark combined tl
// for _, f := range trw.Fields {
// if !f.recursive {
// f.t.FillRecursiveChildren(visitedNodes)
// }
// }
//}
}
func (trw *TypeRWUnion) IsDictKeySafe() (isSafe bool, isString bool) {
return false, false // trw.IsEnum - TODO - in the future?
}
func (trw *TypeRWUnion) CanBeBareBoxed() (canBare bool, canBoxed bool) {
return false, true
}
func (trw *TypeRWUnion) typeResettingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, ref bool) string {
return fmt.Sprintf("%s.Reset()", val)
}
func (trw *TypeRWUnion) typeRandomCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, natArgs []string, ref bool) string {
return fmt.Sprintf("%s.FillRandom(rg%s)", val, joinWithCommas(natArgs))
}
func (trw *TypeRWUnion) typeWritingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, bare bool, natArgs []string, ref bool, last bool, needError bool) string {
if bare {
panic("trying to write bare union, please report TL which caused this")
}
return wrapLastW(last, fmt.Sprintf("%s.Write%s(w %s)", val, addBare(bare), joinWithCommas(natArgs)), needError)
}
func (trw *TypeRWUnion) typeReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, bare bool, natArgs []string, ref bool, last bool) string {
if bare {
panic("trying to write bare union, please report TL which caused this")
}
return wrapLastW(last, fmt.Sprintf("%s.Read%s(w %s)", val, addBare(bare), joinWithCommas(natArgs)), true)
}
func (trw *TypeRWUnion) typeJSONEmptyCondition(bytesVersion bool, val string, ref bool) string {
return ""
}
func (trw *TypeRWUnion) typeJSONWritingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, natArgs []string, ref bool, needError bool) string {
if needError {
return fmt.Sprintf("if w, err = %s.WriteJSONOpt(newTypeNames, short, w %s); err != nil { return w, err }", val, joinWithCommas(natArgs))
} else {
return fmt.Sprintf("w = %s.WriteJSONOpt(newTypeNames, short, w %s)", val, joinWithCommas(natArgs))
}
}
func (trw *TypeRWUnion) typeJSONReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
return fmt.Sprintf("if err := %s.ReadJSONLegacy(legacyTypeNames, %s %s); err != nil { return err }", val, jvalue, joinWithCommas(natArgs))
}
func (trw *TypeRWUnion) typeJSON2ReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
return fmt.Sprintf("if err := %s.ReadJSON(legacyTypeNames, %s %s); err != nil { return err }", val, jvalue, joinWithCommas(natArgs))
}
func (trw *TypeRWUnion) HasShortFieldCollision(wr *TypeRWWrapper) bool {
//messages.peerId peer_id:int = messages.ChatId;
//messagesLong.peerId peer_id:long = messages.ChatId;
//
//messages.globalChatId#07a5893d chat_id:long = messages.ChatId;
//messagesLong.globalChatId global_id:messagesLong.GlobalId = messages.ChatId;
for _, field := range trw.Fields {
if field.t == wr {
return true
}
}
return false
}