Skip to content

fix: Let column names not invalidate Go field names #1840

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions internal/codegen/golang/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ func StructName(name string, settings *plugin.Settings) string {
return rename
}
out := ""
name = strings.Map(func(r rune) rune {
if unicode.IsLetter(r) {
return r
}
if unicode.IsDigit(r) {
return r
}
return rune('_')
}, name)
for _, p := range strings.Split(name, "_") {
if p == "id" {
out += "ID"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This tests that Go struct field names are proper Go identifiers.

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.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE bar (
id INT NOT NULL,
"!!!nobody,_,-would-believe---this-...?!" INT,
"parent id" INT);

-- name: test :one
SELECT * from bar limit 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}