-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_struct.go
230 lines (217 loc) · 7.62 KB
/
gen_struct.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
package bebop
import (
"io"
"strconv"
"strings"
"github.com/200sc/bebop/iohelp"
)
func (st Struct) generateTypeDefinition(w *iohelp.ErrorWriter, settings GenerateSettings) {
writeOpCode(w, st.Name, st.OpCode, settings)
writeRecordAssertion(w, st.Name, settings)
writeComment(w, 0, st.Comment, settings)
writeGoStructDef(w, st.Name, settings)
for _, fd := range st.Fields {
writeFieldDefinition(fd, w, st.ReadOnly, false, settings)
}
writeCloseBlock(w)
}
func (st Struct) generateMarshalBebopTo(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
if settings.AlwaysUsePointerReceivers {
writeLine(w, "func (bbp *%s) MarshalBebopTo(buf []byte) int {", exposedName)
} else {
writeLine(w, "func (bbp %s) MarshalBebopTo(buf []byte) int {", exposedName)
}
startAt := "0"
if len(st.Fields) == 0 {
writeLine(w, "\treturn "+startAt)
writeCloseBlock(w)
return
}
writeLine(w, "\tat := "+startAt)
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
writeFieldByter("bbp."+name, fd.FieldType, w, settings, 1)
}
writeLine(w, "\treturn at")
writeCloseBlock(w)
}
func (st Struct) generateUnmarshalBebop(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
writeLine(w, "func (bbp *%s) UnmarshalBebop(buf []byte) (err error) {", exposedName)
if len(st.Fields) > 0 {
writeLine(w, "\tat := 0")
}
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
writeFieldReadByter("bbp."+name, fd.FieldType, w, settings, 1, true)
}
writeLine(w, "\treturn nil")
writeCloseBlock(w)
}
func (st Struct) generateMustUnmarshalBebop(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
writeLine(w, "func (bbp *%s) MustUnmarshalBebop(buf []byte) {", exposedName)
if len(st.Fields) > 0 {
writeLine(w, "\tat := 0")
}
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
writeFieldReadByter("bbp."+name, fd.FieldType, w, settings, 1, false)
}
writeCloseBlock(w)
}
func (st Struct) generateEncodeBebop(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
*settings.isFirstTopLength = true
if settings.AlwaysUsePointerReceivers {
writeLine(w, "func (bbp *%s) EncodeBebop(iow io.Writer) (err error) {", exposedName)
} else {
writeLine(w, "func (bbp %s) EncodeBebop(iow io.Writer) (err error) {", exposedName)
}
if len(st.Fields) == 0 {
writeLine(w, "\treturn nil")
} else {
writeLine(w, "\tw := iohelp.NewErrorWriter(iow)")
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
writeFieldMarshaller("bbp."+name, fd.FieldType, w, settings, 1)
}
writeLine(w, "\treturn w.Err")
}
writeCloseBlock(w)
}
func (st Struct) generateDecodeBebop(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
*settings.isFirstTopLength = true
writeLine(w, "func (bbp *%s) DecodeBebop(ior io.Reader) (err error) {", exposedName)
if len(st.Fields) == 0 {
writeLine(w, "\treturn nil")
} else {
writeLine(w, "\tr := iohelp.NewErrorReader(ior)")
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
writeStructFieldUnmarshaller("&bbp."+name, fd.FieldType, w, settings, 1)
}
writeLine(w, "\treturn r.Err")
}
writeCloseBlock(w)
}
func (st Struct) generateSize(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(st.Name, settings)
if settings.AlwaysUsePointerReceivers {
writeLine(w, "func (bbp *%s) Size() int {", exposedName)
} else {
writeLine(w, "func (bbp %s) Size() int {", exposedName)
}
if len(st.Fields) == 0 {
writeLine(w, "\treturn 0")
} else {
writeLine(w, "\tbodyLen := 0")
for _, fd := range st.Fields {
name := exposeName(fd.Name, settings)
if st.ReadOnly {
name = unexposeName(fd.Name)
}
name = "bbp." + name
writeFieldBodyCount(name, fd.FieldType, w, settings, 1)
}
writeLine(w, "\treturn bodyLen")
}
writeCloseBlock(w)
}
func (st Struct) generateReadOnlyGetters(w *iohelp.ErrorWriter, settings GenerateSettings) {
// TODO: slices are not read only, we need to return a copy.
exposedName := exposeName(st.Name, settings)
for _, fd := range st.Fields {
if settings.AlwaysUsePointerReceivers {
writeLine(w, "func (bbp *%s) Get%s() %s {", exposedName, exposeName(fd.Name, settings), fd.FieldType.goString(settings))
} else {
writeLine(w, "func (bbp %s) Get%s() %s {", exposedName, exposeName(fd.Name, settings), fd.FieldType.goString(settings))
}
writeLine(w, "\treturn bbp.%s", unexposeName(fd.Name))
writeCloseBlock(w)
}
newFmt := exposeName("New", settings)
writeLine(w, "func %s%s(", newFmt, exposedName)
for _, fd := range st.Fields {
writeLine(w, "\t\t%s %s,", unexposeName(fd.Name), fd.FieldType.goString(settings))
}
writeLine(w, ") %s {", exposedName)
writeLine(w, "\treturn %s{", exposedName)
for _, fd := range st.Fields {
writeLine(w, "\t\t%s: %s,", unexposeName(fd.Name), unexposeName(fd.Name))
}
writeLine(w, "\t}")
writeCloseBlock(w)
}
// Generate writes a .go struct definition out to w.
func (st Struct) Generate(w io.Writer, settings GenerateSettings) {
ew := iohelp.NewErrorWriter(w)
st.generateTypeDefinition(ew, settings)
st.generateMarshalBebopTo(ew, settings)
st.generateUnmarshalBebop(ew, settings)
if settings.GenerateUnsafeMethods {
st.generateMustUnmarshalBebop(ew, settings)
}
st.generateEncodeBebop(ew, settings)
st.generateDecodeBebop(ew, settings)
st.generateSize(ew, settings)
isEmpty := len(st.Fields) == 0
writeWrappers(ew, st.Name, isEmpty, settings)
if st.ReadOnly {
st.generateReadOnlyGetters(ew, settings)
}
}
func writeStructFieldUnmarshaller(name string, typ FieldType, w *iohelp.ErrorWriter, settings GenerateSettings, depth int) {
iName := "i" + strconv.Itoa(depth)
if typ.Array != nil {
writeLineWithTabs(w, "%RECV = make([]%TYPE, iohelp.ReadUint32(r))", depth, name, typ.Array.goString(settings))
if typ.Array.Simple == typeByte {
writeLineWithTabs(w, "r.Read(%RECV)", depth, name)
} else {
writeLineWithTabs(w, "for "+iName+" := range %RECV {", depth, name)
name = "&(" + name[1:] + "[" + iName + "])"
writeStructFieldUnmarshaller(name, *typ.Array, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
}
} else if typ.Map != nil {
lnName := "ln" + strconv.Itoa(depth)
if *settings.isFirstTopLength && depth == 1 {
writeLineWithTabs(w, lnName+" := iohelp.ReadUint32(r)", depth)
*settings.isFirstTopLength = false
} else if depth == 1 {
writeLineWithTabs(w, lnName+" = iohelp.ReadUint32(r)", depth)
} else {
writeLineWithTabs(w, lnName+" := iohelp.ReadUint32(r)", depth)
}
writeLineWithTabs(w, "%RECV = make(%TYPE, "+lnName+")", depth, name, typ.Map.goString(settings))
writeLineWithTabs(w, "for "+iName+" := uint32(0); "+iName+" < "+lnName+"; "+iName+"++ {", depth, name)
ln := getLineWithTabs(settings.typeUnmarshallers[typ.Map.Key], depth+1, "&"+depthName("k", depth))
w.SafeWrite([]byte(strings.Replace(ln, "=", ":=", 1)))
name = "&(" + name[1:] + "[" + depthName("k", depth) + "])"
writeStructFieldUnmarshaller(name, typ.Map.Value, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else {
simpleTyp := typ.Simple
if alias, ok := settings.importTypeAliases[simpleTyp]; ok {
simpleTyp = alias
}
writeLineWithTabs(w, settings.typeUnmarshallers[simpleTyp], depth, name, typ.goString(settings))
}
}