Use _ to avoid reserved words in func args

- Fix #107
This commit is contained in:
Aaron L 2017-02-24 22:27:05 -08:00
parent 3747754236
commit 8aa4e2148c
5 changed files with 61 additions and 3 deletions

View file

@ -168,7 +168,8 @@ func (o once) Put(s string) bool {
// stringMap function. // stringMap function.
var templateStringMappers = map[string]func(string) string{ var templateStringMappers = map[string]func(string) string{
// String ops // String ops
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) }, "quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
"replaceReserved": strmangle.ReplaceReservedWords,
// Casing // Casing
"titleCase": strmangle.TitleCase, "titleCase": strmangle.TitleCase,

View file

@ -43,6 +43,34 @@ var uppercaseWords = map[string]struct{}{
"utf8": {}, "utf8": {},
} }
var reservedWords = map[string]struct{}{
"break": {},
"case": {},
"chan": {},
"const": {},
"continue": {},
"default": {},
"defer": {},
"else": {},
"fallthrough": {},
"for": {},
"func": {},
"go": {},
"goto": {},
"if": {},
"import": {},
"interface": {},
"map": {},
"package": {},
"range": {},
"return": {},
"select": {},
"struct": {},
"switch": {},
"type": {},
"var": {},
}
func init() { func init() {
// Our Boil inflection Ruleset does not include uncountable inflections. // Our Boil inflection Ruleset does not include uncountable inflections.
// This way, people using words like Sheep will not have // This way, people using words like Sheep will not have
@ -630,3 +658,12 @@ func IsEnumNormal(values []string) bool {
func ShouldTitleCaseEnum(value string) bool { func ShouldTitleCaseEnum(value string) bool {
return rgxEnumShouldTitle.MatchString(value) return rgxEnumShouldTitle.MatchString(value)
} }
// ReplaceReservedWords takes a word and replaces it with word_ if it's found
// in the list of reserved words.
func ReplaceReservedWords(word string) string {
if _, ok := reservedWords[word]; ok {
return word + "_"
}
return word
}

View file

@ -580,3 +580,23 @@ func TestShouldTitleCaseEnum(t *testing.T) {
} }
} }
} }
func TestReplaceReservedWords(t *testing.T) {
tests := []struct {
Word string
Replace bool
}{
{"break", true},
{"id", false},
{"type", true},
}
for i, test := range tests {
got := ReplaceReservedWords(test.Word)
if test.Replace && !strings.HasSuffix(got, "_") {
t.Errorf("%i) want suffixed (%s), got: %s", i, test.Word, got)
} else if !test.Replace && strings.HasSuffix(got, "_") {
t.Errorf("%i) want normal (%s), got: %s", i, test.Word, got)
}
}
}

View file

@ -1,7 +1,7 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}} {{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}} {{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase | stringMap .StringFuncs.replaceReserved -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}} {{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", "}}
// Find{{$tableNameSingular}}G retrieves a single record by ID. // Find{{$tableNameSingular}}G retrieves a single record by ID.
func Find{{$tableNameSingular}}G({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) { func Find{{$tableNameSingular}}G({{$pkArgs}}, selectCols ...string) (*{{$tableNameSingular}}, error) {

View file

@ -1,6 +1,6 @@
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}} {{- $colDefs := sqlColDefinitions .Table.Columns .Table.PKey.Columns -}}
{{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase -}} {{- $pkNames := $colDefs.Names | stringMap .StringFuncs.camelCase | stringMap .StringFuncs.replaceReserved -}}
{{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}} {{- $pkArgs := joinSlices " " $pkNames $colDefs.Types | join ", " -}}
{{- $schemaTable := .Table.Name | .SchemaTable}} {{- $schemaTable := .Table.Name | .SchemaTable}}
// {{$tableNameSingular}}Exists checks if the {{$tableNameSingular}} row exists. // {{$tableNameSingular}}Exists checks if the {{$tableNameSingular}} row exists.