-
Notifications
You must be signed in to change notification settings - Fork 35
/
generator.go
206 lines (186 loc) · 5.95 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
package gorma
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goadesign/goa/design"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/goagen/utils"
)
// Generator is the application code generator.
type Generator struct {
genfiles []string // Generated files
outDir string // Absolute path to output directory
target string // Target package name - "models" by default
appPkg string // Generated goa app package name - "app" by default
appPkgPath string // Generated goa app package import path
}
// Generate is the generator entry point called by the meta generator.
func Generate() (files []string, err error) {
var outDir, target, appPkg, ver string
set := flag.NewFlagSet("gorma", flag.PanicOnError)
set.String("design", "", "")
set.StringVar(&outDir, "out", "", "")
set.StringVar(&ver, "version", "", "")
set.StringVar(&target, "pkg", "models", "")
set.StringVar(&appPkg, "app", "app", "")
set.Parse(os.Args[2:])
// First check compatibility
if err := codegen.CheckVersion(ver); err != nil {
return nil, err
}
// Now proceed
appPkgPath, err := codegen.PackagePath(filepath.Join(outDir, appPkg))
if err != nil {
return nil, fmt.Errorf("invalid app package: %s", err)
}
outDir = filepath.Join(outDir, target)
g := &Generator{outDir: outDir, target: target, appPkg: appPkg, appPkgPath: appPkgPath}
return g.Generate(design.Design)
}
// Generate the application code, implement codegen.Generator.
func (g *Generator) Generate(api *design.APIDefinition) (_ []string, err error) {
if api == nil {
return nil, fmt.Errorf("missing API definition, make sure design.Design is properly initialized")
}
go utils.Catch(nil, func() { g.Cleanup() })
defer func() {
if err != nil {
g.Cleanup()
}
}()
if err := os.MkdirAll(g.outDir, 0755); err != nil {
return nil, err
}
if err := g.generateUserTypes(g.outDir, api); err != nil {
return g.genfiles, err
}
if err := g.generateUserHelpers(g.outDir, api); err != nil {
return g.genfiles, err
}
return g.genfiles, nil
}
// Cleanup removes the entire "app" directory if it was created by this generator.
func (g *Generator) Cleanup() {
if len(g.genfiles) == 0 {
return
}
//os.RemoveAll(ModelOutputDir())
g.genfiles = nil
}
// generateUserTypes iterates through the user types and generates the data structures and
// marshaling code.
func (g *Generator) generateUserTypes(outdir string, api *design.APIDefinition) error {
var modelname, filename string
err := GormaDesign.IterateStores(func(store *RelationalStoreDefinition) error {
err := store.IterateModels(func(model *RelationalModelDefinition) error {
modelname = strings.ToLower(codegen.Goify(model.ModelName, false))
filename = fmt.Sprintf("%s.go", modelname)
utFile := filepath.Join(outdir, filename)
err := os.RemoveAll(utFile)
if err != nil {
fmt.Println(err)
}
utWr, err := NewUserTypesWriter(utFile)
if err != nil {
panic(err) // bug
}
title := fmt.Sprintf("%s: Models", api.Context())
imports := []*codegen.ImportSpec{
codegen.SimpleImport(g.appPkgPath),
codegen.SimpleImport("context"),
codegen.SimpleImport("time"),
codegen.SimpleImport("github.com/goadesign/goa"),
codegen.SimpleImport("github.com/jinzhu/gorm"),
codegen.SimpleImport("github.com/gofrs/uuid"),
}
if model.Cached {
imp := codegen.NewImport("cache", "github.com/patrickmn/go-cache")
imports = append(imports, imp)
imp = codegen.SimpleImport("strconv")
imports = append(imports, imp)
}
utWr.WriteHeader(title, g.target, imports)
data := &UserTypeTemplateData{
APIDefinition: api,
UserType: model,
DefaultPkg: g.target,
AppPkg: g.appPkgPath,
}
err = utWr.Execute(data)
g.genfiles = append(g.genfiles, utFile)
if err != nil {
fmt.Println(err)
return err
}
err = utWr.FormatCode()
if err != nil {
fmt.Println(err)
}
return err
})
return err
})
return err
}
// This var is set as a local packagewide variable to store the name of a
// mediatype (Mt1) that may be needed in anohter mediatype (Mt2) in a different call
// scopes of generateUserTypes. (if Mt1 is used in an array like manner in Mt2)
var helperFuncMediaTypeNames = map[string]string{}
// generateUserHelpers iterates through the user types and generates the data structures and
// marshaling code.
func (g *Generator) generateUserHelpers(outdir string, api *design.APIDefinition) error {
var modelname, filename string
err := GormaDesign.IterateStores(func(store *RelationalStoreDefinition) error {
err := store.IterateModels(func(model *RelationalModelDefinition) error {
modelname = strings.ToLower(codegen.Goify(model.ModelName, false))
filename = fmt.Sprintf("%s_helper.go", modelname)
utFile := filepath.Join(outdir, filename)
err := os.RemoveAll(utFile)
if err != nil {
fmt.Println(err)
}
utWr, err := NewUserHelperWriter(utFile)
if err != nil {
panic(err) // bug
}
title := fmt.Sprintf("%s: Model Helpers", api.Context())
imports := []*codegen.ImportSpec{
codegen.SimpleImport(g.appPkgPath),
codegen.SimpleImport("context"),
codegen.SimpleImport("time"),
codegen.SimpleImport("github.com/goadesign/goa"),
codegen.SimpleImport("github.com/jinzhu/gorm"),
codegen.NewImport("uuid", "github.com/satori/go.uuid"),
}
if model.Cached {
imp := codegen.NewImport("cache", "github.com/patrickmn/go-cache")
imports = append(imports, imp)
imp = codegen.SimpleImport("strconv")
imports = append(imports, imp)
}
utWr.WriteHeader(title, g.target, imports)
data := &UserTypeTemplateData{
APIDefinition: api,
UserType: model,
DefaultPkg: g.target,
AppPkg: g.appPkgPath,
}
err = utWr.Execute(data)
g.genfiles = append(g.genfiles, utFile)
if err != nil {
fmt.Println(err)
return err
}
err = utWr.FormatCode()
if err != nil {
fmt.Println(err)
}
return err
})
return err
})
return err
}