Skip to content

Commit

Permalink
Fix compilation errors with model generator (#442)
Browse files Browse the repository at this point in the history
* Fix compilation errors with model generator

Fixes #440

* Fix missing default ID for model migration
  • Loading branch information
stanislas-m committed Oct 5, 2019
1 parent c21e5c8 commit d04f898
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
19 changes: 19 additions & 0 deletions genny/fizz/ctable/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Options struct {
Type string
// ForceDefaultTimestamps enables auto timestamping for the generated table.
ForceDefaultTimestamps bool `json:"force_default_timestamps"`
// ForceDefaultID enables auto UUID for the generated table.
ForceDefaultID bool `json:"force_default_id"`
}

// Validate that options are usuable
Expand All @@ -53,5 +55,22 @@ func (opts *Options) Validate() error {
if opts.Type == "sql" && opts.Translator == nil {
return errors.New("sql migrations require a fizz translator")
}
if opts.ForceDefaultID {
var idFound bool
for _, a := range opts.Attrs {
switch a.Name.Underscore().String() {
case "id":
idFound = true
}
}
if !idFound {
// Add a default UUID
id, err := attrs.Parse("id:uuid")
if err != nil {
return err
}
opts.Attrs = append([]attrs.Attr{id}, opts.Attrs...)
}
}
return nil
}
3 changes: 2 additions & 1 deletion genny/model/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func buildImports(opts *Options) []string {
imps := map[string]bool{
"github.com/gobuffalo/validate": true,
"github.com/gobuffalo/pop": true,
}
if opts.Encoding == "jsonapi" {
imps["github.com/google/jsonapi"] = true
Expand All @@ -19,7 +20,7 @@ func buildImports(opts *Options) []string {
ats := opts.Attrs
for _, a := range ats {
switch a.GoType() {
case "uuid":
case "uuid", "uuid.UUID":
imps["github.com/gofrs/uuid"] = true
case "time.Time":
imps["time"] = true
Expand Down
9 changes: 9 additions & 0 deletions genny/model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import (
"strings"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/flect/name"
"github.com/gobuffalo/genny"
Expand Down Expand Up @@ -33,6 +35,13 @@ func New(opts *Options) (*genny.Generator, error) {
}
help := map[string]interface{}{
"capitalize": flect.Capitalize,
"trim_package": func(t string) string {
i := strings.LastIndex(t, ".")
if i == -1 {
return t
}
return t[i+1:]
},
}

t := gogen.TemplateTransformer(ctx, help)
Expand Down
2 changes: 1 addition & 1 deletion genny/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (opts *Options) forceDefaults() error {
if err != nil {
return err
}
opts.Attrs = append(opts.Attrs, id)
opts.Attrs = append([]attrs.Attr{id}, opts.Attrs...)
}

// Add default timestamp columns if they were not provided
Expand Down
2 changes: 1 addition & 1 deletion genny/model/templates/-path-/-name-.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ({{.model.Name.Char}} *{{.model.Name.Proper}}) Validate(tx *pop.Connection)
{{- if .model.Validations }}
return validate.Validate(
{{- range $a := .model.Validations }}
&validators.{{capitalize $a.GoType}}IsPresent{Field: {{$.model.Name.Char}}.{{$a.Name.Pascalize}}, Name: "{{$a.Name.Pascalize}}"},
&validators.{{capitalize (trim_package $a.GoType)}}IsPresent{Field: {{$.model.Name.Char}}.{{$a.Name.Pascalize}}, Name: "{{$a.Name.Pascalize}}"},
{{- end}}
), nil
{{- else }}
Expand Down
2 changes: 1 addition & 1 deletion packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions soda/cmd/generate/model_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var ModelCmd = &cobra.Command{
Path: path,
Type: modelCmdConfig.MigrationType,
Translator: translator,
ForceDefaultID: true,
ForceDefaultTimestamps: true,
})
if err != nil {
Expand Down

0 comments on commit d04f898

Please sign in to comment.