Skip to content

Commit 14506a8

Browse files
committed
Generate Null Enum type
- We need to import driver to comply with the Valuer interface. - The Scan method uses the Scan from the Non Null Enum type
1 parent 9240b66 commit 14506a8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

internal/codegen/golang/imports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func (i *importer) modelImports() fileImports {
264264

265265
if len(i.Enums) > 0 {
266266
std["fmt"] = struct{}{}
267+
std["database/sql/driver"] = struct{}{}
267268
}
268269

269270
return sortedImports(std, pkg)

internal/codegen/golang/templates/template.tmpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ func (e *{{.Name}}) Scan(src interface{}) error {
8080
}
8181
return nil
8282
}
83+
84+
type Null{{.Name}} struct {
85+
{{.Name}} {{.Name}}
86+
Valid bool // Valid is true if String is not NULL
87+
}
88+
89+
// Scan implements the Scanner interface.
90+
func (ns *Null{{.Name}}) Scan(value interface{}) error {
91+
if value == nil {
92+
ns.{{.Name}}, ns.Valid = "", false
93+
return nil
94+
}
95+
ns.Valid = true
96+
return ns.{{.Name}}.Scan(value)
97+
}
98+
99+
// Value implements the driver Valuer interface.
100+
func (ns Null{{.Name}}) Value() (driver.Value, error) {
101+
if !ns.Valid {
102+
return nil, nil
103+
}
104+
return ns.{{.Name}}, nil
105+
}
106+
83107
{{end}}
84108

85109
{{range .Structs}}

0 commit comments

Comments
 (0)