Improve database to golang name mapping.
This commit is contained in:
parent
3e7277015d
commit
950663dadb
19 changed files with 538 additions and 122 deletions
|
|
@ -3,7 +3,7 @@ package postgres_metadata
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/serenize/snaker"
|
||||
"github.com/go-jet/jet/internal/util"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
|
|||
func (c ColumnInfo) GoBaseType() string {
|
||||
switch c.DataType {
|
||||
case "USER-DEFINED":
|
||||
return snaker.SnakeToCamel(c.EnumName)
|
||||
return util.ToGoIdentifier(c.EnumName)
|
||||
case "boolean":
|
||||
return "bool"
|
||||
case "smallint":
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package postgres_metadata
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/serenize/snaker"
|
||||
"github.com/go-jet/jet/internal/util"
|
||||
)
|
||||
|
||||
type TableInfo struct {
|
||||
|
|
@ -58,7 +58,7 @@ func (t TableInfo) GetImports() []string {
|
|||
}
|
||||
|
||||
func (t TableInfo) GoStructName() string {
|
||||
return snaker.SnakeToCamel(t.name) + "Table"
|
||||
return util.ToGoIdentifier(t.name) + "Table"
|
||||
}
|
||||
|
||||
func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo TableInfo, err error) {
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@ package utils
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/serenize/snaker"
|
||||
"github.com/go-jet/jet/internal/util"
|
||||
"go/format"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -51,9 +50,7 @@ func EnsureDirPath(dirPath string) error {
|
|||
func GenerateTemplate(templateText string, templateData interface{}) ([]byte, error) {
|
||||
|
||||
t, err := template.New("sqlBuilderTableTemplate").Funcs(template.FuncMap{
|
||||
"camelize": func(txt string) string {
|
||||
return snaker.SnakeToCamel(strings.Replace(txt, "-", "_", -1))
|
||||
},
|
||||
"ToGoIdentifier": util.ToGoIdentifier,
|
||||
"now": func() string {
|
||||
return time.Now().Format(time.RFC850)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
"github.com/go-jet/jet/generator/internal/metadata/postgres-metadata"
|
||||
"github.com/go-jet/jet/generator/internal/utils"
|
||||
"github.com/go-jet/jet/internal/util"
|
||||
_ "github.com/lib/pq"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
|
@ -115,7 +116,7 @@ func generate(schemaInfo postgres_metadata.SchemaInfo, dirPath, packageName stri
|
|||
return err
|
||||
}
|
||||
|
||||
err = utils.SaveGoFile(modelDirPath, metaData.Name(), append(autoGenWarning, text...))
|
||||
err = utils.SaveGoFile(modelDirPath, util.ToGoFileName(metaData.Name()), append(autoGenWarning, text...))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ package postgresgen
|
|||
|
||||
var autoGenWarningTemplate = `
|
||||
//
|
||||
// Code generated by go-jet DO NOT EDIT.
|
||||
// Code generated by jetgen 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 ...
|
||||
// Licence under github.com/go-jet/jet/LICENSE
|
||||
//
|
||||
|
||||
`
|
||||
|
|
@ -16,7 +16,7 @@ var autoGenWarningTemplate = `
|
|||
var sqlBuilderTableTemplate = `
|
||||
{{define "column-list" -}}
|
||||
{{- range $i, $c := . }}
|
||||
{{- if gt $i 0 }}, {{end}}{{camelize $c.Name}}Column
|
||||
{{- if gt $i 0 }}, {{end}}{{ToGoIdentifier $c.Name}}Column
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
|
|
@ -26,14 +26,14 @@ import (
|
|||
"github.com/go-jet/jet"
|
||||
)
|
||||
|
||||
var {{camelize .Name}} = new{{.GoStructName}}()
|
||||
var {{ToGoIdentifier .Name}} = new{{.GoStructName}}()
|
||||
|
||||
type {{.GoStructName}} struct {
|
||||
jet.Table
|
||||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{camelize .Name}} jet.Column{{.SqlBuilderColumnType}}
|
||||
{{ToGoIdentifier .Name}} jet.Column{{.SqlBuilderColumnType}}
|
||||
{{- end}}
|
||||
|
||||
AllColumns jet.ColumnList
|
||||
|
|
@ -52,7 +52,7 @@ func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} {
|
|||
func new{{.GoStructName}}() *{{.GoStructName}} {
|
||||
var (
|
||||
{{- range .Columns}}
|
||||
{{camelize .Name}}Column = jet.{{.SqlBuilderColumnType}}Column("{{.Name}}")
|
||||
{{ToGoIdentifier .Name}}Column = jet.{{.SqlBuilderColumnType}}Column("{{.Name}}")
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ func new{{.GoStructName}}() *{{.GoStructName}} {
|
|||
|
||||
//Columns
|
||||
{{- range .Columns}}
|
||||
{{camelize .Name}}: {{camelize .Name}}Column,
|
||||
{{ToGoIdentifier .Name}}: {{ToGoIdentifier .Name}}Column,
|
||||
{{- end}}
|
||||
|
||||
AllColumns: jet.ColumnList{ {{template "column-list" .Columns}} },
|
||||
|
|
@ -82,9 +82,9 @@ import (
|
|||
{{end}}
|
||||
|
||||
|
||||
type {{camelize .Name}} struct {
|
||||
type {{ToGoIdentifier .Name}} struct {
|
||||
{{- range .Columns}}
|
||||
{{camelize .Name}} {{.GoModelType}} ` + "{{.GoModelTag ($.IsPrimaryKey .Name)}}" + `
|
||||
{{ToGoIdentifier .Name}} {{.GoModelType}} ` + "{{.GoModelTag ($.IsPrimaryKey .Name)}}" + `
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
|
@ -93,32 +93,32 @@ var enumModelTemplate = `package model
|
|||
|
||||
import "errors"
|
||||
|
||||
type {{camelize $.Name}} string
|
||||
type {{ToGoIdentifier $.Name}} string
|
||||
|
||||
const (
|
||||
{{- range $index, $element := .Values}}
|
||||
{{camelize $.Name}}_{{camelize $element}} {{camelize $.Name}} = "{{$element}}"
|
||||
{{ToGoIdentifier $.Name}}_{{ToGoIdentifier $element}} {{ToGoIdentifier $.Name}} = "{{$element}}"
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
func (e *{{camelize $.Name}}) Scan(value interface{}) error {
|
||||
func (e *{{ToGoIdentifier $.Name}}) Scan(value interface{}) error {
|
||||
if v, ok := value.(string); !ok {
|
||||
return errors.New("Invalid data for {{camelize $.Name}} enum")
|
||||
return errors.New("Invalid data for {{ToGoIdentifier $.Name}} enum")
|
||||
} else {
|
||||
switch string(v) {
|
||||
{{- range $index, $element := .Values}}
|
||||
case "{{$element}}":
|
||||
*e = {{camelize $.Name}}_{{camelize $element}}
|
||||
*e = {{ToGoIdentifier $.Name}}_{{ToGoIdentifier $element}}
|
||||
{{- end}}
|
||||
default:
|
||||
return errors.New("Inavlid data " + string(v) + "for {{camelize $.Name}} enum")
|
||||
return errors.New("Inavlid data " + string(v) + "for {{ToGoIdentifier $.Name}} enum")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (e {{camelize $.Name}}) String() string {
|
||||
func (e {{ToGoIdentifier $.Name}}) String() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
|
|
@ -127,13 +127,13 @@ var enumTypeTemplate = `package enum
|
|||
|
||||
import "github.com/go-jet/jet"
|
||||
|
||||
var {{camelize $.Name}} = &struct {
|
||||
var {{ToGoIdentifier $.Name}} = &struct {
|
||||
{{- range $index, $element := .Values}}
|
||||
{{camelize $element}} jet.StringExpression
|
||||
{{ToGoIdentifier $element}} jet.StringExpression
|
||||
{{- end}}
|
||||
} {
|
||||
{{- range $index, $element := .Values}}
|
||||
{{camelize $element}}: jet.NewEnumValue("{{$element}}"),
|
||||
{{ToGoIdentifier $element}}: jet.NewEnumValue("{{$element}}"),
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue