Skip to content

Commit

Permalink
Add sql nullable types. (#46)
Browse files Browse the repository at this point in the history
* Add sql nullable types.
  • Loading branch information
abice authored Mar 3, 2021
1 parent 5ed55ce commit 74db3eb
Show file tree
Hide file tree
Showing 22 changed files with 5,221 additions and 318 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ I took the output of the [Stringer](https://godoc.org/golang.org/x/tools/cmd/str

``` shell
go-enum --help
Options:

-h, --help display help information
-f, --file *The file(s) to generate enums. Use more than one flag for more files.
--noprefix Prevents the constants generated from having the Enum as a prefix.
--lower Adds lowercase variants of the enum strings for lookup.
--marshal Adds text (and inherently json) marshalling functions.
--sql Adds SQL database scan and value functions.
--flag Adds golang flag functions.
--prefix Replaces the prefix with a user one.
--names Generates a 'Names() []string' function, and adds the possible enum values in the error response during parsing
--nocamel Removes the snake_case to CamelCase name changing
NAME:
go-enum - An enum generator for go

USAGE:
go-enum [global options] [arguments...]

GLOBAL OPTIONS:
--file value, -f value The file(s) to generate enums. Use more than one flag for more files.
--noprefix Prevents the constants generated from having the Enum as a prefix. (default: false)
--lower Adds lowercase variants of the enum strings for lookup. (default: false)
--nocase Adds case insensitive parsing to the enumeration (forces lower flag). (default: false)
--marshal Adds text (and inherently json) marshalling functions. (default: false)
--sql Adds SQL database scan and value functions. (default: false)
--flag Adds golang flag functions. (default: false)
--prefix value Replaces the prefix with a user one.
--names Generates a 'Names() []string' function, and adds the possible enum values in the error response during parsing (default: false)
--nocamel Removes the snake_case to CamelCase name changing (default: false)
--ptr Adds a pointer method to get a pointer from const values (default: false)
--sqlnullint Adds a Null{{ENUM}} type for marshalling a nullable int value to sql (default: false)
--sqlnullstr Adds a Null{{ENUM}} type for marshalling a nullable string value to sql. If sqlnullint is specified too, it will be Null{{ENUM}}Str (default: false)
--help, -h show help (default: false)
```
### Syntax
Expand Down
2 changes: 1 addition & 1 deletion example/color.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower --ptr

package example

Expand Down
4 changes: 4 additions & 0 deletions example/color_enum.go

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

24 changes: 24 additions & 0 deletions example/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package example
import (
"encoding/json"
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -19,6 +20,7 @@ func TestColorString(t *testing.T) {

assert.Equal(t, Color(33), ColorGreen)
assert.Equal(t, Color(34), ColorBlue)
assert.Equal(t, &x, Color(109).Ptr())
}

func TestColorUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -225,3 +227,25 @@ func TestColorMarshal(t *testing.T) {
})
}
}

func BenchmarkColorParse(b *testing.B) {

knownItems := []string{
ColorRedOrangeBlue.String(),
strings.ToLower(ColorRedOrangeBlue.String()),
// "2", Leave this in to add an int as string parsing option in future.
}

var err error
for _, item := range knownItems {
b.Run(item, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = ParseColor(item)
assert.NoError(b, err)
}
})
}

}
55 changes: 55 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"strings"
"testing"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -219,3 +220,57 @@ func TestNoZeroValues(t *testing.T) {

}
}

func BenchmarkMakeParse(b *testing.B) {

knownItems := map[string]struct {
input string
output Make
err error
}{
"cased lookup": {
input: MakeAudi.String(),
output: MakeAudi,
},
"lowercase lookup": {
input: strings.ToLower(MakeVolkswagon.String()),
output: MakeVolkswagon,
},
"hyphenated upper": {
input: strings.ToUpper(MakeMercedesBenz.String()),
output: MakeMercedesBenz,
},
// Leave this in to add an int as string parsing option in future.
// "numeric": {
// input: "2",
// output: MakeChevy,
// },
// "last numeric": {
// input: "20",
// output: MakeVolkswagon,
// },
"failure": {
input: "xyz",
output: MakeToyota,
err: errors.New("xyz is not a valid Make, try [Toyota, Chevy, Ford, Tesla, Hyundai, Nissan, Jaguar, Audi, BMW, Mercedes-Benz, Volkswagon]"),
},
}

for name, tc := range knownItems {
b.Run(name, func(b *testing.B) {
b.ReportAllocs()

b.ResetTimer()
for i := 0; i < b.N; i++ {
out, err := ParseMake(tc.input)
assert.Equal(b, tc.output, out)
if tc.err != nil {
assert.Error(b, err)
} else {
assert.NoError(b, err)
}
}
})
}

}
2 changes: 1 addition & 1 deletion example/sql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate ../bin/go-enum -f=$GOFILE --sql
//go:generate ../bin/go-enum -f=$GOFILE --sql --sqlnullstr --sqlnullint --ptr --marshal

package example

Expand Down
Loading

0 comments on commit 74db3eb

Please sign in to comment.