Skip to content

Commit 0979fc7

Browse files
authored
add custom json package for imports instead encoding/json support (#267)
1 parent a6f63bd commit 0979fc7

File tree

8 files changed

+24
-5
lines changed

8 files changed

+24
-5
lines changed

example/sql_enum.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/strings_only_enum.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generator/.snapshots/Test118CustomPrefixExampleFile-1.18

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(string) "",
1010
(string) (len=8) "import (",
1111
(string) (len=22) "\t\"database/sql/driver\"",
12-
(string) (len=16) "\t\"encoding/json\"",
12+
(string) (len=21) "\tjson \"encoding/json\"",
1313
(string) (len=9) "\t\"errors\"",
1414
(string) (len=6) "\t\"fmt\"",
1515
(string) (len=10) "\t\"strconv\"",

generator/.snapshots/Test118CustomPrefixExampleFile-og

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(string) "",
1010
(string) (len=8) "import (",
1111
(string) (len=22) "\t\"database/sql/driver\"",
12-
(string) (len=16) "\t\"encoding/json\"",
12+
(string) (len=21) "\tjson \"encoding/json\"",
1313
(string) (len=9) "\t\"errors\"",
1414
(string) (len=6) "\t\"fmt\"",
1515
(string) (len=10) "\t\"strconv\"",

generator/.snapshots/TestCustomPrefixExampleFile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(string) "",
1010
(string) (len=8) "import (",
1111
(string) (len=22) "\t\"database/sql/driver\"",
12-
(string) (len=16) "\t\"encoding/json\"",
12+
(string) (len=21) "\tjson \"encoding/json\"",
1313
(string) (len=9) "\t\"errors\"",
1414
(string) (len=6) "\t\"fmt\"",
1515
(string) (len=10) "\t\"strconv\"",

generator/enum.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package {{.package}}
1313

1414
import (
1515
"fmt"
16+
json "{{.jsonpkg}}"
1617
)
1718
{{end -}}
1819

generator/generator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Generator struct {
4545
names bool
4646
values bool
4747
leaveSnakeCase bool
48+
jsonPkg string
4849
prefix string
4950
sqlNullInt bool
5051
sqlNullStr bool
@@ -90,6 +91,7 @@ func NewGenerator() *Generator {
9091
fileSet: token.NewFileSet(),
9192
noPrefix: false,
9293
replacementNames: map[string]string{},
94+
jsonPkg: "encoding/json",
9395
}
9496

9597
funcs := sprig.TxtFuncMap()
@@ -171,6 +173,12 @@ func (g *Generator) WithoutSnakeToCamel() *Generator {
171173
return g
172174
}
173175

176+
// WithJsonPkg is used to add a custom json package to the imports
177+
func (g *Generator) WithJsonPkg(pkg string) *Generator {
178+
g.jsonPkg = pkg
179+
return g
180+
}
181+
174182
// WithPrefix is used to add a custom prefix to the enum constants
175183
func (g *Generator) WithPrefix(prefix string) *Generator {
176184
g.prefix = prefix
@@ -295,6 +303,7 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) {
295303
"buildDate": g.BuildDate,
296304
"builtBy": g.BuiltBy,
297305
"buildTags": g.buildTags,
306+
"jsonpkg": g.jsonPkg,
298307
})
299308
if err != nil {
300309
return nil, fmt.Errorf("failed writing header: %w", err)

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type rootT struct {
2828
SQL bool
2929
SQLInt bool
3030
Flag bool
31+
JsonPkg string
3132
Prefix string
3233
Names bool
3334
Values bool
@@ -102,6 +103,11 @@ func main() {
102103
Usage: "Adds golang flag functions.",
103104
Destination: &argv.Flag,
104105
},
106+
&cli.StringFlag{
107+
Name: "jsonpkg",
108+
Usage: "Custom json package for imports instead encoding/json.",
109+
Destination: &argv.JsonPkg,
110+
},
105111
&cli.StringFlag{
106112
Name: "prefix",
107113
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() {
227233
if argv.LeaveSnakeCase {
228234
g.WithoutSnakeToCamel()
229235
}
236+
if argv.JsonPkg != "" {
237+
g.WithJsonPkg(argv.JsonPkg)
238+
}
230239
if argv.Prefix != "" {
231240
g.WithPrefix(argv.Prefix)
232241
}

0 commit comments

Comments
 (0)