Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: Apply rename rules to enum constants #523

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/codegen/golang/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type Enum struct {
Constants []Constant
}

func EnumReplace(value string) string {
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
return IdentPattern.ReplaceAllString(id, "")
}

func EnumValueName(value string) string {
name := ""
id := strings.Replace(value, "-", "_", -1)
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (r *Result) Enums(settings config.CombinedSettings) []golang.Enum {
}
for _, v := range enum.Vals {
e.Constants = append(e.Constants, golang.Constant{
Name: e.Name + golang.EnumValueName(v),
Name: golang.StructName(enumName+"_"+golang.EnumReplace(v), settings),
Value: v,
Type: e.Name,
})
Expand Down
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/rename/endtoend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimental_parser_only": true
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/rename/go/db.go

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

32 changes: 32 additions & 0 deletions internal/endtoend/testdata/rename/go/models.go

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

74 changes: 74 additions & 0 deletions internal/endtoend/testdata/rename/go/query.sql.go

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/rename/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TYPE ip_protocol AS enum('tcp', 'ip', 'icmp');

CREATE TABLE bar_old (id_old serial not null, ip_old ip_protocol NOT NULL);

-- name: ListFoo :many
SELECT id_old as foo_old, id_old as baz_old
FROM bar_old
WHERE ip_old = $1 AND id_old = $2;

-- name: ListBar :many
SELECT * FROM bar_old;

18 changes: 18 additions & 0 deletions internal/endtoend/testdata/rename/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
],
"rename": {
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol_tcp": "IPProtocolTCP"
}
}