Skip to content

add custom json package for imports instead encoding/json support #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/sql_enum.go

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

2 changes: 1 addition & 1 deletion example/strings_only_enum.go

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

2 changes: 1 addition & 1 deletion generator/.snapshots/Test118CustomPrefixExampleFile-1.18
Original file line number Diff line number Diff line change
@@ -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\"",
2 changes: 1 addition & 1 deletion generator/.snapshots/Test118CustomPrefixExampleFile-og
Original file line number Diff line number Diff line change
@@ -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\"",
2 changes: 1 addition & 1 deletion generator/.snapshots/TestCustomPrefixExampleFile
Original file line number Diff line number Diff line change
@@ -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\"",
1 change: 1 addition & 0 deletions generator/enum.tmpl
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ package {{.package}}

import (
"fmt"
json "{{.jsonpkg}}"
)
{{end -}}

9 changes: 9 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}