-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_templates.go
370 lines (341 loc) · 10.5 KB
/
gen_templates.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package bebop
import (
"fmt"
"io"
"strconv"
"strings"
)
func writeLineWithTabs(w io.Writer, format string, depth int, args ...string) {
var assigner string
var receiver string
var typename string
var namespace string
var baretype string
if len(args) > 0 {
assigner = args[0]
if assigner[0] == '&' || assigner[0] == '*' {
receiver = assigner[1:]
} else {
if assigner[0] == '(' {
receiver = "(*" + assigner[1:]
} else {
receiver = "*" + assigner
}
}
}
if len(args) > 1 {
typename = args[1]
if strings.Contains(typename, ".") {
splitType := strings.Split(typename, ".")
if len(splitType) == 2 {
namespace = splitType[0]
baretype = splitType[1]
}
}
}
// add tabs
tbs := strings.Repeat("\t", depth)
format = tbs + format
format = strings.Replace(format, "\n", "\n"+tbs, -1)
format = strings.Replace(format, fillReciever, receiver, -1)
format = strings.Replace(format, fillAssigner, assigner, -1)
format = strings.Replace(format, fillTypename, typename, -1)
format = strings.Replace(format, fillBareType, baretype, -1)
format = strings.Replace(format, fillNamespace, namespace, -1)
format = strings.Replace(format, fillKey, depthName("k", depth), -1)
format = strings.Replace(format, fillValue, depthName("v", depth), -1)
fmt.Fprint(w, format+"\n")
}
const (
fillAssigner = "%ASGN"
fillReciever = "%RECV"
fillTypename = "%TYPE"
// type name without namepsace
fillBareType = "%BARETYPE"
fillNamespace = "%NAMESPACE"
fillKey = "%KNAME"
fillValue = "%VNAME"
hintSafeKey = "&safe"
fmtErrReturn = "if err != nil {\n\treturn err\n}"
fmtAddSizeToAt = "{\n\ttmp := (%ASGN)\n\tat += tmp.Size()\n}\n"
fmtAdd4PlusLenToAt = "at += 4 + len(%ASGN)"
fmtAddSizeToBodyLen = "{\n\ttmp := (%ASGN)\n\tbodyLen += tmp.Size()\n}\n"
fmtAdd4PlusLenToBodyLen = "bodyLen += 4 + len(%ASGN)"
fmtMakeType = "(%RECV), err = Make%TYPE(r)\n" + fmtErrReturn
fmtMakeNamespacedType = "(%RECV), err = %NAMESPACE.Make%BARETYPE(r)\n" + fmtErrReturn
fmtMakePrivateType = "(%RECV), err = make%TYPE(r)\n" + fmtErrReturn
fmtMake = "%ASGN, err = Make%TYPEFromBytes(buf[at:])\n"
fmtMakeNamespaced = "%ASGN, err = %NAMESPACE.Make%BARETYPEFromBytes(buf[at:])\n"
fmtMakePrivate = "%ASGN, err = make%TYPEFromBytes(buf[at:])\n"
fmtMustMake = "%ASGN = MustMake%TYPEFromBytes(buf[at:])\n"
fmtMustMakeNamespaced = "%ASGN = %NAMESPACE.MustMake%BARETYPEFromBytes(buf[at:])\n"
fmtMustMakePrivate = "%ASGN = mustMake%TYPEFromBytes(buf[at:])\n"
fmtMarshal = "(%ASGN).MarshalBebopTo(buf[at:])\n"
fmtEncode = "err = (%ASGN).EncodeBebop(w)\n"
)
var fixedSizeTypes = map[string]uint8{
typeBool: 1,
typeByte: 1,
typeUint8: 1,
typeUint16: 2,
typeInt16: 2,
typeUint32: 4,
typeInt32: 4,
typeUint64: 8,
typeInt64: 8,
typeFloat32: 4,
typeFloat64: 8,
typeGUID: 16,
typeDate: 8,
}
func fixedTitleString(typ string) string {
if typ == typeGUID {
return "GUID"
}
if len(typ) == 0 {
return typ
}
prefix := strings.ToUpper(typ[0:1])
return prefix + typ[1:]
}
func makeFormatType(namespace string, settings GenerateSettings) string {
// namespacing takes precedence over privacy-- its assumed that
// a namespaced import will not be compiled as private.
if namespace != "" {
return fmtMakeNamespacedType
}
if settings.PrivateDefinitions {
return fmtMakePrivateType
}
return fmtMakeType
}
func makeFormat(namespace string, settings GenerateSettings) string {
if namespace != "" {
return fmtMakeNamespaced
}
if settings.PrivateDefinitions {
return fmtMakePrivate
}
return fmtMake
}
func mustMakeFormat(namespace string, settings GenerateSettings) string {
if namespace != "" {
return fmtMustMakeNamespaced
}
if settings.PrivateDefinitions {
return fmtMustMakePrivate
}
return fmtMustMake
}
func (f File) typeUnmarshallers(settings GenerateSettings) map[string]string {
out := make(map[string]string)
for typ := range fixedSizeTypes {
out[typ] = "%RECV = iohelp.Read" + fixedTitleString(typ) + "(r)"
}
for _, en := range f.Enums {
out[en.Name] = "%RECV = %TYPE(iohelp.Read" + fixedTitleString(en.SimpleType) + "(r))"
}
out[typeString] = "%RECV = iohelp.ReadString(r)"
for _, st := range f.Structs {
out[st.Name] = makeFormatType(st.Namespace, settings)
}
for _, msg := range f.Messages {
out[msg.Name] = makeFormatType(msg.Namespace, settings)
}
for _, union := range f.Unions {
uout := union.typeUnmarshallers(settings)
for k, v := range uout {
out[k] = v
}
}
return out
}
func (u Union) typeUnmarshallers(settings GenerateSettings) map[string]string {
out := make(map[string]string, 1+len(u.Fields))
out[u.Name] = makeFormatType(u.Namespace, settings)
for _, ufd := range u.Fields {
if ufd.Struct != nil {
out[ufd.Struct.Name] = makeFormatType(ufd.Struct.Namespace, settings)
}
if ufd.Message != nil {
out[ufd.Message.Name] = makeFormatType(ufd.Message.Namespace, settings)
}
}
return out
}
func (f File) typeMarshallers() map[string]string {
out := make(map[string]string)
for typ := range fixedSizeTypes {
out[typ] = "iohelp.Write" + fixedTitleString(typ) + "(w, %ASGN)"
}
for _, en := range f.Enums {
out[en.Name] = "iohelp.Write" + fixedTitleString(en.SimpleType) + "(w, " + en.SimpleType + "(%ASGN))"
}
out[typeString] = "iohelp.WriteUint32(w, uint32(len(%ASGN)))\n" +
"w.Write([]byte(%ASGN))"
out[typeDate] = "if (%ASGN).IsZero() {\n" +
"\tiohelp.WriteInt64(w, 0)\n" +
"} else {\n" +
"\tiohelp.WriteInt64(w, ((%ASGN).UnixNano() / 100))\n" +
"}"
for _, st := range f.Structs {
out[st.Name] = fmtEncode + fmtErrReturn
}
for _, msg := range f.Messages {
out[msg.Name] = fmtEncode + fmtErrReturn
}
for _, union := range f.Unions {
uout := union.typeMarshallers()
for k, v := range uout {
out[k] = v
}
}
return out
}
func (u Union) typeMarshallers() map[string]string {
out := make(map[string]string)
out[u.Name] = fmtEncode + fmtErrReturn
for _, ufd := range u.Fields {
if ufd.Struct != nil {
out[ufd.Struct.Name] = fmtEncode + fmtErrReturn
}
if ufd.Message != nil {
out[ufd.Message.Name] = fmtEncode + fmtErrReturn
}
}
return out
}
func (f File) typeLengthers() map[string]string {
out := make(map[string]string)
for typ, sz := range fixedSizeTypes {
out[typ] = "bodyLen += " + strconv.Itoa(int(sz))
}
for _, en := range f.Enums {
sz := fixedSizeTypes[en.SimpleType]
out[en.Name] = "bodyLen += " + strconv.Itoa(int(sz))
}
out[typeString] = fmtAdd4PlusLenToBodyLen
for _, st := range f.Structs {
out[st.Name] = fmtAddSizeToBodyLen
}
for _, msg := range f.Messages {
out[msg.Name] = fmtAddSizeToBodyLen
}
for _, union := range f.Unions {
uout := union.typeLengthers()
for k, v := range uout {
out[k] = v
}
}
return out
}
func (u Union) typeLengthers() map[string]string {
out := make(map[string]string)
out[u.Name] = fmtAddSizeToBodyLen
for _, ufd := range u.Fields {
if ufd.Struct != nil {
out[ufd.Struct.Name] = fmtAddSizeToBodyLen
}
if ufd.Message != nil {
out[ufd.Message.Name] = fmtAddSizeToBodyLen
}
}
return out
}
func (f File) typeByters() map[string]string {
out := make(map[string]string)
for typ, sz := range fixedSizeTypes {
out[typ] = "iohelp.Write" + fixedTitleString(typ) + "Bytes(buf[at:], %ASGN)\n" +
"at += " + strconv.Itoa(int(sz))
}
for _, en := range f.Enums {
sz := fixedSizeTypes[en.SimpleType]
out[en.Name] = "iohelp.Write" + fixedTitleString(en.SimpleType) + "Bytes(buf[at:], " + en.SimpleType + "(%ASGN))\n" +
"at += " + strconv.Itoa(int(sz))
}
out[typeString] = "iohelp.WriteUint32Bytes(buf[at:], uint32(len(%ASGN)))\n" +
"copy(buf[at+4:at+4+len(%ASGN)], []byte(%ASGN))\n" + fmtAdd4PlusLenToAt
out[typeDate] = "if (%ASGN).IsZero() {\n" +
"\tiohelp.WriteInt64Bytes(buf[at:], 0)\n" +
"} else {\n" +
"\tiohelp.WriteInt64Bytes(buf[at:], ((%ASGN).UnixNano() / 100))\n" +
"}\n" +
"at += 8"
for _, st := range f.Structs {
out[st.Name] = fmtMarshal + fmtAddSizeToAt
}
for _, msg := range f.Messages {
out[msg.Name] = fmtMarshal + fmtAddSizeToAt
}
for _, union := range f.Unions {
uout := union.typeByters()
for k, v := range uout {
out[k] = v
}
}
return out
}
func (u Union) typeByters() map[string]string {
out := map[string]string{}
out[u.Name] = fmtMarshal + fmtAddSizeToAt
for _, ufd := range u.Fields {
if ufd.Struct != nil {
out[ufd.Struct.Name] = fmtMarshal + fmtAddSizeToAt
}
if ufd.Message != nil {
out[ufd.Message.Name] = fmtMarshal + fmtAddSizeToAt
}
}
return out
}
func (f File) typeByteReaders(gs GenerateSettings) map[string]string {
out := make(map[string]string)
for typ, sz := range fixedSizeTypes {
out[typ] = "%ASGN = iohelp.Read" + fixedTitleString(typ) + "Bytes(buf[at:])\n" +
"at += " + strconv.Itoa(int(sz))
}
for _, en := range f.Enums {
sz := fixedSizeTypes[en.SimpleType]
out[en.Name] = "%ASGN = %TYPE(iohelp.Read" + fixedTitleString(en.SimpleType) + "Bytes(buf[at:]))\n" +
"at += " + strconv.Itoa(int(sz))
}
stringRead := "ReadStringBytes(buf[at:])"
if gs.SharedMemoryStrings {
stringRead = "ReadStringBytesSharedMemory(buf[at:])"
}
out[typeString] = "%ASGN = iohelp.Must" + stringRead + "\n" + fmtAdd4PlusLenToAt
out["string&safe"] = "%ASGN, err = iohelp." + stringRead + "\n" + fmtErrReturn + "\n" + fmtAdd4PlusLenToAt
for _, st := range f.Structs {
out[st.Name] = mustMakeFormat(st.Namespace, gs) + fmtAddSizeToAt
out[st.Name+hintSafeKey] = makeFormat(st.Namespace, gs) + fmtErrReturn + "\n" + fmtAddSizeToAt
}
for _, msg := range f.Messages {
out[msg.Name] = mustMakeFormat(msg.Namespace, gs) + fmtAddSizeToAt
out[msg.Name+hintSafeKey] = makeFormat(msg.Namespace, gs) + fmtErrReturn + "\n" + fmtAddSizeToAt
}
for _, union := range f.Unions {
uout := union.typeByteReaders(gs)
for k, v := range uout {
out[k] = v
}
}
return out
}
func (u Union) typeByteReaders(settings GenerateSettings) map[string]string {
out := map[string]string{}
out[u.Name] = mustMakeFormat(u.Namespace, settings) + fmtAddSizeToAt
out[u.Name+hintSafeKey] = makeFormat(u.Namespace, settings) + fmtErrReturn + "\n" + fmtAddSizeToAt
for _, ufd := range u.Fields {
if ufd.Struct != nil {
st := ufd.Struct
out[st.Name] = mustMakeFormat(st.Namespace, settings) + fmtAddSizeToAt
out[st.Name+hintSafeKey] = makeFormat(st.Namespace, settings) + fmtErrReturn + "\n" + fmtAddSizeToAt
}
if ufd.Message != nil {
msg := ufd.Message
out[msg.Name] = mustMakeFormat(msg.Namespace, settings) + fmtAddSizeToAt
out[msg.Name+hintSafeKey] = makeFormat(msg.Namespace, settings) + fmtErrReturn + "\n" + fmtAddSizeToAt
}
}
return out
}