github.com/solongordon/pop@v4.10.0+incompatible/soda/cmd/generate/model_templates.go (about) 1 package generate 2 3 const modelTemplate = `package {{.package_name}} 4 5 import ( 6 {{ range $i := .model.Imports -}} 7 "{{$i}}" 8 {{ end -}} 9 {{ if .model.ValidatableAttributes -}} 10 "github.com/gobuffalo/validate/validators" 11 {{ end -}} 12 ) 13 14 type {{.model_name}} struct { 15 {{range $a := .model.Attributes -}} 16 {{$a}} 17 {{end -}} 18 } 19 20 // String is not required by pop and may be deleted 21 func ({{.char}} {{.model_name}}) String() string { 22 {{.encoding_type_char}}{{.char}}, _ := {{.encoding_type}}.Marshal({{.char}}) 23 return string({{.encoding_type_char}}{{.char}}) 24 } 25 26 // {{.plural_model_name}} is not required by pop and may be deleted 27 type {{.plural_model_name}} []{{.model_name}} 28 29 // String is not required by pop and may be deleted 30 func ({{.char}} {{.plural_model_name}}) String() string { 31 {{.encoding_type_char}}{{.char}}, _ := {{.encoding_type}}.Marshal({{.char}}) 32 return string({{.encoding_type_char}}{{.char}}) 33 } 34 35 // Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method. 36 // This method is not required and may be deleted. 37 func ({{.char}} *{{.model_name}}) Validate(tx *pop.Connection) (*validate.Errors, error) { 38 {{ if .model.ValidatableAttributes -}} 39 return validate.Validate( 40 {{ range $a := .model.ValidatableAttributes -}} 41 &validators.{{capitalize $a.GoType}}IsPresent{Field: {{$.char}}.{{$a.Name.Pascalize}}, Name: "{{$a.Name.Pascalize}}"}, 42 {{end -}} 43 ), nil 44 {{ else -}} 45 return validate.NewErrors(), nil 46 {{ end -}} 47 } 48 49 // ValidateCreate gets run every time you call "pop.ValidateAndCreate" method. 50 // This method is not required and may be deleted. 51 func ({{.char}} *{{.model_name}}) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) { 52 return validate.NewErrors(), nil 53 } 54 55 // ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method. 56 // This method is not required and may be deleted. 57 func ({{.char}} *{{.model_name}}) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) { 58 return validate.NewErrors(), nil 59 } 60 ` 61 62 const modelTestTemplate = `package {{.test_package_name}} 63 64 import "testing" 65 66 func Test_{{.model_name}}(t *testing.T) { 67 t.Fatal("This test needs to be implemented!") 68 } 69 `