Skip to content

Commit

Permalink
Add json_tags_id_uppercase configuration option (#2325)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
jwc-clinnection committed Jun 21, 2023
1 parent e6548cd commit e1e5ed6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ The `gen` mapping supports the following keys:
- `emit_all_enum_values`:
- If true, emit a function per enum type
that returns all valid enum values.
- `json_tags_id_uppercase`:
- 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`.
- `output_batch_file_name`:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
return &plugin.GoCode{
EmitInterface: s.EmitInterface,
EmitJsonTags: s.EmitJSONTags,
JsonTagsIDUppercase: s.JsonTagsIDUppercase,
EmitDbTags: s.EmitDBTags,
EmitPreparedQueries: s.EmitPreparedQueries,
EmitExactTableNames: s.EmitExactTableNames,
Expand Down
38 changes: 37 additions & 1 deletion internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func TagsToString(tags map[string]string) string {

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

Expand All @@ -62,6 +63,19 @@ func SetCaseStyle(name string, style string) string {
}
}

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

var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")

func toSnakeCase(s string) string {
Expand Down Expand Up @@ -97,6 +111,28 @@ func toCamelInitCase(name string, initUpper bool) string {
return out
}

func toJsonCamelCase(name string, idUppercase bool) string {
out := ""
idStr := "Id"

if idUppercase {
idStr = "ID"
}

for i, p := range strings.Split(name, "_") {
if i == 0 {
out += p
continue
}
if p == "id" {
out += idStr
} else {
out += strings.Title(p)
}
}
return out
}

func toLowerCase(str string) string {
if str == "" {
return ""
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type tmplCtx struct {
SourceName string

EmitJSONTags bool
JsonTagsIDUppercase bool
EmitDBTags bool
EmitPreparedQueries bool
EmitInterface bool
Expand Down Expand Up @@ -122,6 +123,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
tctx := tmplCtx{
EmitInterface: golang.EmitInterface,
EmitJSONTags: golang.EmitJsonTags,
JsonTagsIDUppercase: golang.JsonTagsIDUppercase,
EmitDBTags: golang.EmitDbTags,
EmitPreparedQueries: golang.EmitPreparedQueries,
EmitEmptySlices: golang.EmitEmptySlices,
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type SQLGen struct {
type SQLGo struct {
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
JsonTagsIDUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
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 @@ -25,6 +25,7 @@ type v1PackageSettings struct {
Queries Paths `json:"queries" yaml:"queries"`
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
JsonTagsIDUppercase bool `json:"json_tags_id_uppercase" yaml:"json_tags_id_uppercase"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
Expand Down Expand Up @@ -143,6 +144,7 @@ func (c *V1GenerateSettings) Translate() Config {
Go: &SQLGo{
EmitInterface: pkg.EmitInterface,
EmitJSONTags: pkg.EmitJSONTags,
JsonTagsIDUppercase: pkg.JsonTagsIDUppercase,
EmitDBTags: pkg.EmitDBTags,
EmitPreparedQueries: pkg.EmitPreparedQueries,
EmitExactTableNames: pkg.EmitExactTableNames,
Expand Down
8 changes: 8 additions & 0 deletions internal/plugin/codegen.pb.go

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

53 changes: 50 additions & 3 deletions internal/plugin/codegen_vtproto.pb.go

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

0 comments on commit e1e5ed6

Please sign in to comment.