jet/generator/templates.go

117 lines
2.3 KiB
Go
Raw Normal View History

package generator
2019-05-24 13:13:13 +02:00
var autoGenWarningTemplate = `
//
// Code generated by sqlbuilder DO NOT EDIT.
// Generated at {{now}}
//
// WARNING: Changes to this file may cause incorrect behavior and will be lost
// if the code is regenerated
//
// Licence under ...
//
`
var sqlBuilderTableTemplate = `package table
2019-03-16 20:41:06 +01:00
import (
2019-04-07 10:05:41 +02:00
"github.com/sub0zero/go-sqlbuilder/sqlbuilder"
2019-03-16 20:41:06 +01:00
)
type {{.ToGoStructName}} struct {
sqlbuilder.Table
//Columns
{{- range .Columns}}
{{.ToGoFieldName}} *sqlbuilder.{{.ToSqlBuilderColumnType}}
{{- end}}
2019-03-15 21:25:24 +01:00
AllColumns sqlbuilder.ColumnList
}
2019-03-16 20:41:06 +01:00
var {{.ToGoVarName}} = new{{.ToGoStructName}}()
func new{{.ToGoStructName}}() *{{.ToGoStructName}} {
var (
{{- range .Columns}}
{{.ToGoVarName}} = sqlbuilder.New{{.ToSqlBuilderColumnType}}("{{.Name}}", {{if .IsNullable}}sqlbuilder.Nullable{{else}}sqlbuilder.NotNullable{{end}})
2019-03-16 20:41:06 +01:00
{{- end}}
)
return &{{.ToGoStructName}}{
2019-05-24 13:13:13 +02:00
Table: *sqlbuilder.NewTable("{{.SchemaName}}", "{{.Name}}", {{.ToGoColumnFieldList ", "}}),
2019-03-16 20:41:06 +01:00
//Columns
{{- range .Columns}}
2019-03-16 20:41:06 +01:00
{{.ToGoFieldName}}: {{.ToGoVarName}},
{{- end}}
2019-03-16 20:41:06 +01:00
AllColumns: sqlbuilder.ColumnList{ {{.ToGoColumnFieldList ", "}} },
}
}
func (a *{{.ToGoStructName}}) AS(alias string) *{{.ToGoStructName}} {
2019-03-16 20:41:06 +01:00
aliasTable := new{{.ToGoStructName}}()
aliasTable.Table.SetAlias(alias)
return aliasTable
}
`
2019-05-24 13:13:13 +02:00
var dataModelTemplate = `package model
2019-04-04 15:25:45 +02:00
{{ if .GetImports }}
import (
{{- range .GetImports}}
"{{.}}"
{{- end}}
)
{{end}}
2019-04-04 15:25:45 +02:00
type {{.ToGoModelStructName}} struct {
{{- range .Columns}}
2019-05-24 13:13:13 +02:00
{{.ToGoFieldName}} {{.ToGoType}} {{if $.IsUnique .Name}}` + "`sql:\"unique\"`" + ` {{end}}
{{- end}}
}
`
2019-04-03 19:21:46 +02:00
2019-05-24 13:13:13 +02:00
var enumModelTemplate = `package model
2019-04-03 19:21:46 +02:00
import "errors"
2019-04-04 13:07:21 +02:00
type {{camelize $.Name}} string
2019-04-03 19:21:46 +02:00
const (
{{- range $index, $element := .Values}}
2019-04-04 13:07:21 +02:00
{{camelize $.Name}}_{{camelize $element}} {{camelize $.Name}} = "{{$element}}"
2019-04-03 19:21:46 +02:00
{{- end}}
)
2019-04-04 13:07:21 +02:00
func (e *{{camelize $.Name}}) Scan(value interface{}) error {
2019-04-03 19:21:46 +02:00
if v, ok := value.(string); !ok {
2019-04-04 13:07:21 +02:00
return errors.New("Invalid data for {{camelize $.Name}} enum")
2019-04-03 19:21:46 +02:00
} else {
switch string(v) {
{{- range $index, $element := .Values}}
case "{{$element}}":
*e = {{camelize $.Name}}_{{camelize $element}}
{{- end}}
default:
2019-04-04 13:07:21 +02:00
return errors.New("Inavlid data " + string(v) + "for {{camelize $.Name}} enum")
2019-04-03 19:21:46 +02:00
}
return nil
}
}
2019-05-21 17:40:25 +02:00
func (e {{camelize $.Name}}) String() string {
return string(e)
}
2019-04-03 19:21:46 +02:00
`