From dac46c828e858a5b4bccbaaa8cab938ab04e3004 Mon Sep 17 00:00:00 2001 From: igormiku <> Date: Thu, 17 Mar 2022 19:37:38 +0200 Subject: [PATCH 1/2] 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) } From 2cd1d1c7c3550a4b2a8be610c70fa5748be58b01 Mon Sep 17 00:00:00 2001 From: igormiku <> Date: Thu, 17 Mar 2022 19:43:50 +0200 Subject: [PATCH 2/2] add posibility to reference as separate module --- examples/gomods/README.md | 8 ++++---- examples/gomods/main.go | 2 +- go.mod | 2 +- stringer.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/gomods/README.md b/examples/gomods/README.md index 8d96aa0..97d6ad5 100644 --- a/examples/gomods/README.md +++ b/examples/gomods/README.md @@ -2,7 +2,7 @@ ## Steps - 1. Go get enumer `go get -u github.com/dmarkham/enumer` - 2. `go generate` This should create `pill_enumer.go` - 3. `go run *.go` to see it in action - 4. `go mod tidy` to remove the deps for `enumer` once your happy. +1. Go get enumer `go get -u github.com/igormiku/enumer` +2. `go generate` This should create `pill_enumer.go` +3. `go run *.go` to see it in action +4. `go mod tidy` to remove the deps for `enumer` once your happy. diff --git a/examples/gomods/main.go b/examples/gomods/main.go index 7d38027..798efe8 100644 --- a/examples/gomods/main.go +++ b/examples/gomods/main.go @@ -2,7 +2,7 @@ package main import "fmt" -//go:generate go run github.com/dmarkham/enumer -type=Pill -json +//go:generate go run github.com/igormiku/enumer -type=Pill -json type Pill int const ( diff --git a/go.mod b/go.mod index b08e1fa..11c3cd1 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/dmarkham/enumer +module github.com/igormiku/enumer require ( github.com/pascaldekloe/name v1.0.0 diff --git a/stringer.go b/stringer.go index 49c87fd..29ee405 100644 --- a/stringer.go +++ b/stringer.go @@ -5,7 +5,7 @@ // Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). // It started as a fork of Rob Pike’s Stringer tool // -// Please visit http://github.com/dmarkham/enumer for a comprehensive documentation +// Please visit http://github.com/igormiku/enumer for a comprehensive documentation package main import ( @@ -71,7 +71,7 @@ func Usage() { _, _ = fmt.Fprintf(os.Stderr, "\tEnumer [flags] -type T [directory]\n") _, _ = fmt.Fprintf(os.Stderr, "\tEnumer [flags] -type T files... # Must be a single package\n") _, _ = fmt.Fprintf(os.Stderr, "For more information, see:\n") - _, _ = fmt.Fprintf(os.Stderr, "\thttp://godoc.org/github.com/dmarkham/enumer\n") + _, _ = fmt.Fprintf(os.Stderr, "\thttp://godoc.org/github.com/igormiku/enumer\n") _, _ = fmt.Fprintf(os.Stderr, "Flags:\n") flag.PrintDefaults() }