Skip to content

Commit e1e5ed6

Browse files
Add json_tags_id_uppercase configuration option (#2325)
* Add json_tags_id_camelcase configuration option If json_tags_id_camelcase is true, "ID" in json tags will be camelcase. If false, will be uppercase. Defaults to `false` * json_tags_id_uppercase If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
1 parent e6548cd commit e1e5ed6

File tree

8 files changed

+103
-4
lines changed

8 files changed

+103
-4
lines changed

Diff for: docs/reference/config.md

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ The `gen` mapping supports the following keys:
111111
- `emit_all_enum_values`:
112112
- If true, emit a function per enum type
113113
that returns all valid enum values.
114+
- `json_tags_id_uppercase`:
115+
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
114116
- `json_tags_case_style`:
115117
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
116118
- `output_batch_file_name`:

Diff for: internal/cmd/shim.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
8484
return &plugin.GoCode{
8585
EmitInterface: s.EmitInterface,
8686
EmitJsonTags: s.EmitJSONTags,
87+
JsonTagsIDUppercase: s.JsonTagsIDUppercase,
8788
EmitDbTags: s.EmitDBTags,
8889
EmitPreparedQueries: s.EmitPreparedQueries,
8990
EmitExactTableNames: s.EmitExactTableNames,

Diff for: internal/codegen/golang/field.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ func TagsToString(tags map[string]string) string {
4242

4343
func JSONTagName(name string, settings *plugin.Settings) string {
4444
style := settings.Go.JsonTagsCaseStyle
45+
idUppercase := settings.Go.JsonTagsIDUppercase
4546
if style == "" || style == "none" {
4647
return name
4748
} else {
48-
return SetCaseStyle(name, style)
49+
return SetJSONCaseStyle(name, style, idUppercase)
4950
}
5051
}
5152

@@ -62,6 +63,19 @@ func SetCaseStyle(name string, style string) string {
6263
}
6364
}
6465

66+
func SetJSONCaseStyle(name string, style string, idUppercase bool) string {
67+
switch style {
68+
case "camel":
69+
return toJsonCamelCase(name, idUppercase)
70+
case "pascal":
71+
return toPascalCase(name)
72+
case "snake":
73+
return toSnakeCase(name)
74+
default:
75+
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style))
76+
}
77+
}
78+
6579
var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")
6680

6781
func toSnakeCase(s string) string {
@@ -97,6 +111,28 @@ func toCamelInitCase(name string, initUpper bool) string {
97111
return out
98112
}
99113

114+
func toJsonCamelCase(name string, idUppercase bool) string {
115+
out := ""
116+
idStr := "Id"
117+
118+
if idUppercase {
119+
idStr = "ID"
120+
}
121+
122+
for i, p := range strings.Split(name, "_") {
123+
if i == 0 {
124+
out += p
125+
continue
126+
}
127+
if p == "id" {
128+
out += idStr
129+
} else {
130+
out += strings.Title(p)
131+
}
132+
}
133+
return out
134+
}
135+
100136
func toLowerCase(str string) string {
101137
if str == "" {
102138
return ""

Diff for: internal/codegen/golang/gen.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type tmplCtx struct {
2828
SourceName string
2929

3030
EmitJSONTags bool
31+
JsonTagsIDUppercase bool
3132
EmitDBTags bool
3233
EmitPreparedQueries bool
3334
EmitInterface bool
@@ -122,6 +123,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
122123
tctx := tmplCtx{
123124
EmitInterface: golang.EmitInterface,
124125
EmitJSONTags: golang.EmitJsonTags,
126+
JsonTagsIDUppercase: golang.JsonTagsIDUppercase,
125127
EmitDBTags: golang.EmitDbTags,
126128
EmitPreparedQueries: golang.EmitPreparedQueries,
127129
EmitEmptySlices: golang.EmitEmptySlices,

Diff for: internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type SQLGen struct {
119119
type SQLGo struct {
120120
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
121121
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
122+
JsonTagsIDUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
122123
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
123124
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
124125
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`

Diff for: internal/config/v_one.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type v1PackageSettings struct {
2525
Queries Paths `json:"queries" yaml:"queries"`
2626
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
2727
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
28+
JsonTagsIDUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
2829
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
2930
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
3031
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
@@ -143,6 +144,7 @@ func (c *V1GenerateSettings) Translate() Config {
143144
Go: &SQLGo{
144145
EmitInterface: pkg.EmitInterface,
145146
EmitJSONTags: pkg.EmitJSONTags,
147+
JsonTagsIDUppercase: pkg.JsonTagsIDUppercase,
146148
EmitDBTags: pkg.EmitDBTags,
147149
EmitPreparedQueries: pkg.EmitPreparedQueries,
148150
EmitExactTableNames: pkg.EmitExactTableNames,

Diff for: internal/plugin/codegen.pb.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/plugin/codegen_vtproto.pb.go

+50-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)