Skip to content

Feat/nullable enums #1485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/batch/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions examples/booktest/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions examples/booktest/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions examples/ondeck/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions examples/ondeck/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func (i *importer) modelImports() fileImports {

if len(i.Enums) > 0 {
std["fmt"] = struct{}{}
std["database/sql/driver"] = struct{}{}
}

return sortedImports(std, pkg)
Expand Down
13 changes: 10 additions & 3 deletions internal/codegen/golang/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,17 @@ func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
for _, schema := range req.Catalog.Schemas {
for _, enum := range schema.Enums {
if enum.Name == columnType {
if schema.Name == req.Catalog.DefaultSchema {
return StructName(enum.Name, req.Settings)
if notNull {
if schema.Name == req.Catalog.DefaultSchema {
return StructName(enum.Name, req.Settings)
}
return StructName(schema.Name+"_"+enum.Name, req.Settings)
} else {
if schema.Name == req.Catalog.DefaultSchema {
return "Null" + StructName(enum.Name, req.Settings)
}
return "Null" + StructName(schema.Name+"_"+enum.Name, req.Settings)
}
return StructName(schema.Name+"_"+enum.Name, req.Settings)
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,17 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {

for _, enum := range schema.Enums {
if rel.Name == enum.Name && rel.Schema == schema.Name {
if schema.Name == req.Catalog.DefaultSchema {
return StructName(enum.Name, req.Settings)
if notNull {
if schema.Name == req.Catalog.DefaultSchema {
return StructName(enum.Name, req.Settings)
}
return StructName(schema.Name+"_"+enum.Name, req.Settings)
} else {
if schema.Name == req.Catalog.DefaultSchema {
return "Null" + StructName(enum.Name, req.Settings)
}
return "Null" + StructName(schema.Name+"_"+enum.Name, req.Settings)
}
return StructName(schema.Name+"_"+enum.Name, req.Settings)
}
}

Expand Down
24 changes: 24 additions & 0 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ func (e *{{.Name}}) Scan(src interface{}) error {
return nil
}

type Null{{.Name}} struct {
{{.Name}} {{.Name}}
Valid bool // Valid is true if String is not NULL
}

// Scan implements the Scanner interface.
func (ns *Null{{.Name}}) Scan(value interface{}) error {
if value == nil {
ns.{{.Name}}, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.{{.Name}}.Scan(value)
}

// Value implements the driver Valuer interface.
func (ns Null{{.Name}}) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.{{.Name}}, nil
}


{{ if $.EmitEnumValidMethod }}
func (e {{.Name}}) Valid() bool {
switch e {
Expand Down
24 changes: 24 additions & 0 deletions internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading