Skip to content

Commit 3894823

Browse files
authored
feat(golang): Add initialisms configuration (#3308)
* feat(golang): Add intialisms configuration * docs: Add docs for initialisms * Fix test names
1 parent 031c92a commit 3894823

File tree

15 files changed

+224
-2
lines changed

15 files changed

+224
-2
lines changed

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ The `gen` mapping supports the following keys:
167167
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
168168
- `build_tags`:
169169
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
170+
- `initialisms`:
171+
- 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"]`.
170172
- `json_tags_id_uppercase`:
171173
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
172174
- `json_tags_case_style`:

internal/codegen/golang/opts/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Options struct {
4343
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
4444
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
4545
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
46+
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`
47+
48+
InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
4649
}
4750

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

117+
if options.Initialisms == nil {
118+
options.Initialisms = new([]string)
119+
*options.Initialisms = []string{"id"}
120+
}
121+
122+
options.InitialismsMap = map[string]struct{}{}
123+
for _, initial := range *options.Initialisms {
124+
options.InitialismsMap[initial] = struct{}{}
125+
}
126+
114127
return &options, nil
115128
}
116129

internal/codegen/golang/struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func StructName(name string, options *opts.Options) string {
3232
}, name)
3333

3434
for _, p := range strings.Split(name, "_") {
35-
if p == "id" {
36-
out += "ID"
35+
if _, found := options.InitialismsMap[p]; found {
36+
out += strings.ToUpper(p)
3737
} else {
3838
out += strings.Title(p)
3939
}

internal/endtoend/testdata/golang_initialisms_empty/db/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/golang_initialisms_empty/db/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/golang_initialisms_empty/db/query.sql.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: SelectFoo :many
2+
SELECT * FROM foo;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE foo(
2+
bar_id text
3+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "schema.sql",
6+
"queries": "query.sql",
7+
"engine": "postgresql",
8+
"gen": {
9+
"go": {
10+
"out": "db",
11+
"initialisms": []
12+
}
13+
}
14+
}
15+
]
16+
}

internal/endtoend/testdata/golang_initialisms_url/db/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/golang_initialisms_url/db/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/golang_initialisms_url/db/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: SelectFoo :many
2+
SELECT * FROM foo;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE foo(
2+
bar_id text,
3+
site_url text
4+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "schema.sql",
6+
"queries": "query.sql",
7+
"engine": "postgresql",
8+
"gen": {
9+
"go": {
10+
"out": "db",
11+
"initialisms": ["id", "url"]
12+
}
13+
}
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)