Skip to content

feat(json-tags): Add case style config option #905

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

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ packages:
queries: "./sql/query/"
schema: "./sql/schema/"
engine: "postgresql"
emit_json_tags: true
emit_prepared_queries: true
emit_interface: false
emit_exact_table_names: false
emit_empty_slices: false
emit_json_tags: true
json_tags_case_style: "camel"
```
Each package document has the following keys:
Expand All @@ -29,8 +30,6 @@ Each package document has the following keys:
- Directory of SQL migrations or path to single SQL file; or a list of paths
- `engine`:
- Either `postgresql` or `mysql`. Defaults to `postgresql`. MySQL support is experimental
- `emit_json_tags`:
- If true, add JSON tags to generated structs. Defaults to `false`.
- `emit_db_tags`:
- If true, add DB tags to generated structs. Defaults to `false`.
- `emit_prepared_queries`:
Expand All @@ -41,6 +40,10 @@ Each package document has the following keys:
- If true, struct names will mirror table names. Otherwise, sqlc attempts to singularize plural table names. Defaults to `false`.
- `emit_empty_slices`:
- If true, slices returned by `:many` queries will be empty instead of `nil`. Defaults to `false`.
- `emit_json_tags`:
- If true, add JSON tags to generated structs. Defaults to `false`.
- `json_tags_case_style`:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.

## Type Overrides

Expand Down Expand Up @@ -118,5 +121,3 @@ packages: [...]
rename:
spotify_url: "SpotifyURL"
```


52 changes: 52 additions & 0 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sort"
"strings"

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

type Field struct {
Expand All @@ -24,3 +26,53 @@ func (gf Field) Tag() string {
sort.Strings(tags)
return strings.Join(tags, " ")
}

func JSONTagName(name string, settings config.CombinedSettings) 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/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func buildStructs(r *compiler.Result, settings config.CombinedSettings) []Struct
tags["db:"] = column.Name
}
if settings.Go.EmitJSONTags {
tags["json:"] = column.Name
tags["json:"] = JSONTagName(column.Name, settings)
}
s.Fields = append(s.Fields, Field{
Name: StructName(column.Name, settings),
Expand Down Expand Up @@ -259,7 +259,7 @@ func columnsToStruct(r *compiler.Result, name string, columns []goColumn, settin
tags["db:"] = tagName
}
if settings.Go.EmitJSONTags {
tags["json:"] = tagName
tags["json:"] = JSONTagName(tagName, settings)
}
gs.Fields = append(gs.Fields, Field{
Name: fieldName,
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type SQLGo struct {
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type v1PackageSettings struct {
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Overrides []Override `json:"overrides" yaml:"overrides"`
}

Expand Down Expand Up @@ -112,6 +113,7 @@ func (c *V1GenerateSettings) Translate() Config {
Package: pkg.Name,
Out: pkg.Path,
Overrides: pkg.Overrides,
JSONTagsCaseStyle: pkg.JSONTagsCaseStyle,
},
},
})
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/json_tags/camel_case/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/json_tags/camel_case/go/models.go

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

35 changes: 35 additions & 0 deletions internal/endtoend/testdata/json_tags/camel_case/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/json_tags/camel_case/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE users (
first_name varchar(255),
last_name varchar(255),
age smallint
);

-- name: GetAll :many
SELECT * FROM users;
14 changes: 14 additions & 0 deletions internal/endtoend/testdata/json_tags/camel_case/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_case_style": "camel"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/json_tags/pascal_case/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/json_tags/pascal_case/go/models.go

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

35 changes: 35 additions & 0 deletions internal/endtoend/testdata/json_tags/pascal_case/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/json_tags/pascal_case/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE users (
first_name varchar(255),
last_name varchar(255),
age smallint
);

-- name: GetAll :many
SELECT * FROM users;
14 changes: 14 additions & 0 deletions internal/endtoend/testdata/json_tags/pascal_case/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_case_style": "pascal"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/json_tags/snake_case/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/json_tags/snake_case/go/models.go

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

Loading