-
Notifications
You must be signed in to change notification settings - Fork 11
/
hasgo.go
258 lines (235 loc) · 6.21 KB
/
hasgo.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
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"golang.org/x/tools/go/packages"
"io/ioutil"
"os"
"sort"
"strings"
)
const (
elementTypeSymbol = "ElementType"
sliceTypeSymbol = "SliceType"
sliceSliceTypeSymbol = "SliceSliceType" // todo: generalize for N slices
)
var (
unitType = flag.String("T", "", "Type for which to generate data")
sliceType = flag.String("S", "", "Corresponding Slice Type for T")
packag = flag.String("P", "types", "Package for which to generate")
ofilename = flag.String("N", "", "name for the generated file (_hasgo.go will be appended)")
numberTypes = map[string]struct{}{
"int": {},
"int32": {},
"int64": {},
"uint": {},
"uint32": {},
"uint64": {},
"float": {},
"float32": {},
"float64": {},
}
stringTypes = map[string]struct{}{
"string": {},
}
)
func check(e error) {
if e != nil {
panic(e)
}
}
// the Type / sliceType for which we are generating data..
type symbols struct {
T, ST string
}
type generator struct {
buf bytes.Buffer // accumulate the output
pkg string // package in which to place the generated code.
imports map[string]struct{} // the imports that the generator needs to add
}
// currently hasgo works like this:
// once the generator is trigger, I scan all files in the named packages
// I generate one hasgo file with functions
// all the functions get stored here
// I write out _once_, collecting all data
// Importantly, hasgo should trigger only once.. I should figure out how to do so
func main() {
flag.Parse()
fmt.Printf("type: %v - slice: %v\n", *unitType, *sliceType)
sym := symbols{*unitType, *sliceType}
g := generator{
imports: map[string]struct{}{},
pkg: *packag,
}
g.parsePackage(os.Args, nil)
// stringer prints everything in one file. This might be bad.
// but let's roll with it for now :-)
g.generate(sym)
filename := fmt.Sprintf("%v_hasgo.go", *sliceType)
if ofilename != nil && *ofilename != "" {
filename = fmt.Sprintf("%v_hasgo.go", *ofilename)
}
err := ioutil.WriteFile(filename, g.format(), 0644)
if err != nil {
panic(err)
}
}
// is the function valid for the type?
func validFunction(function, T string) bool {
domains, ok := funcDomains[function]
if !ok {
return false
}
for _, d := range domains {
switch d {
case ForNumbers:
if _, ok := numberTypes[T]; ok {
return true
}
break
case ForStrings:
if _, ok := stringTypes[T]; ok {
return true
}
break
case ForStructs:
// everything that is not a string or number we will consider a struct!
if _, ok := numberTypes[T]; !ok {
if _, ok := stringTypes[T]; !ok {
return true
}
}
break
}
}
return false
}
// write the data for the generator
func (g *generator) generate(s symbols) {
funcs := []string{}
for function := range hasgoTemplates {
funcs = append(funcs, function)
}
sort.Strings(funcs)
for _, function := range funcs {
template := hasgoTemplates[function]
if !validFunction(function, s.T) {
continue
}
template, imports := extractImports(template)
g.addImports(imports)
g.printf("// =============== %v =================\n", function)
g.print(generify(template, s))
g.newline()
}
}
// extracts the imports and removes them from the template
// we assume that the go code is correctly formatted!
func extractImports(template string) (outtemp string, imports []string) {
// split template into lines
// check for import statements in line
lines := strings.Split(template, "\n")
nonImportLines := []string{}
for i := 0; i < len(lines); {
line := lines[i]
if strings.Contains(line, "import") {
// determine if it's a single import or multiple..
if strings.Contains(line, "(") {
// implement
i++
line := lines[i]
// check end condition
for !strings.Contains(line, ")") {
imports = append(imports, line)
i++
line = lines[i]
}
i++
continue
} else {
line = strings.TrimSpace(line)[len("import"):]
imports = append(imports, line)
i++
continue
}
}
nonImportLines = append(nonImportLines, line)
i++
}
return strings.Join(nonImportLines, "\n"), imports
}
// replace the placeholders by the correct symbols
func generify(template string, sym symbols) (out string) {
out = template
out = strings.Replace(out, elementTypeSymbol, sym.T, -1)
out = strings.Replace(out, sliceSliceTypeSymbol, "[][]"+sym.T, -1)
out = strings.Replace(out, sliceTypeSymbol, sym.ST, -1)
return
}
func (g *generator) printf(format string, args ...interface{}) {
fmt.Fprintf(&g.buf, format, args...)
}
func (g *generator) print(s string) {
fmt.Fprint(&g.buf, s)
}
func (g *generator) newline() {
g.printf("\n")
}
func (g *generator) addImports(imports []string) {
for _, imp := range imports {
// sanitize:
sane := strings.TrimSpace(imp)
g.imports[sane] = struct{}{}
}
}
// analyze the package
func (g *generator) parsePackage(patterns []string, tags []string) {
cfg := &packages.Config{
Mode: packages.LoadSyntax,
Tests: false,
BuildFlags: []string{fmt.Sprintf("-tags=%s", strings.Join(tags, " "))},
}
pkgs, err := packages.Load(cfg, patterns...)
check(err)
if len(pkgs) == 0 {
panic("not enough packages")
}
fmt.Printf("packages %v\n", pkgs)
g.addPackage(pkgs[0])
}
// add a package to the generator
func (g *generator) addPackage(pkg *packages.Package) {
fmt.Println("adding files..")
for _, file := range pkg.Syntax {
fmt.Printf("file %v\n", file)
}
}
// return all unique imports
func (g *generator) listImports() (out []string) {
for k := range g.imports {
out = append(out, k)
}
return
}
// print the formatted source code
// adds the package declaration + imports
func (g *generator) format() []byte {
code := "// Package " + g.pkg + " contains code generated by hasgo. [DO NOT EDIT!]" +
"\n" +
"package " + g.pkg + "\n"
if imports := g.listImports(); len(imports) > 0 {
code += "import (\n"
code += strings.Join(imports, "\n") + "\n)\n"
}
// add our generated functions
src := append([]byte(code), g.buf.Bytes()...)
src, err := format.Source(src)
if err != nil {
// should never happen. But, during development, might.
fmt.Println("Printing code without formatting")
return append([]byte(code), g.buf.Bytes()...)
}
return src
}