forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
269 lines (240 loc) · 6.96 KB
/
model.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
package generate
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gobuffalo/makr"
"github.com/pkg/errors"
"github.com/markbates/going/defaults"
"github.com/markbates/inflect"
"github.com/markbates/pop"
"github.com/spf13/cobra"
)
var skipMigration bool
var structTag string
func init() {
ModelCmd.Flags().StringVarP(&structTag, "struct-tag", "", "json", "sets the struct tags for model (xml or json)")
ModelCmd.Flags().BoolVarP(&skipMigration, "skip-migration", "s", false, "Skip creating a new fizz migration for this model.")
}
var nrx = regexp.MustCompile(`^nulls\.(.+)`)
type names struct {
Original string
Table string
Proper string
File string
Plural string
Char string
}
func newName(name string) names {
return names{
Original: name,
File: inflect.Underscore(inflect.Singularize(name)),
Table: inflect.Tableize(inflect.Pluralize(name)),
Proper: inflect.ForeignKeyToAttribute(name),
Plural: inflect.Pluralize(inflect.Camelize(name)),
Char: strings.ToLower(string([]byte(name)[0])),
}
}
type attribute struct {
Names names
OriginalType string
GoType string
Nullable bool
}
func (a attribute) String() string {
return fmt.Sprintf("\t%s %s `%s:\"%s\" db:\"%s\"`", a.Names.Proper, a.GoType, structTag, a.Names.Original, a.Names.Original)
}
type model struct {
Package string
Imports []string
Names names
Attributes []attribute
ValidatableAttributes []attribute
}
func (m model) Generate() error {
g := makr.New()
defer g.Fmt(".")
ctx := makr.Data{}
ctx["model"] = m
ctx["plural_model_name"] = m.Names.Plural
ctx["model_name"] = m.Names.Proper
ctx["package_name"] = m.Package
ctx["char"] = m.Names.Char
ctx["encoding_type"] = structTag
ctx["encoding_type_char"] = strings.ToLower(string([]byte(structTag)[0]))
fname := filepath.Join(m.Package, m.Names.File+".go")
g.Add(makr.NewFile(fname, modelTemplate))
tfname := filepath.Join(m.Package, m.Names.File+"_test.go")
g.Add(makr.NewFile(tfname, modelTestTemplate))
return g.Run(".", ctx)
}
func (m model) Fizz() string {
s := []string{fmt.Sprintf("create_table(\"%s\", func(t) {", m.Names.Table)}
for _, a := range m.Attributes {
switch a.Names.Original {
case "created_at", "updated_at":
case "id":
s = append(s, fmt.Sprintf("\tt.Column(\"id\", \"%s\", {\"primary\": true})", fizzColType(a.OriginalType)))
default:
x := fmt.Sprintf("\tt.Column(\"%s\", \"%s\", {})", a.Names.Original, fizzColType(a.OriginalType))
if a.Nullable {
x = strings.Replace(x, "{}", `{"null": true}`, -1)
}
s = append(s, x)
}
}
s = append(s, "})")
return strings.Join(s, "\n")
}
func newModel(name string) model {
m := model{
Package: "models",
Imports: []string{"time", "github.com/markbates/pop", "github.com/markbates/validate"},
Names: newName(name),
Attributes: []attribute{
{Names: newName("created_at"), OriginalType: "time.Time", GoType: "time.Time"},
{Names: newName("updated_at"), OriginalType: "time.Time", GoType: "time.Time"},
},
ValidatableAttributes: []attribute{},
}
m.Names.Proper = inflect.Singularize(m.Names.Proper)
return m
}
var ModelCmd = &cobra.Command{
Use: "model [name]",
Aliases: []string{"m"},
Short: "Generates a model for your database",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("You must supply a name for your model!")
}
if structTag != "json" && structTag != "xml" {
return errors.New("Invalid struct tags (use xml or json)!")
}
model := newModel(args[0])
if structTag == "json" {
model.Imports = append(model.Imports, "encoding/json")
} else if structTag == "xml" {
model.Imports = append(model.Imports, "encoding/xml")
}
hasId := false
hasUuid := false
hasNulls := false
for _, def := range args[1:] {
col := strings.Split(def, ":")
if len(col) == 1 {
col = append(col, "string")
}
nullable := nrx.MatchString(col[1])
if !hasNulls && nullable {
hasNulls = true
model.Imports = append(model.Imports, "github.com/markbates/pop/nulls")
}
if strings.HasPrefix(col[1], "slices.") {
model.Imports = append(model.Imports, "github.com/markbates/pop/slices")
} else if !hasUuid && col[1] == "uuid" {
hasUuid = true
model.Imports = append(model.Imports, "github.com/satori/go.uuid")
}
a := attribute{
Names: newName(col[0]),
OriginalType: col[1],
GoType: colType(col[1]),
Nullable: nullable,
}
if col[0] == "id" {
// No need to create a default ID
hasId = true
// Ensure ID is the first attribute
model.Attributes = append([]attribute{a}, model.Attributes...)
} else {
model.Attributes = append(model.Attributes, a)
}
if !a.Nullable {
if a.GoType == "string" || a.GoType == "time.Time" || a.GoType == "int" {
if a.GoType == "time.Time" {
a.GoType = "Time"
}
model.ValidatableAttributes = append(model.ValidatableAttributes, a)
}
}
}
// Add a default UUID, if no custom ID is provided
if !hasId {
if !hasUuid {
model.Imports = append(model.Imports, "github.com/satori/go.uuid")
}
id := newName("id")
id.Proper = "ID"
a := attribute{Names: id, OriginalType: "uuid.UUID", GoType: "uuid.UUID"}
// Ensure ID is the first attribute
model.Attributes = append([]attribute{a}, model.Attributes...)
}
err := os.MkdirAll(model.Package, 0766)
if err != nil {
return errors.Wrapf(err, "couldn't create folder %s", model.Package)
}
err = model.Generate()
if err != nil {
return err
}
if !skipMigration {
cflag := cmd.Flag("path")
migrationPath := defaults.String(cflag.Value.String(), "./migrations")
err = pop.MigrationCreate(migrationPath, fmt.Sprintf("create_%s", model.Names.Table), "fizz", []byte(model.Fizz()), []byte(fmt.Sprintf("drop_table(\"%s\")", model.Names.Table)))
if err != nil {
return err
}
}
return nil
},
}
func colType(s string) string {
switch strings.ToLower(s) {
case "text":
return "string"
case "time", "timestamp", "datetime":
return "time.Time"
case "nulls.text":
return "nulls.String"
case "uuid":
return "uuid.UUID"
case "json", "jsonb":
return "slices.Map"
case "[]string":
return "slices.String"
case "[]int":
return "slices.Int"
case "slices.float", "[]float", "[]float32", "[]float64":
return "slices.Float"
default:
return s
}
}
func fizzColType(s string) string {
switch strings.ToLower(s) {
case "int":
return "integer"
case "time", "datetime":
return "timestamp"
case "uuid.uuid", "uuid":
return "uuid"
case "nulls.float32", "nulls.float64":
return "float"
case "slices.string", "slices.uuid", "[]string":
return "varchar[]"
case "slices.float", "[]float", "[]float32", "[]float64":
return "numeric[]"
case "slices.int":
return "int[]"
case "slices.map":
return "jsonb"
default:
if nrx.MatchString(s) {
return fizzColType(strings.Replace(s, "nulls.", "", -1))
}
return strings.ToLower(s)
}
}