diff --git a/example/sql_enum.go b/example/sql_enum.go index acd9941..11351d3 100644 --- a/example/sql_enum.go +++ b/example/sql_enum.go @@ -11,7 +11,7 @@ package example import ( "database/sql/driver" - "encoding/json" + json "encoding/json" "errors" "fmt" "strconv" diff --git a/example/strings_only_enum.go b/example/strings_only_enum.go index b08d60a..3a2790e 100644 --- a/example/strings_only_enum.go +++ b/example/strings_only_enum.go @@ -11,7 +11,7 @@ package example import ( "database/sql/driver" - "encoding/json" + json "encoding/json" "errors" "fmt" "strings" diff --git a/generator/.snapshots/Test118CustomPrefixExampleFile-1.18 b/generator/.snapshots/Test118CustomPrefixExampleFile-1.18 index 55722e0..efb7571 100644 --- a/generator/.snapshots/Test118CustomPrefixExampleFile-1.18 +++ b/generator/.snapshots/Test118CustomPrefixExampleFile-1.18 @@ -9,7 +9,7 @@ (string) "", (string) (len=8) "import (", (string) (len=22) "\t\"database/sql/driver\"", - (string) (len=16) "\t\"encoding/json\"", + (string) (len=21) "\tjson \"encoding/json\"", (string) (len=9) "\t\"errors\"", (string) (len=6) "\t\"fmt\"", (string) (len=10) "\t\"strconv\"", diff --git a/generator/.snapshots/Test118CustomPrefixExampleFile-og b/generator/.snapshots/Test118CustomPrefixExampleFile-og index 7af7670..5921a18 100644 --- a/generator/.snapshots/Test118CustomPrefixExampleFile-og +++ b/generator/.snapshots/Test118CustomPrefixExampleFile-og @@ -9,7 +9,7 @@ (string) "", (string) (len=8) "import (", (string) (len=22) "\t\"database/sql/driver\"", - (string) (len=16) "\t\"encoding/json\"", + (string) (len=21) "\tjson \"encoding/json\"", (string) (len=9) "\t\"errors\"", (string) (len=6) "\t\"fmt\"", (string) (len=10) "\t\"strconv\"", diff --git a/generator/.snapshots/TestCustomPrefixExampleFile b/generator/.snapshots/TestCustomPrefixExampleFile index 7af7670..5921a18 100644 --- a/generator/.snapshots/TestCustomPrefixExampleFile +++ b/generator/.snapshots/TestCustomPrefixExampleFile @@ -9,7 +9,7 @@ (string) "", (string) (len=8) "import (", (string) (len=22) "\t\"database/sql/driver\"", - (string) (len=16) "\t\"encoding/json\"", + (string) (len=21) "\tjson \"encoding/json\"", (string) (len=9) "\t\"errors\"", (string) (len=6) "\t\"fmt\"", (string) (len=10) "\t\"strconv\"", diff --git a/generator/enum.tmpl b/generator/enum.tmpl index 574acdc..d96d6d2 100644 --- a/generator/enum.tmpl +++ b/generator/enum.tmpl @@ -13,6 +13,7 @@ package {{.package}} import ( "fmt" + json "{{.jsonpkg}}" ) {{end -}} diff --git a/generator/generator.go b/generator/generator.go index 38b02eb..61fd6ac 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -45,6 +45,7 @@ type Generator struct { names bool values bool leaveSnakeCase bool + jsonPkg string prefix string sqlNullInt bool sqlNullStr bool @@ -90,6 +91,7 @@ func NewGenerator() *Generator { fileSet: token.NewFileSet(), noPrefix: false, replacementNames: map[string]string{}, + jsonPkg: "encoding/json", } funcs := sprig.TxtFuncMap() @@ -171,6 +173,12 @@ func (g *Generator) WithoutSnakeToCamel() *Generator { return g } +// WithJsonPkg is used to add a custom json package to the imports +func (g *Generator) WithJsonPkg(pkg string) *Generator { + g.jsonPkg = pkg + return g +} + // WithPrefix is used to add a custom prefix to the enum constants func (g *Generator) WithPrefix(prefix string) *Generator { g.prefix = prefix @@ -295,6 +303,7 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) { "buildDate": g.BuildDate, "builtBy": g.BuiltBy, "buildTags": g.buildTags, + "jsonpkg": g.jsonPkg, }) if err != nil { return nil, fmt.Errorf("failed writing header: %w", err) diff --git a/main.go b/main.go index 94ec6c3..725a270 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ type rootT struct { SQL bool SQLInt bool Flag bool + JsonPkg string Prefix string Names bool Values bool @@ -102,6 +103,11 @@ func main() { Usage: "Adds golang flag functions.", Destination: &argv.Flag, }, + &cli.StringFlag{ + Name: "jsonpkg", + Usage: "Custom json package for imports instead encoding/json.", + Destination: &argv.JsonPkg, + }, &cli.StringFlag{ Name: "prefix", Usage: "Adds a prefix with a user one. If you would like to replace the prefix, then combine this option with --noprefix.", @@ -227,6 +233,9 @@ func main() { if argv.LeaveSnakeCase { g.WithoutSnakeToCamel() } + if argv.JsonPkg != "" { + g.WithJsonPkg(argv.JsonPkg) + } if argv.Prefix != "" { g.WithPrefix(argv.Prefix) }