From dac46c828e858a5b4bccbaaa8cab938ab04e3004 Mon Sep 17 00:00:00 2001 From: igormiku <> Date: Thu, 17 Mar 2022 19:37:38 +0200 Subject: [PATCH] add support for Nullable sql enums --- sql.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/sql.go b/sql.go index 09f050c..e077a71 100644 --- a/sql.go +++ b/sql.go @@ -32,6 +32,38 @@ const scanMethod = `func (i *%[1]s) Scan(value interface{}) error { *i = val return nil } + +` +const nullableImplementation = ` +type Null%[1]s struct { + %[1]s %[1]s + Valid bool +} + +func NewNull%[1]s(val interface{}) (x Null%[1]s) { + x.Scan(val) // yes, we ignore this error, it will just be an invalid value. + return +} + +// Scan implements the Scanner interface. +func (x *Null%[1]s) Scan(value interface{}) (err error) { + if value == nil { + x.%[1]s, x.Valid = %[1]s(0), false + return + } + + err = x.%[1]s.Scan(value) + x.Valid = (err == nil) + return +} + +// Value implements the driver Valuer interface. +func (x Null%[1]s) Value() (driver.Value, error) { + if !x.Valid { + return nil, nil + } + return x.%[1]s.String(), nil +} ` func (g *Generator) addValueAndScanMethod(typeName string) { @@ -39,4 +71,6 @@ func (g *Generator) addValueAndScanMethod(typeName string) { g.Printf(valueMethod, typeName) g.Printf("\n\n") g.Printf(scanMethod, typeName) + g.Printf("\n\n") + g.Printf(nullableImplementation, typeName) }