github.com/mpontillo/pop@v4.13.1+incompatible/genny/model/templates/-path-/-name-.go.tmpl (about) 1 package {{.opts.Package}} 2 3 import ( 4 {{- range $i := .model.Imports }} 5 "{{$i}}" 6 {{- end }} 7 {{- if .model.Validations }} 8 "github.com/gobuffalo/validate/validators" 9 {{- end }} 10 ) 11 // {{.model.Name.Proper}} is used by pop to map your {{.model.Name.Proper.Pluralize.Underscore}} database table to your go code. 12 {{- if eq $.model.Encoding.String "jsonapi"}} 13 type {{.model.Name.Proper}} struct { 14 {{- range $a := .opts.Attrs }} 15 {{$a.Name.Pascalize}} {{$a.GoType}} `jsonapi:"{{ if eq $a.Name.Underscore.String "id" }}primary{{ else }}attr{{ end }},{{$a.Name.Underscore}}" db:"{{$a.Name.Underscore}}"` 16 {{- end }} 17 {{- else }} 18 type {{.model.Name.Proper}} struct { 19 {{- range $a := .opts.Attrs }} 20 {{$a.Name.Pascalize}} {{$a.GoType}} `{{$.model.Encoding}}:"{{$a.Name.Underscore}}" db:"{{$a.Name.Underscore}}"` 21 {{- end }} 22 {{- end }} 23 } 24 25 // String is not required by pop and may be deleted 26 func ({{.model.Name.Char}} {{.model.Name.Proper}}) String() string { 27 {{- if eq $.model.Encoding.String "jsonapi"}} 28 var jb strings.Builder 29 _ = jsonapi.MarshalPayload(&jb, &{{.model.Name.Char}}) 30 return jb.String() 31 {{- else }} 32 {{.model.Encoding.Char}}{{.model.Name.Char}}, _ := {{.model.Encoding}}.Marshal({{.model.Name.Char}}) 33 return string({{.model.Encoding.Char}}{{.model.Name.Char}}) 34 {{- end }} 35 } 36 37 // {{.model.Name.Proper.Pluralize}} is not required by pop and may be deleted 38 type {{.model.Name.Proper.Pluralize}} []{{.model.Name.Proper}} 39 40 // String is not required by pop and may be deleted 41 func ({{.model.Name.Char}} {{.model.Name.Proper.Pluralize}}) String() string { 42 {{- if eq $.model.Encoding.String "jsonapi"}} 43 var jb strings.Builder 44 _ = jsonapi.MarshalPayload(&jb, &{{.model.Name.Char}}) 45 return jb.String() 46 {{- else }} 47 {{.model.Encoding.Char}}{{.model.Name.Char}}, _ := {{.model.Encoding}}.Marshal({{.model.Name.Char}}) 48 return string({{.model.Encoding.Char}}{{.model.Name.Char}}) 49 {{- end }} 50 } 51 52 // Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method. 53 // This method is not required and may be deleted. 54 func ({{.model.Name.Char}} *{{.model.Name.Proper}}) Validate(tx *pop.Connection) (*validate.Errors, error) { 55 {{- if .model.Validations }} 56 return validate.Validate( 57 {{- range $a := .model.Validations }} 58 &validators.{{capitalize (trim_package $a.GoType)}}IsPresent{Field: {{$.model.Name.Char}}.{{$a.Name.Pascalize}}, Name: "{{$a.Name.Pascalize}}"}, 59 {{- end}} 60 ), nil 61 {{- else }} 62 return validate.NewErrors(), nil 63 {{- end }} 64 } 65 66 // ValidateCreate gets run every time you call "pop.ValidateAndCreate" method. 67 // This method is not required and may be deleted. 68 func ({{.model.Name.Char}} *{{.model.Name.Proper}}) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) { 69 return validate.NewErrors(), nil 70 } 71 72 // ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method. 73 // This method is not required and may be deleted. 74 func ({{.model.Name.Char}} *{{.model.Name.Proper}}) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) { 75 return validate.NewErrors(), nil 76 }