Skip to content

internal/dinosql: Fix incorrect enum names #223

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

Merged
merged 1 commit into from
Dec 29, 2019
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
24 changes: 14 additions & 10 deletions internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/jinzhu/inflection"
)

var identPattern = regexp.MustCompile("[^a-zA-Z0-9]+")
var identPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")

type GoConstant struct {
Name string
Expand Down Expand Up @@ -388,6 +388,18 @@ func (r Result) QueryImports(filename string) [][]string {
return [][]string{stds, pkgs}
}

func enumValueName(value string) string {
name := ""
id := strings.Replace(value, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
id = identPattern.ReplaceAllString(id, "")
for _, part := range strings.Split(id, "_") {
name += strings.Title(part)
}
return name
}

func (r Result) Enums() []GoEnum {
var enums []GoEnum
for name, schema := range r.Catalog.Schemas {
Expand All @@ -406,16 +418,8 @@ func (r Result) Enums() []GoEnum {
Comment: enum.Comment,
}
for _, v := range enum.Vals {
name := ""
id := strings.Replace(v, "-", "_", -1)
id = strings.Replace(id, ":", "_", -1)
id = strings.Replace(id, "/", "_", -1)
id = identPattern.ReplaceAllString(id, "")
for _, part := range strings.Split(id, "_") {
name += strings.Title(part)
}
e.Constants = append(e.Constants, GoConstant{
Name: e.Name + name,
Name: e.Name + enumValueName(v),
Value: v,
Type: e.Name,
})
Expand Down
24 changes: 24 additions & 0 deletions internal/dinosql/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,27 @@ func TestNullInnerType(t *testing.T) {
})
}
}

func TestEnumValueName(t *testing.T) {
values := map[string]string{
// Valid separators
"foo-bar": "FooBar",
"foo_bar": "FooBar",
"foo:bar": "FooBar",
"foo/bar": "FooBar",
// Strip unknown characters
"foo@bar": "Foobar",
"foo+bar": "Foobar",
"foo!bar": "Foobar",
}
for k, v := range values {
input := k
expected := v
t.Run(k+"-"+v, func(t *testing.T) {
actual := enumValueName(k)
if actual != expected {
t.Errorf("expected name for %s to be %s, not %s", input, expected, actual)
}
})
}
}