Skip to content

Commit 3eacb36

Browse files
authored
internal/dinosql: Filter out invalid characters (#198)
* internal/dinosql: Filter out invalid characters Ensure that enum identifiers are valid * Ignore certain values when picking names
1 parent 4c9f713 commit 3eacb36

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

internal/dinosql/gen.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"go/format"
88
"log"
99
"path/filepath"
10+
"regexp"
1011
"sort"
1112
"strings"
1213
"text/template"
@@ -17,6 +18,8 @@ import (
1718
"github.com/jinzhu/inflection"
1819
)
1920

21+
var identPattern = regexp.MustCompile("[^a-zA-Z0-9]+")
22+
2023
type GoConstant struct {
2124
Name string
2225
Type string
@@ -404,7 +407,11 @@ func (r Result) Enums() []GoEnum {
404407
}
405408
for _, v := range enum.Vals {
406409
name := ""
407-
for _, part := range strings.Split(strings.Replace(v, "-", "_", -1), "_") {
410+
id := strings.Replace(v, "-", "_", -1)
411+
id = strings.Replace(id, ":", "_", -1)
412+
id = strings.Replace(id, "/", "_", -1)
413+
id = identPattern.ReplaceAllString(id, "")
414+
for _, part := range strings.Split(id, "_") {
408415
name += strings.Title(part)
409416
}
410417
e.Constants = append(e.Constants, GoConstant{

internal/dinosql/testdata/ondeck/models.go

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

internal/dinosql/testdata/ondeck/prepared/models.go

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

internal/dinosql/testdata/ondeck/schema/0002_venue.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TYPE status AS ENUM ('open', 'closed');
1+
CREATE TYPE status AS ENUM ('op!en', 'clo@sed');
22
COMMENT ON TYPE status IS 'Venues can be either open or closed';
33

44
CREATE TABLE venues (

0 commit comments

Comments
 (0)