-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for custom Go struct tags (#1569)
* internal/config: use strings.Trim{Prefix,Suffix} This is equivalent and slightly simpler. * Makefile: fix vtproto 'go install' command * internal/codegen/golang: simplify template tag condition Rather than modeling when .Tag will be empty, check directly whether .Tag is empty. This simplifies the template and reduces the number of places that must be touched when adding new sources of struct tags. * internal/codegen/golang: tweak tag formatting Rather than inserting the colon at tag construction time, insert it at tag formatting time. This makes the input look a bit more natural. This matters more, as we are about to add another, more distant, place where we insert tags. * all: add support for custom Go struct tags This change adds a new type of override: go_struct_tag. When provided for a field, it adds that struct tag to the generated code. The provided struct tag is parsed according to the standard package reflect rules, and its components are updated independently. This allows struct tag overrides to be compatible with (and optionally override) autogenerated json and db struct tags. Fixes #534 * go.mod: bump to Go 1.18 The code uses some 1.18-only features, like strings.Cut and testing.F. The CI requires Go 1.18. Since Go 1.18 is now required, reflect that in the go.mod.
- Loading branch information
Showing
36 changed files
with
817 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
internal/endtoend/testdata/overrides_go_struct_tags/invalid_tags/query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1; |
14 changes: 14 additions & 0 deletions
14
internal/endtoend/testdata/overrides_go_struct_tags/invalid_tags/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE foo ( | ||
other text NOT NULL, | ||
tagged text NOT NULL | ||
); | ||
|
||
CREATE TABLE bar ( | ||
other text NOT NULL, | ||
also_tagged text NOT NULL | ||
); | ||
|
||
CREATE TABLE baz ( | ||
other text NOT NULL, | ||
also_tagged text NOT NULL | ||
); |
22 changes: 22 additions & 0 deletions
22
internal/endtoend/testdata/overrides_go_struct_tags/invalid_tags/sqlc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"version": "1", | ||
"packages": [ | ||
{ | ||
"path": "go", | ||
"name": "override", | ||
"engine": "mysql", | ||
"schema": "schema.sql", | ||
"queries": "query.sql", | ||
"overrides": [ | ||
{ | ||
"go_struct_tag": "abc", | ||
"column": "foo.tagged" | ||
}, | ||
{ | ||
"go_struct_tag": "a:b", | ||
"column": "*.also_tagged" | ||
} | ||
] | ||
} | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
internal/endtoend/testdata/overrides_go_struct_tags/invalid_tags/stderr.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error parsing sqlc.json: Failed to parse Go struct tag: no colon in field "abc" |
31 changes: 31 additions & 0 deletions
31
internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/db.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
internal/endtoend/testdata/overrides_go_struct_tags/mysql/query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1; |
14 changes: 14 additions & 0 deletions
14
internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE foo ( | ||
other text NOT NULL, | ||
tagged text NOT NULL | ||
); | ||
|
||
CREATE TABLE bar ( | ||
other text NOT NULL, | ||
also_tagged text NOT NULL | ||
); | ||
|
||
CREATE TABLE baz ( | ||
other text NOT NULL, | ||
also_tagged text NOT NULL | ||
); |
Oops, something went wrong.