Add support for database uuid types.

This commit is contained in:
zer0sub 2019-04-04 13:07:21 +02:00
parent 2c7a9f5058
commit 37b0a6445b
6 changed files with 126 additions and 44 deletions

View file

@ -40,14 +40,14 @@ func (c ColumnInfo) ToSqlBuilderColumnType() string {
return "IntegerColumn"
case "date", "timestamp without time zone", "timestamp with time zone":
return "TimeColumn"
case "text", "character", "character varying", "bytea":
case "text", "character", "character varying", "bytea", "uuid":
return "StringColumn"
case "real":
return "NumericColumn"
case "numeric", "double precision":
return "NumericColumn"
default:
fmt.Println("Unknownl type: " + c.DataType + ", using string instead.")
fmt.Println("Unknownl type: " + c.DataType + ", using string column instead.")
return "StringColumn"
}
}
@ -67,7 +67,7 @@ func (c ColumnInfo) GoBaseType() string {
} else {
switch c.DataType {
case "USER-DEFINED":
return c.EnumName
return snaker.SnakeToCamel(c.EnumName)
case "boolean":
return "bool"
case "smallint":
@ -86,8 +86,10 @@ func (c ColumnInfo) GoBaseType() string {
return "float32"
case "numeric", "double precision":
return "float64"
case "uuid":
return "uuid.UUID"
default:
fmt.Println("Unknown go map type: " + c.DataType + ", using string instead.")
fmt.Println("Unknown go map type: " + c.DataType + ", " + c.EnumName + ", using string instead.")
return "string"
}
}

View file

@ -21,8 +21,11 @@ func (t TableInfo) GetImports() []string {
for _, column := range t.Columns {
columnType := column.GoBaseType()
if columnType == "time.Time" {
switch columnType {
case "time.Time":
imports["time.Time"] = "time"
case "uuid.UUID":
imports["uuid.UUID"] = "github.com/google/uuid"
}
}

View file

@ -66,17 +66,17 @@ var EnumModelTemplate = `package model
import "errors"
type {{.Name}} string
type {{camelize $.Name}} string
const (
{{- range $index, $element := .Values}}
{{camelize $.Name}}_{{camelize $element}} {{$.Name}} = "{{$element}}"
{{camelize $.Name}}_{{camelize $element}} {{camelize $.Name}} = "{{$element}}"
{{- end}}
)
func (e *{{$.Name}}) Scan(value interface{}) error {
func (e *{{camelize $.Name}}) Scan(value interface{}) error {
if v, ok := value.(string); !ok {
return errors.New("Invalid data for {{$.Name}} enum")
return errors.New("Invalid data for {{camelize $.Name}} enum")
} else {
switch string(v) {
{{- range $index, $element := .Values}}
@ -84,7 +84,7 @@ func (e *{{$.Name}}) Scan(value interface{}) error {
*e = {{camelize $.Name}}_{{camelize $element}}
{{- end}}
default:
return errors.New("Inavlid data " + string(v) + "for {{$.Name}} enum")
return errors.New("Inavlid data " + string(v) + "for {{camelize $.Name}} enum")
}
return nil