Skip to content
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

Support JSON tags for nullable enum structs #2121

Merged
merged 13 commits into from
Jun 9, 2023
Merged
4 changes: 2 additions & 2 deletions examples/batch/postgresql/models.go

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

4 changes: 2 additions & 2 deletions examples/ondeck/mysql/models.go

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

4 changes: 2 additions & 2 deletions examples/ondeck/postgresql/models.go

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

10 changes: 10 additions & 0 deletions internal/codegen/golang/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type Enum struct {
Name string
Comment string
Constants []Constant
NameTags map[string]string
ValidTags map[string]string
}

func (e Enum) NameTag() string {
return TagsToString(e.NameTags)
}

func (e Enum) ValidTag() string {
return TagsToString(e.ValidTags)
}

func EnumReplace(value string) string {
Expand Down
24 changes: 14 additions & 10 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ type Field struct {
Tags map[string]string
Comment string
Column *plugin.Column
// EmbedFields contains the embedded fields that reuqire scanning.
// EmbedFields contains the embedded fields that require scanning.
EmbedFields []string
}

func (gf Field) Tag() string {
tags := make([]string, 0, len(gf.Tags))
for key, val := range gf.Tags {
tags = append(tags, fmt.Sprintf("%s:\"%s\"", key, val))
}
if len(tags) == 0 {
return ""
}
sort.Strings(tags)
return strings.Join(tags, " ")
return TagsToString(gf.Tags)
}

func (gf Field) HasSqlcSlice() bool {
return gf.Column.IsSqlcSlice
}

func TagsToString(tags map[string]string) string {
if len(tags) == 0 {
return ""
}
tagParts := make([]string, 0, len(tags))
for key, val := range tags {
tagParts = append(tagParts, fmt.Sprintf("%s:\"%s\"", key, val))
}
sort.Strings(tagParts)
return strings.Join(tagParts, " ")
}

func JSONTagName(name string, settings *plugin.Settings) string {
style := settings.Go.JsonTagsCaseStyle
if style == "" || style == "none" {
Expand Down
12 changes: 10 additions & 2 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ func buildEnums(req *plugin.CodeGenRequest) []Enum {
} else {
enumName = schema.Name + "_" + enum.Name
}

e := Enum{
Name: StructName(enumName, req.Settings),
Comment: enum.Comment,
Name: StructName(enumName, req.Settings),
Comment: enum.Comment,
NameTags: map[string]string{},
ValidTags: map[string]string{},
}
if req.Settings.Go.EmitJsonTags {
e.NameTags["json"] = JSONTagName(enumName, req.Settings)
e.ValidTags["json"] = JSONTagName("valid", req.Settings)
}

seen := make(map[string]struct{}, len(enum.Vals))
for i, v := range enum.Vals {
value := EnumReplace(v)
Expand Down
4 changes: 2 additions & 2 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (e *{{.Name}}) Scan(src interface{}) error {
}

type Null{{.Name}} struct {
{{.Name}} {{.Name}}
Valid bool // Valid is true if {{.Name}} is not NULL
{{.Name}} {{.Name}} {{if .NameTag}}{{$.Q}}{{.NameTag}}{{$.Q}}{{end}}
Valid bool {{if .ValidTag}}{{$.Q}}{{.ValidTag}}{{$.Q}}{{end}} // Valid is true if {{.Name}} is not NULL
}

// Scan implements the Scanner interface.
Expand Down

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TYPE job_post_location_type AS ENUM('remote', 'in_office', 'hybrid');

CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
type job_post_location_type,
name text NOT NULL,
bio text
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "db",
"schema": "query.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_case_style": "camel"
}
]
}

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

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

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

Loading