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
68 changes: 1 addition & 67 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package golang

import (
"fmt"
"sort"
"strings"

"github.com/kyleconroy/sqlc/internal/plugin"
)

type Field struct {
Name string // CamelCased name for Go
DBName string // Name as used in the DB
Expand All @@ -17,63 +9,5 @@ type Field struct {
}

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, " ")
}

func JSONTagName(name string, settings *plugin.Settings) string {
style := settings.Go.JsonTagsCaseStyle
if style == "" || style == "none" {
return name
} else {
return SetCaseStyle(name, style)
}
}

func SetCaseStyle(name string, style string) string {
switch style {
case "camel":
return toCamelCase(name)
case "pascal":
return toPascalCase(name)
case "snake":
return toSnakeCase(name)
default:
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style))
}
}

func toSnakeCase(s string) string {
return strings.ToLower(s)
}

func toCamelCase(s string) string {
return toCamelInitCase(s, false)
}

func toPascalCase(s string) string {
return toCamelInitCase(s, true)
}

func toCamelInitCase(name string, initUpper bool) string {
out := ""
for i, p := range strings.Split(name, "_") {
if !initUpper && i == 0 {
out += p
continue
}
if p == "id" {
out += "ID"
} else {
out += strings.Title(p)
}
}
return out
return TagsToString(gf.Tags)
}
15 changes: 13 additions & 2 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ func buildEnums(req *plugin.CodeGenRequest) []Enum {
} else {
enumName = schema.Name + "_" + enum.Name
}

nameTags := map[string]string{}
validTags := map[string]string{}
if req.Settings.Go.EmitJsonTags {
nameTags["json"] = JSONTagName(enumName, req.Settings)
validTags["json"] = JSONTagName("valid", req.Settings)
}

e := Enum{
Name: StructName(enumName, req.Settings),
Comment: enum.Comment,
Name: StructName(enumName, req.Settings),
Comment: enum.Comment,
NameTags: nameTags,
ValidTags: validTags,
}

seen := make(map[string]struct{}, len(enum.Vals))
for i, v := range enum.Vals {
value := EnumReplace(v)
Expand Down
70 changes: 70 additions & 0 deletions internal/codegen/golang/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package golang

import (
"fmt"
"github.com/kyleconroy/sqlc/internal/plugin"
"sort"
"strings"
)

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

func JSONTagName(name string, settings *plugin.Settings) string {
style := settings.Go.JsonTagsCaseStyle
if style == "" || style == "none" {
return name
} else {
return SetCaseStyle(name, style)
}
}

func SetCaseStyle(name string, style string) string {
switch style {
case "camel":
return toCamelCase(name)
case "pascal":
return toPascalCase(name)
case "snake":
return toSnakeCase(name)
default:
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style))
}
}

func toSnakeCase(s string) string {
return strings.ToLower(s)
}

func toCamelCase(s string) string {
return toCamelInitCase(s, false)
}

func toPascalCase(s string) string {
return toCamelInitCase(s, true)
}

func toCamelInitCase(name string, initUpper bool) string {
out := ""
for i, p := range strings.Split(name, "_") {
if !initUpper && i == 0 {
out += p
continue
}
if p == "id" {
out += "ID"
} else {
out += strings.Title(p)
}
}
return out
}
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"
}
]
}
Loading