Skip to content

Commit 8f46216

Browse files
authored
compiler: Apply rename rules to enum constants (#523)
* compiler: Apply rename rules to enum constants * endtoend: Add rename test case * endtoend: Don't test rename via old parser
1 parent eecb559 commit 8f46216

File tree

8 files changed

+176
-1
lines changed

8 files changed

+176
-1
lines changed

internal/codegen/golang/enum.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ type Enum struct {
1919
Constants []Constant
2020
}
2121

22+
func EnumReplace(value string) string {
23+
id := strings.Replace(value, "-", "_", -1)
24+
id = strings.Replace(id, ":", "_", -1)
25+
id = strings.Replace(id, "/", "_", -1)
26+
return IdentPattern.ReplaceAllString(id, "")
27+
}
28+
2229
func EnumValueName(value string) string {
2330
name := ""
2431
id := strings.Replace(value, "-", "_", -1)

internal/compiler/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (r *Result) Enums(settings config.CombinedSettings) []golang.Enum {
101101
}
102102
for _, v := range enum.Vals {
103103
e.Constants = append(e.Constants, golang.Constant{
104-
Name: e.Name + golang.EnumValueName(v),
104+
Name: golang.StructName(enumName+"_"+golang.EnumReplace(v), settings),
105105
Value: v,
106106
Type: e.Name,
107107
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"experimental_parser_only": true
3+
}

internal/endtoend/testdata/rename/go/db.go

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

internal/endtoend/testdata/rename/go/models.go

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

internal/endtoend/testdata/rename/go/query.sql.go

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TYPE ip_protocol AS enum('tcp', 'ip', 'icmp');
2+
3+
CREATE TABLE bar_old (id_old serial not null, ip_old ip_protocol NOT NULL);
4+
5+
-- name: ListFoo :many
6+
SELECT id_old as foo_old, id_old as baz_old
7+
FROM bar_old
8+
WHERE ip_old = $1 AND id_old = $2;
9+
10+
-- name: ListBar :many
11+
SELECT * FROM bar_old;
12+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
],
11+
"rename": {
12+
"id_old": "IDNew",
13+
"bar_old": "BarNew",
14+
"foo_old": "FooNew",
15+
"ip_protocol": "IPProtocol",
16+
"ip_protocol_tcp": "IPProtocolTCP"
17+
}
18+
}

0 commit comments

Comments
 (0)