forked from fgrosse/goldi
/
generator.go
215 lines (181 loc) · 5.56 KB
/
generator.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
// The Generator is used to generate compilable go code from a yaml configuration
type Generator struct {
Config Config
Debug bool
}
// NewGenerator creates a new Generator instance
func NewGenerator(config Config) *Generator {
return &Generator{
Config: config,
Debug: false,
}
}
// Generate reads a yaml type configuration from the `input` and writes the corresponding go code to the `output`.
func (g *Generator) Generate(input io.Reader, output io.Writer) error {
g.logVerbose("Generating code from input %q with output package %q", g.Config.InputPath, g.Config.Package)
conf, err := g.parseInput(input)
if err != nil {
return fmt.Errorf("could not parse type definition: %s", err)
}
err = conf.Validate()
if err != nil {
return err
}
if g.Config.OutputPath != "" {
g.generateGoGenerateLine(output)
}
fmt.Fprintf(output, "package %s\n\n", g.Config.PackageName())
g.generateImports(conf, output)
g.generateGoldiGenComment(output)
g.generateTypeRegistrationFunction(conf, output)
// TODO: once done check if the output is valid go code
return nil
}
func (g *Generator) parseInput(input io.Reader) (*TypesConfiguration, error) {
g.logVerbose("Parsing input..")
inputData, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
inputData = g.sanitizeInput(inputData)
var config TypesConfiguration
err = yaml.Unmarshal(inputData, &config)
captureStrings(&config)
return &config, err
}
func (g *Generator) sanitizeInput(input []byte) []byte {
g.logVerbose("Sanitizing input..")
var sanitizedInput = newSanitizer()
line := &bytes.Buffer{}
lineBeginning := true
for _, c := range input {
switch c {
case '\n':
if strings.TrimSpace(line.String()) != "" {
sanitizedInput.Write(append(line.Bytes(), '\n'))
line.Reset()
lineBeginning = true
}
case '\t':
if lineBeginning {
line.WriteString(" ")
} else {
line.WriteByte(c)
}
case ' ':
line.WriteByte(c)
default:
lineBeginning = false
line.WriteByte(c)
}
}
sanitizedInput.Write(line.Bytes())
s := sanitizedInput.Bytes()
g.logVerbose("Sanitized input is:\n%s", string(s))
return s
}
// captureStrings reverts any escape sequences that were introduced during the input sanitizing.
func captureStrings(config *TypesConfiguration) {
unescape := func(input string) string {
output := strings.Replace(input, `\@`, `@`, -1)
return output
}
for id, t := range config.Types {
t.TypeName = unescape(t.TypeName)
t.FuncName = unescape(t.FuncName)
t.FactoryMethod = unescape(t.FactoryMethod)
t.AliasForType = unescape(t.AliasForType)
for i, s := range t.Configurator {
t.Configurator[i] = unescape(s)
}
for i, a := range t.RawArguments {
s, isString := a.(string)
if !isString {
continue
}
t.RawArguments[i] = unescape(s)
}
for i, a := range t.RawArgumentsShort {
s, isString := a.(string)
if !isString {
continue
}
t.RawArgumentsShort[i] = unescape(s)
}
config.Types[id] = t
}
}
func (g *Generator) generateGoGenerateLine(output io.Writer) {
fmt.Fprintf(output, "//go:generate goldigen --in %q --out %q --package %s --function %s --overwrite --nointeraction\n",
g.Config.InputName(), g.Config.OutputName(), g.Config.Package, g.Config.FunctionName,
)
}
func (g *Generator) generateImports(conf *TypesConfiguration, output io.Writer) {
g.logVerbose("Generating import packages (ignoring %q)", g.Config.Package)
packages := conf.Packages("github.com/fgrosse/goldi")
fmt.Fprint(output, "import (\n")
for _, pkg := range packages {
if pkg != "" && pkg != g.Config.Package {
g.logVerbose("Detected new import package %q", pkg)
fmt.Fprintf(output, "\t%q\n", pkg)
}
}
fmt.Fprint(output, ")\n\n")
}
func (g *Generator) generateGoldiGenComment(output io.Writer) {
fmt.Fprintf(output, "// %s registers all types that have been defined in the file %q\n", g.Config.FunctionName, g.Config.InputName())
fmt.Fprintf(output, "//\n")
fmt.Fprintf(output, "// DO NOT EDIT THIS FILE: it has been generated by goldigen v%s.\n", Version)
fmt.Fprintf(output, "// It is however good practice to put this file under version control.\n")
fmt.Fprintf(output, "// See https://github.com/fgrosse/goldi for what is going on here.\n")
}
func (g *Generator) generateTypeRegistrationFunction(conf *TypesConfiguration, output io.Writer) {
fmt.Fprintf(output, "func %s(types goldi.TypeRegistry) {\n", g.Config.FunctionName)
typeIDs := make([]string, len(conf.Types))
i := 0
maxIDLength := 0
for typeID := range conf.Types {
typeIDs[i] = typeID
i++
if len(typeID) > maxIDLength {
maxIDLength = len(typeID)
}
}
sort.Strings(typeIDs)
if len(conf.Types) == 1 {
typeID := typeIDs[0]
typeDef := conf.Types[typeID]
fmt.Fprint(output, "\t")
fmt.Fprintf(output, "types.Register(%q, %s)", typeID, FactoryCode(typeDef, g.Config.Package))
fmt.Fprint(output, "\n")
} else {
fmt.Fprint(output, "\ttypes.RegisterAll(map[string]goldi.TypeFactory{\n")
for _, typeID := range typeIDs {
typeDef := conf.Types[typeID]
spaces := strings.Repeat(" ", maxIDLength-len(typeID))
fmt.Fprintf(output, "\t\t%q: %s%s,\n", typeID, spaces, FactoryCode(typeDef, g.Config.Package))
}
fmt.Fprint(output, "\t})\n")
}
// close the outmost surrounding function
fmt.Fprint(output, "}\n")
}
func (g *Generator) logVerbose(message string, args ...interface{}) {
if g.Debug {
fmt.Fprintf(os.Stderr, message+"\n", args...)
}
}
func (g *Generator) logWarn(message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, message+"\n", args...)
}