/
gen.go
336 lines (279 loc) · 8.23 KB
/
gen.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
package generator
import (
"bytes"
"errors"
"fmt"
"go/format"
"io"
"log"
"os"
"path"
"sort"
"strings"
"github.com/iancoleman/strcase"
"golang.org/x/exp/maps"
"github.com/woven-planet/go-zserio/internal/ast"
"github.com/woven-planet/go-zserio/internal/model"
)
type options struct {
rootPackage string
doNotFormatSource bool
outputPackage string
outputToStdout bool
EmitSQLSupport bool
maxPathLength int
}
const (
DefaultMaxPathLength = 260 // Hard limit on Windows
MinFileNameLength = 2
FileSuffixLength = 5 // Leave space for at least single-digit index if needed
FileSuffix = ".go"
)
// Option sets a configuration option
type Option func(opts *options)
// DoNotFormatSource indicates generate Go source should not be formatted
func DoNotFormatSource(opts *options) {
opts.doNotFormatSource = true
}
// EmitSQLSupport tells the generator to produce support for storing enum
// types in a SQL database.
func EmitSQLSupport(opt *options) {
opt.EmitSQLSupport = true
}
// PathLengthLimiter indicates that generated source file paths should be clipped to the given length
type PathLengthLimiter struct {
MaxPathLength int
}
func (limiter PathLengthLimiter) LimitPathLength(opts *options) {
opts.maxPathLength = limiter.MaxPathLength
}
type data map[string]any
// uniquePaths keeps track of all codes files that have been generated so far and adds an "_idx" suffix if the same path appears again
var uniquePaths map[string]int
func Generate(m *model.Model, rootPath, rootPackage, outputPackage string, flags ...Option) error {
opts := &options{
outputToStdout: rootPath == "-",
rootPackage: rootPackage,
}
for _, f := range flags {
f(opts)
}
if opts.outputToStdout {
pkg, ok := m.Packages[outputPackage]
if !ok {
var known []string
for name := range m.Packages {
known = append(known, name)
}
sort.Strings(known)
return fmt.Errorf("%q not found, only know about %q", outputPackage, known)
}
return generatePackage(rootPath, pkg, opts)
}
for _, pkg := range m.Packages {
if err := generatePackage(rootPath, pkg, opts); err != nil {
return err
}
}
return nil
}
func executeTemplate(out io.Writer, tmpl string, data data) error {
templ := templates.Lookup(tmpl)
if templ == nil {
return fmt.Errorf("template %q is missing", tmpl)
}
return templ.Execute(out, data)
}
func writeToFile(code []byte, rootPath, zserioPkg, fn string, opts *options) (retErr error) {
ids := strings.Split(strings.ToLower(zserioPkg), ".")
outputDir := path.Join(append([]string{rootPath}, ids...)...)
outputFile, err := assembleUniqueFilePath(outputDir, fn, opts.maxPathLength)
if err != nil {
return fmt.Errorf("file path: %w", err)
}
log.Printf("Writing %s\n", outputFile)
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
f, err := os.OpenFile(outputFile, FileOpenFlags, 0644)
if err != nil {
return fmt.Errorf("open: %w", err)
}
defer func() {
if retErr == nil {
return
}
_ = os.Remove(outputFile)
}()
defer f.Close()
return writeGoSource(f, code, opts.doNotFormatSource)
}
func assembleUniqueFilePath(outputDir string, fn string, maxPathLength int) (string, error) {
if uniquePaths == nil {
uniquePaths = make(map[string]int)
}
if minLength := len(outputDir) + FileSuffixLength + MinFileNameLength; maxPathLength < minLength {
return "", fmt.Errorf("maximum path length %d is too short, at least %d required to fit output directory", maxPathLength, minLength)
}
outputFile := path.Join(outputDir, fn)
if maxPathLength > 0 {
maxLength := maxPathLength - FileSuffixLength
if len(outputFile) > maxLength {
outputFile = outputFile[:maxLength]
if strings.Contains(outputFile, "_") && strings.LastIndex(outputFile, "_") > strings.LastIndex(outputFile, "/") {
outputFile = outputFile[:strings.LastIndex(outputFile, "_")]
}
}
}
if count, ok := uniquePaths[outputFile]; ok {
uniquePaths[outputFile]++
return fmt.Sprintf("%s_%d%s", outputFile, count, FileSuffix), nil
} else {
uniquePaths[outputFile] = 1
return fmt.Sprintf("%s%s", outputFile, FileSuffix), nil
}
}
func writeGoSource(w io.Writer, code []byte, doNotFormatCode bool) error {
if doNotFormatCode {
if _, err := w.Write(code); err != nil {
return fmt.Errorf("write: %w", err)
}
return nil
}
code, err := StripImports(code)
if err != nil {
return fmt.Errorf("strip imports: %w", err)
}
formatted, err := format.Source(code)
if err != nil {
log.Println(string(code))
return fmt.Errorf("format: %w", err)
}
if _, err := w.Write(formatted); err != nil {
return fmt.Errorf("write: %w", err)
}
return nil
}
// output allows us easier passing of arguments to write packages.
type output struct {
name string // the name of the data structure that is output
template string // template file name
data data
withPreamble bool
options *options
}
func newOutput(name, template string, templateData data, withPreamble bool, opts *options) output {
newData := data{
"options": opts,
}
maps.Copy(newData, templateData)
return output{
name: name,
template: template,
data: newData,
withPreamble: withPreamble,
options: opts,
}
}
func newTypeOutput(astType any, d data, withPreamble bool, opts *options) output {
var name, typeName string
switch t := astType.(type) {
case *ast.Enum:
name = t.Name
typeName = "enum"
case *ast.Union:
name = t.Name
typeName = "union"
case *ast.BitmaskType:
name = t.Name
typeName = "bitmask"
case *ast.Choice:
name = t.Name
typeName = "choice"
case *ast.Struct:
name = t.Name
typeName = "struct"
default:
panic(fmt.Sprintf("%#T is unsupported", t))
}
templateData := make(data, len(d)+1)
for k, v := range d {
templateData[k] = v
}
templateData[typeName] = astType
return newOutput(strcase.ToSnake(name), typeName+".go.tmpl", templateData, withPreamble, opts)
}
func (o *output) executeTemplate(w io.Writer) error {
if o.withPreamble {
if err := executeTemplate(w, "preamble.go.tmpl", o.data); err != nil {
return fmt.Errorf("generate preamble: %w", err)
}
}
return executeTemplate(w, o.template, o.data)
}
func newOutputs(pkg *ast.Package, rootPackage string, opts *options) []output {
data := data{
"pkg": pkg,
"rootPackage": rootPackage,
}
var outs []output
for _, t := range pkg.Enums {
outs = append(outs, newTypeOutput(t, data, !opts.outputToStdout, opts))
}
for _, t := range pkg.Unions {
if len(t.TemplateParameters) > 0 {
// We only need to generate source for instantiated templates
continue
}
outs = append(outs, newTypeOutput(t, data, !opts.outputToStdout, opts))
}
for _, t := range pkg.Bitmasks {
outs = append(outs, newTypeOutput(t, data, !opts.outputToStdout, opts))
}
for _, t := range pkg.Choices {
if len(t.TemplateParameters) > 0 {
// We only need to generate source for instantiated templates
continue
}
outs = append(outs, newTypeOutput(t, data, !opts.outputToStdout, opts))
}
for _, t := range pkg.Structs {
if len(t.TemplateParameters) > 0 {
// We only need to generate source for instantiated templates
continue
}
outs = append(outs, newTypeOutput(t, data, !opts.outputToStdout, opts))
}
sort.Slice(outs, func(i, j int) bool {
if outs[i].template == outs[j].template {
return outs[i].name > outs[j].name
}
return outs[i].template > outs[j].template
})
return append([]output{newOutput("pkg", "package.go.tmpl", data, true, opts)}, outs...)
}
func generatePackage(rootPath string, pkg *ast.Package, opts *options) error {
if opts.rootPackage == "" {
return errors.New("BUG: root package needs to be specified")
}
outs := newOutputs(pkg, opts.rootPackage, opts)
if opts.outputToStdout {
var out bytes.Buffer
for _, o := range outs {
if err := o.executeTemplate(&out); err != nil {
return fmt.Errorf("generate %s: %w", o.name, err)
}
}
return writeGoSource(os.Stdout, out.Bytes(), opts.doNotFormatSource)
}
for _, o := range outs {
var out bytes.Buffer
if err := o.executeTemplate(&out); err != nil {
return fmt.Errorf("generate %s: %w", o.name, err)
}
if err := writeToFile(out.Bytes(), rootPath, pkg.Name, o.name, opts); err != nil {
return fmt.Errorf("write %s: %w", o.name, err)
}
}
return nil
}