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

Add support for omitempty in JSON tags #3117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ The `gen` mapping supports the following keys:
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. 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`.
- `json_tags_omit_empty`:
- If true will add `omitempty` to JSON tags. Defaults to `false`.
- `omit_unused_structs`:
- If `true`, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to `false`.
- `output_batch_file_name`:
Expand Down
11 changes: 7 additions & 4 deletions internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ func TagsToString(tags map[string]string) string {
func JSONTagName(name string, options *opts.Options) string {
style := options.JsonTagsCaseStyle
idUppercase := options.JsonTagsIdUppercase
if style == "" || style == "none" {
return name
} else {
return SetJSONCaseStyle(name, style, idUppercase)
addOmitEmpty := options.JsonTagsOmitEmpty
if style != "" && style != "none" {
name = SetJSONCaseStyle(name, style, idUppercase)
}
if addOmitEmpty {
name = name + ",omitempty"
}
return name
}

func SetCaseStyle(name string, style string) string {
Expand Down
1 change: 1 addition & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
JsonTagsOmitEmpty bool `json:"json_tags_omit_empty,omitempty" yaml:"json_tags_omit_empty"`
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 @@ -43,6 +43,7 @@ type v1PackageSettings struct {
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
JSONTagsOmitEmpty bool `json:"json_tags_omit_empty,omitempty" yaml:"json_tags_omit_empty"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
Overrides []golang.Override `json:"overrides" yaml:"overrides"`
Expand Down Expand Up @@ -158,6 +159,7 @@ func (c *V1GenerateSettings) Translate() Config {
SqlDriver: pkg.SQLDriver,
Overrides: pkg.Overrides,
JsonTagsCaseStyle: pkg.JSONTagsCaseStyle,
JsonTagsOmitEmpty: pkg.JSONTagsOmitEmpty,
OutputBatchFileName: pkg.OutputBatchFileName,
OutputDbFileName: pkg.OutputDBFileName,
OutputModelsFileName: pkg.OutputModelsFileName,
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,2 @@
-- name: GetAll :many
SELECT * FROM users;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
first_name varchar(255),
last_name varchar(255),
age smallint
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_omit_empty": true
}
]
}

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,2 @@
-- name: GetAll :many
SELECT * FROM users;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
first_name varchar(255),
last_name varchar(255),
age smallint
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v5",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_omit_empty": true
}
]
}

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,2 @@
-- name: GetAll :many
SELECT * FROM users;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
first_name varchar(255),
last_name varchar(255),
age smallint
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_json_tags": true,
"json_tags_omit_empty": true
}
]
}
Loading