-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
317 lines (286 loc) · 7.37 KB
/
models.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
package task
import (
"github.com/pkg/errors"
"github.com/randallmlough/simmer/core"
"github.com/randallmlough/simmer/data"
"github.com/randallmlough/simmer/importers"
"github.com/randallmlough/simmer/templates"
"github.com/volatiletech/strmangle"
"regexp"
)
var (
rgxValidTableColumn = regexp.MustCompile(`^[\w]+\.[\w]+$|^[\w]+$`)
)
func ModelsTask(p *core.Options) core.Task {
return &Models{
Options: p,
NoDriverTemplates: false,
NoContext: false,
AddSoftDeletes: false,
NoRowsAffected: false,
NoHooks: false,
NoAutoTimestamps: false,
NoBackReferencing: false,
}
}
type Models struct {
*core.Options
NoDriverTemplates bool
NoContext bool
AddSoftDeletes bool
NoRowsAffected bool
NoHooks bool
NoAutoTimestamps bool
NoBackReferencing bool
}
func (m *Models) Name() string {
return "models"
}
func (m *Models) Run(simmer *core.Simmer) error {
if err := m.Init(defaultModelOptions); err != nil {
return errors.Wrap(err, "failed to initialize model options")
}
m.Imports = m.ConfigureImports(defaultModelsImports)
tpls, err := templates.LoadTemplates(m.TemplateDirs, m.Name())
if err != nil {
return errors.Wrap(err, "unable to initialize templates")
}
tplsFuncs := templates.TemplateFunctions
tplsFuncs.Append(data.DataFuncs)
if !m.NoDriverTemplates {
driverTpls, err := simmer.Data.Driver.Templates()
if err != nil {
return errors.Wrap(err, "failed to retrieve driver templates")
}
tpls.AppendBase64Templates(driverTpls)
driverImports, err := simmer.Data.Driver.Imports()
if err != nil {
return errors.Wrap(err, "failed to retrieve driver imports")
}
m.Imports = importers.Merge(m.Imports, driverImports)
}
if !m.NoContext {
m.Imports.All.Standard = append(m.Imports.All.Standard, `"context"`)
m.Imports.Test.Standard = append(m.Imports.Test.Standard, `"context"`)
}
type options struct {
*core.Options
TagIgnore map[string]struct{}
RelationTag string
PkgName string
OutputDirDepth int
AddGlobal bool
AddPanic bool
AddSoftDeletes bool
NoContext bool
NoHooks bool
NoAutoTimestamps bool
NoRowsAffected bool
NoDriverTemplates bool
NoBackReferencing bool
DBTypes templates.Once
}
ignoreTags := make(map[string]struct{})
for _, v := range m.TagIgnore {
if !rgxValidTableColumn.MatchString(v) {
return errors.New("Invalid column name %q supplied, only specify column name or table.column, eg: created_at, user.password")
}
ignoreTags[v] = struct{}{}
}
simmer.Options = options{
Options: m.Options,
TagIgnore: ignoreTags,
RelationTag: "-",
OutputDirDepth: core.OutputDirDepth(m.OutFolder),
AddGlobal: false,
AddPanic: false,
AddSoftDeletes: true,
NoContext: false,
NoHooks: false,
NoAutoTimestamps: false,
NoRowsAffected: false,
NoDriverTemplates: false,
NoBackReferencing: true,
DBTypes: make(templates.Once),
}
if err := templates.Render(templates.Options{
ImportNamedSet: m.Imports.Singleton,
OutFolder: m.OutFolder,
NoGeneratedHeader: m.NoGeneratedHeader,
PkgName: m.PkgName,
Data: simmer,
Templates: tpls,
TemplateFuncs: tplsFuncs,
IsSingleton: true,
IsTest: false,
}); err != nil {
return err
}
if !m.NoTests {
if err := templates.Render(templates.Options{
ImportNamedSet: m.Imports.TestSingleton,
OutFolder: m.OutFolder,
NoGeneratedHeader: m.NoGeneratedHeader,
PkgName: m.PkgName,
Data: simmer,
Templates: tpls,
TemplateFuncs: tplsFuncs,
IsSingleton: true,
IsTest: true,
}); err != nil {
return err
}
}
for _, model := range simmer.Models() {
if model.Table.IsJoinTable {
continue
}
simmer.Model = model
fname := model.Name
if m.PluralFileNames {
fname = strmangle.Plural(fname)
}
table := model.Table
var imps importers.Set
imps.Standard = m.Imports.All.Standard
imps.ThirdParty = m.Imports.All.ThirdParty
colTypes := make([]string, len(table.Columns))
for i, ct := range table.Columns {
colTypes[i] = ct.Type
}
imps = importers.AddTypeImports(imps, m.Imports.BasedOnType, colTypes)
if err := templates.Render(templates.Options{
Filename: fname,
ImportSet: imps,
OutFolder: m.OutFolder,
NoGeneratedHeader: m.NoGeneratedHeader,
PkgName: m.PkgName,
Data: simmer,
Templates: tpls,
TemplateFuncs: tplsFuncs,
IsTest: false,
}); err != nil {
return err
}
if !m.NoTests {
if err := templates.Render(templates.Options{
Filename: fname,
ImportSet: m.Imports.Test,
OutFolder: m.OutFolder,
NoGeneratedHeader: m.NoGeneratedHeader,
PkgName: m.PkgName,
Data: simmer,
Templates: tpls,
TemplateFuncs: tplsFuncs,
IsTest: true,
}); err != nil {
return err
}
}
}
return nil
}
func defaultModelOptions() core.Options {
return core.Options{
Name: "orm",
Debug: false,
PluralFileNames: false,
NoTests: false,
Tags: nil,
Replacements: nil,
Wipe: true,
NoGeneratedHeader: true,
StructTagCasing: "snake",
TagIgnore: nil,
}
}
// NewDefaultImports returns a default Imports struct.
func defaultModelsImports() importers.Collection {
var col importers.Collection
col.All = importers.Set{
Standard: importers.List{
`"database/sql"`,
`"fmt"`,
`"reflect"`,
`"strings"`,
`"sync"`,
`"time"`,
},
ThirdParty: importers.List{
`"github.com/pkg/errors"`,
`"github.com/volatiletech/null/v8"`,
`"github.com/randallmlough/simmer/simmer"`,
`"github.com/randallmlough/simmer/queries"`,
`"github.com/volatiletech/strmangle"`,
},
}
col.Singleton = importers.Map{
"boil_queries": {
ThirdParty: importers.List{
`"github.com/randallmlough/simmer/database"`,
`"github.com/randallmlough/simmer/queries"`,
},
},
"boil_types": {
Standard: importers.List{
`"strconv"`,
},
ThirdParty: importers.List{
`"github.com/pkg/errors"`,
`"github.com/randallmlough/simmer/simmer"`,
`"github.com/volatiletech/strmangle"`,
},
},
}
col.Test = importers.Set{
Standard: importers.List{
`"bytes"`,
`"reflect"`,
`"testing"`,
},
ThirdParty: importers.List{
`"github.com/randallmlough/simmer/simmer"`,
`"github.com/randallmlough/simmer/queries"`,
`"github.com/volatiletech/randomize"`,
`"github.com/volatiletech/strmangle"`,
},
}
col.TestSingleton = importers.Map{
"boil_main_test": {
Standard: importers.List{
`"database/sql"`,
`"flag"`,
`"fmt"`,
`"math/rand"`,
`"os"`,
`"path/filepath"`,
`"strings"`,
`"testing"`,
`"time"`,
},
ThirdParty: importers.List{
`"github.com/spf13/viper"`,
`"github.com/randallmlough/simmer/simmer"`,
},
},
"boil_queries_test": {
Standard: importers.List{
`"bytes"`,
`"fmt"`,
`"io"`,
`"io/ioutil"`,
`"math/rand"`,
`"regexp"`,
},
ThirdParty: importers.List{
`"github.com/randallmlough/simmer/simmer"`,
},
},
"boil_suites_test": {
Standard: importers.List{
`"testing"`,
},
},
}
return col
}