Skip to content

Commit

Permalink
feat(golang): Add initialisms configuration (sqlc-dev#3308)
Browse files Browse the repository at this point in the history
* feat(golang): Add intialisms configuration

* docs: Add docs for initialisms

* Fix test names
  • Loading branch information
kyleconroy authored and lisitsky committed Jun 21, 2024
1 parent 5d40805 commit 53314f8
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ The `gen` mapping supports the following keys:
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
- `build_tags`:
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
- `initialisms`:
- An array of [initialisms](https://google.github.io/styleguide/go/decisions.html#initialisms) to upper-case. For example, `app_id` becomes `AppID`. Defaults to `["id"]`.
- `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`:
Expand Down
13 changes: 13 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Options struct {
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`

InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
}

type GlobalOptions struct {
Expand Down Expand Up @@ -111,6 +114,16 @@ func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
*options.QueryParameterLimit = 1
}

if options.Initialisms == nil {
options.Initialisms = new([]string)
*options.Initialisms = []string{"id"}
}

options.InitialismsMap = map[string]struct{}{}
for _, initial := range *options.Initialisms {
options.InitialismsMap[initial] = struct{}{}
}

return &options, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/codegen/golang/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func StructName(name string, options *opts.Options) string {
}, name)

for _, p := range strings.Split(name, "_") {
if p == "id" {
out += "ID"
if _, found := options.InitialismsMap[p]; found {
out += strings.ToUpper(p)
} else {
out += strings.Title(p)
}
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_empty/db/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/golang_initialisms_empty/db/models.go

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.

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_empty/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: SelectFoo :many
SELECT * FROM foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE foo(
bar_id text
);
16 changes: 16 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_empty/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2",
"sql": [
{
"schema": "schema.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"go": {
"out": "db",
"initialisms": []
}
}
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/db/db.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/db/models.go

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

37 changes: 37 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/db/query.sql.go

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

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: SelectFoo :many
SELECT * FROM foo;
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE foo(
bar_id text,
site_url text
);
16 changes: 16 additions & 0 deletions internal/endtoend/testdata/golang_initialisms_url/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2",
"sql": [
{
"schema": "schema.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"go": {
"out": "db",
"initialisms": ["id", "url"]
}
}
}
]
}

0 comments on commit 53314f8

Please sign in to comment.