Skip to content

Commit dc85b37

Browse files
codegen(go): Support JSON tags for nullable enum structs (#2121)
When `emit_json_tags` is set, add tags to nullable enum structs. --------- Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent f41173a commit dc85b37

File tree

32 files changed

+770
-20
lines changed

32 files changed

+770
-20
lines changed

examples/batch/postgresql/models.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ondeck/mysql/models.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ondeck/postgresql/models.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/codegen/golang/enum.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ type Enum struct {
1717
Name string
1818
Comment string
1919
Constants []Constant
20+
NameTags map[string]string
21+
ValidTags map[string]string
22+
}
23+
24+
func (e Enum) NameTag() string {
25+
return TagsToString(e.NameTags)
26+
}
27+
28+
func (e Enum) ValidTag() string {
29+
return TagsToString(e.ValidTags)
2030
}
2131

2232
func EnumReplace(value string) string {

internal/codegen/golang/field.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ type Field struct {
1616
Tags map[string]string
1717
Comment string
1818
Column *plugin.Column
19-
// EmbedFields contains the embedded fields that reuqire scanning.
19+
// EmbedFields contains the embedded fields that require scanning.
2020
EmbedFields []string
2121
}
2222

2323
func (gf Field) Tag() string {
24-
tags := make([]string, 0, len(gf.Tags))
25-
for key, val := range gf.Tags {
26-
tags = append(tags, fmt.Sprintf("%s:\"%s\"", key, val))
27-
}
28-
if len(tags) == 0 {
29-
return ""
30-
}
31-
sort.Strings(tags)
32-
return strings.Join(tags, " ")
24+
return TagsToString(gf.Tags)
3325
}
3426

3527
func (gf Field) HasSqlcSlice() bool {
3628
return gf.Column.IsSqlcSlice
3729
}
3830

31+
func TagsToString(tags map[string]string) string {
32+
if len(tags) == 0 {
33+
return ""
34+
}
35+
tagParts := make([]string, 0, len(tags))
36+
for key, val := range tags {
37+
tagParts = append(tagParts, fmt.Sprintf("%s:\"%s\"", key, val))
38+
}
39+
sort.Strings(tagParts)
40+
return strings.Join(tagParts, " ")
41+
}
42+
3943
func JSONTagName(name string, settings *plugin.Settings) string {
4044
style := settings.Go.JsonTagsCaseStyle
4145
if style == "" || style == "none" {

internal/codegen/golang/result.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ func buildEnums(req *plugin.CodeGenRequest) []Enum {
2424
} else {
2525
enumName = schema.Name + "_" + enum.Name
2626
}
27+
2728
e := Enum{
28-
Name: StructName(enumName, req.Settings),
29-
Comment: enum.Comment,
29+
Name: StructName(enumName, req.Settings),
30+
Comment: enum.Comment,
31+
NameTags: map[string]string{},
32+
ValidTags: map[string]string{},
33+
}
34+
if req.Settings.Go.EmitJsonTags {
35+
e.NameTags["json"] = JSONTagName(enumName, req.Settings)
36+
e.ValidTags["json"] = JSONTagName("valid", req.Settings)
3037
}
38+
3139
seen := make(map[string]struct{}, len(enum.Vals))
3240
for i, v := range enum.Vals {
3341
value := EnumReplace(v)

internal/codegen/golang/templates/template.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (e *{{.Name}}) Scan(src interface{}) error {
8888
}
8989

9090
type Null{{.Name}} struct {
91-
{{.Name}} {{.Name}}
92-
Valid bool // Valid is true if {{.Name}} is not NULL
91+
{{.Name}} {{.Name}} {{if .NameTag}}{{$.Q}}{{.NameTag}}{{$.Q}}{{end}}
92+
Valid bool {{if .ValidTag}}{{$.Q}}{{.ValidTag}}{{$.Q}}{{end}} // Valid is true if {{.Name}} is not NULL
9393
}
9494

9595
// Scan implements the Scanner interface.

internal/endtoend/testdata/json_tags_null_enum/camel_case/postgresql/stdlib/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/json_tags_null_enum/camel_case/postgresql/stdlib/go/models.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/json_tags_null_enum/camel_case/postgresql/stdlib/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TYPE job_post_location_type AS ENUM('remote', 'in_office', 'hybrid');
2+
3+
CREATE TABLE authors (
4+
id BIGSERIAL PRIMARY KEY,
5+
type job_post_location_type,
6+
name text NOT NULL,
7+
bio text
8+
);
9+
10+
-- name: GetAuthor :one
11+
SELECT * FROM authors
12+
WHERE id = $1 LIMIT 1;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "db",
8+
"schema": "query.sql",
9+
"queries": "query.sql",
10+
"emit_json_tags": true,
11+
"json_tags_case_style": "camel"
12+
}
13+
]
14+
}

internal/endtoend/testdata/json_tags_null_enum/none/postgresql/stdlib/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/json_tags_null_enum/none/postgresql/stdlib/go/models.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/json_tags_null_enum/none/postgresql/stdlib/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)