-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.go
285 lines (268 loc) · 9.41 KB
/
code.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
package stroo
import (
"errors"
"fmt"
"log"
"strings"
"text/template"
"time"
)
type Code struct {
CodeConfig
Imports []string
PackageInfo *PackageInfo
keeper map[string]interface{} // template authors keeps data in here, key-value, as they need
tmpl *template.Template // reference to template, so we don't pass it as parameter
}
var Root *Code
func New(
info *PackageInfo,
config CodeConfig,
tmpl *template.Template,
) (*Code, error) {
result := &Code{
PackageInfo: info,
CodeConfig: config,
}
// reset keeper
result.ResetKeeper()
// add imports (they get cleared by importer tool)
for _, imprt := range info.Imports {
result.AddToImports(imprt.Path)
}
if tmpl != nil {
result.tmpl = tmpl
}
Root = result // set global, there is no other way to make template aware of the "common"
return result, nil
}
// getters for config - to be accessible from template
func (c *Code) SelectedType() string { return c.CodeConfig.SelectedType }
func (c *Code) TestMode() bool { return c.CodeConfig.TestMode }
func (c *Code) DebugPrint() bool { return c.CodeConfig.DebugPrint }
func (c *Code) Serve() bool { return c.CodeConfig.Serve }
func (c *Code) TemplateFile() string { return c.CodeConfig.TemplateFile }
func (c *Code) OutputFile() string { return c.CodeConfig.OutputFile }
func (c *Code) SelectedPeerType() string { return c.CodeConfig.SelectedPeerType }
func (c *Code) Tmpl() *template.Template { return c.tmpl } // can't really say what's the usage, but we're open
func (c *Code) Keeper() map[string]interface{} { return c.keeper }
func (c *Code) ResetKeeper() { c.keeper = make(map[string]interface{}) }
func (c *Code) PackageName() string { return c.PackageInfo.Name }
// gets a struct declaration by it's name
func (c *Code) StructByKey(key string) (*TypeInfo, error) {
result := c.PackageInfo.Types.Extract(key)
if result == nil {
if c.DebugPrint() {
log.Printf("error looking for %q into types", key)
}
knowntypes := ""
for _, t := range c.PackageInfo.Types {
knowntypes += "`" + t.Name + "` " + t.Kind + "\n"
}
return nil, fmt.Errorf("error looking for %q into known types :\n %s\n", key, knowntypes)
}
return result, nil
}
// returns true if the key exist and will overwrite
func (c *Code) Store(key string, value interface{}) error {
_, has := c.keeper[key]
if has {
log.Printf("%q was overwritten in store", key)
}
c.keeper[key] = value
return nil
}
// retrieves the entire "storage" at template dev disposal
func (c *Code) Retrieve(key string) (interface{}, error) {
value, has := c.keeper[key]
if !has {
return nil, fmt.Errorf("error : attempt to retrieve %q - was not found", key)
}
return value, nil
}
func (c *Code) HasInStore(key string) bool {
_, has := c.keeper[key]
if c.CodeConfig.DebugPrint {
log.Printf("Has in store %q = %t", key, has)
}
return has
}
func (c *Code) AddToImports(imp string) string {
if imp == "" {
// dummy fix : don't allow empty imports
return ""
}
for _, imprt := range c.Imports {
if imprt == imp {
// dummy fix : already has it
return ""
}
}
c.Imports = append(c.Imports, imp)
return ""
}
// check if a kind has a method called the same as the template being declared
func (c *Code) Implements(fieldInfo TypeInfo) (bool, error) {
if c.CodeConfig.TemplateName == "" {
return false, errors.New("you haven't called Declare(methodName) to allow replacing existing generated code")
}
if fieldInfo.Package == "" {
nt, err := c.StructByKey(fieldInfo.Kind)
if nt == nil || err != nil {
log.Printf("lookup error : %v", err)
return false, err
}
} else {
if fieldInfo.PackagePath != "" {
log.Printf("package path : %q", fieldInfo.PackagePath)
}
log.Printf("we should load : %q and lookup for %q", fieldInfo.Package, fieldInfo.Kind)
// we have to load the package
loadedPackage, err := LoadPackage(fieldInfo.Package)
if err != nil {
log.Printf("load error : %v", err)
return false, err
}
codeBuilder := DefaultAnalyzer()
command := NewCommand(codeBuilder)
if err := command.Analyse(codeBuilder, loadedPackage); err != nil {
return false, fmt.Errorf("error analyzing package : %v", err)
}
//log.Printf("package loaded :\n %#v\n", command.Result)
}
// by default : assume it doens't implements
return false, nil
}
// this should be called to allow the generator to know which kind of methods we're generating
func (c *Code) Declare(name string) error {
if name == "" {
//log.Printf("error : cannot declare empty template name (e.g.`String` for Stringer interface implementation)")
return errors.New("error : cannot declare empty template name (e.g.`String` for Stringer interface implementation)")
}
if c.CodeConfig.SelectedType == "" {
return errors.New("error : selected type is empty")
}
c.CodeConfig.TemplateName = name
c.keeper[name+c.CodeConfig.SelectedType] = "" // set it to empty in case of self reference, so template will exit
return nil
}
// checker for recurse generated
func (c *Code) HasNotGenerated(pkg, kind string) (bool, error) {
if c == nil {
return false, errors.New("impossible : code was not passed here (.Root missing ???)")
}
if c.CodeConfig.TemplateName == "" {
//log.Printf("you haven't called Declare(methodName) to allow replacing existing generated code")
return false, errors.New("you haven't called Declare(methodName) to allow replacing existing generated code")
}
if pkg == "" {
//log.Printf("HasNotGenerated : empty package on kind %q", kind)
return false, nil
}
if pkg != c.PackageInfo.Name {
//log.Printf("HasNotGenerated : different packages %q != %q", pkg, c.PackageInfo.Name)
return false, nil
}
// check if we're going to allow call to RecurseGenerate (optim calls)
nt, err := c.StructByKey(kind)
if nt == nil || err != nil {
return false, err
}
if nt.IsImported {
//log.Printf("HasNotGenerated : %q in package %q it's maked imported", kind, pkg)
return false, nil
}
if nt.Package != pkg {
//log.Printf("HasNotGenerated : %q in package %q it's a different package %q", kind, pkg, nt.Package)
return false, nil
}
if IsBasic(nt.Kind) {
//log.Printf("HasNotGenerated : call for basic kind %q", nt.Kind)
return false, nil
}
// finally we deliver result
entity := c.CodeConfig.TemplateName + kind
_, has := c.keeper[entity]
//log.Printf("HasNotGenerated : %q %t", entity, has)
return !has, nil
}
// uses the template name to apply the template recursively
// it's useful for replacing the code in existing generated files
func (c *Code) RecurseGenerate(pkg, kind string) error {
if c.CodeConfig.TemplateName == "" {
return errors.New("you haven't called Declare(methodName) to allow replacing existing generated code")
}
if pkg == "" {
log.Printf("RecurseGenerate : empty package on kind %q", kind)
return nil
}
if pkg != c.PackageInfo.Name {
log.Printf("RecurseGenerate : different packages %q != %q", pkg, c.PackageInfo.Name)
return nil
}
entity := c.CodeConfig.TemplateName + kind
if c.CodeConfig.DebugPrint {
log.Printf("RecurseGenerate : processing %q %q ", c.CodeConfig.TemplateName, kind)
}
// already has it
if _, has := c.keeper[entity]; has {
if c.CodeConfig.DebugPrint {
log.Printf("RecurseGenerate : %q already stored.", kind)
}
log.Printf("RecurseGenerate : %q ALREADY stored.", kind)
return errors.New("RecurseGenerate : `" + kind + "` already stored. you are not checking that yourself?")
}
nt, err := c.StructByKey(kind)
if nt == nil || err != nil {
return err
}
if nt.IsImported {
log.Printf("RecurseGenerate : %q in package %q it's imported", kind, pkg)
return fmt.Errorf("RecurseGenerate : %q in package %q it's imported", kind, pkg)
}
if nt.Package != pkg {
log.Printf("RecurseGenerate : %q in package %q it's a different pacakge %q", kind, pkg, nt.Package)
return fmt.Errorf("RecurseGenerate : %q in package %q it's a different package %q", kind, pkg, nt.Package)
}
if IsBasic(nt.Kind) {
log.Printf("RecurseGenerate : call for basic kind %q", nt.Kind)
return errors.New("RecurseGenerate : don't recurse call for basic kind")
}
//log.Printf("RecurseGenerate : %q for kind %q generating in package %q", entity, nt.Kind, pkg)
var buf strings.Builder
c.keeper[entity] = "" // mark it empty
err = c.tmpl.ExecuteTemplate(&buf, c.CodeConfig.TemplateName, nt)
if err != nil {
c.keeper[entity] = "//" + err.Error() // put a comment with that error
log.Printf("RecurseGenerate template error : %v", err)
return err
}
// everything went fine
c.keeper[entity] = buf.String()
//log.Printf("RecurseGenerate : %q for kind %q in package %q STORED", entity, nt.Kind, pkg)
return nil
}
func (c *Code) ListStored() []string {
var result []string
for key, value := range c.keeper {
if strings.HasPrefix(key, c.CodeConfig.TemplateName) {
if r, ok := value.(string); ok {
// len(0) is default template for main (
if len(r) > 0 {
result = append(result, r)
}
} else {
// if it's not a string, we're ignoring it
if c.CodeConfig.DebugPrint {
log.Printf("%q has prefix %q but it's not a string and we're ignoring it", key, c.CodeConfig.TemplateName)
}
}
}
}
return result
}
func (c *Code) Header(flagValues string) string {
return fmt.Sprintf("// Generated on %v by Stroo [https://github.com/badu/stroo]\n"+
"// Do NOT bother with altering it by hand : use the tool\n"+
"// Arguments at the time of generation:\n//\t%s", time.Now().Format("Mon Jan 2 15:04:05"), flagValues)
}