add custom js functions for templates
This commit is contained in:
44
examples/custom-js-functions/templates/model.tmpl
Normal file
44
examples/custom-js-functions/templates/model.tmpl
Normal file
@ -0,0 +1,44 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
{{formatComment (printf "Model for %s entity\nGenerated with custom JavaScript functions" .Entity.Name)}}
|
||||
type {{.Entity.Name | title}} struct {
|
||||
{{- range .Entity.Fields}}
|
||||
{{.Name | title}} {{goType .Type}} {{dbFieldMapping .Name .Type}}
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
{{formatComment "Table name for GORM"}}
|
||||
func ({{.Entity.Name | title}}) TableName() string {
|
||||
return "{{pluralize (.Entity.Name | lower)}}"
|
||||
}
|
||||
|
||||
{{formatComment "Validation function using custom JavaScript validation rules"}}
|
||||
func (m *{{.Entity.Name | title}}) Validate() error {
|
||||
{{- range .Entity.Fields}}
|
||||
{{- if .Required}}
|
||||
{{generateValidation .Type .Name}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
return nil
|
||||
}
|
||||
|
||||
{{formatComment "Create a new instance with validation"}}
|
||||
func New{{.Entity.Name | title}}({{range $i, $field := .Entity.Fields}}{{if $i}}, {{end}}{{$field.Name | lower}} {{goType $field.Type}}{{end}}) (*{{.Entity.Name | title}}, error) {
|
||||
model := &{{.Entity.Name | title}}{
|
||||
{{- range .Entity.Fields}}
|
||||
{{.Name | title}}: {{.Name | lower}},
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
if err := model.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return model, nil
|
||||
}
|
Reference in New Issue
Block a user