Skip to content

Commit

Permalink
Fix false negatives in enum values
Browse files Browse the repository at this point in the history
Also strip non-alphanumerics when title casing
  • Loading branch information
stephenafamo committed Apr 15, 2022
1 parent 6eabd66 commit 15560e6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Versioning](http://semver.org/spec/v2.0.0.html).

- Don't generate test suites for views
- Properly assign new query object in models.Pural()
- Fix false negatives for enum values
- Strip non alphanumeric characters when title casing.

## [v4.9.2] - 2022-04-11

Expand Down
15 changes: 6 additions & 9 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (o once) Put(s string) bool {
// stringMap function.
var templateStringMappers = map[string]func(string) string{
// String ops
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
"quoteWrap": func(a string) string { return fmt.Sprintf(`%q`, a) },
"safeQuoteWrap": func(a string) string { return fmt.Sprintf(`\"%s\"`, a) },
"replaceReserved": strmangle.ReplaceReservedWords,

Expand Down Expand Up @@ -295,14 +295,11 @@ var templateFunctions = template.FuncMap{
"generateIgnoreTags": strmangle.GenerateIgnoreTags,

// Enum ops
"parseEnumName": strmangle.ParseEnumName,
"parseEnumVals": strmangle.ParseEnumVals,
"isEnumNormal": strmangle.IsEnumNormal,
"stripWhitespace": strmangle.StripWhitespace,
"shouldTitleCaseEnum": strmangle.ShouldTitleCaseEnum,
"onceNew": newOnce,
"oncePut": once.Put,
"onceHas": once.Has,
"parseEnumName": strmangle.ParseEnumName,
"parseEnumVals": strmangle.ParseEnumVals,
"onceNew": newOnce,
"oncePut": once.Put,
"onceHas": once.Has,

// String Map ops
"makeStringMap": strmangle.MakeStringMap,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/volatiletech/null/v8 v8.1.2
github.com/volatiletech/randomize v0.0.1
github.com/volatiletech/strmangle v0.0.2
github.com/volatiletech/strmangle v0.0.3
modernc.org/sqlite v1.14.5
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ github.com/volatiletech/randomize v0.0.1/go.mod h1:GN3U0QYqfZ9FOJ67bzax1cqZ5q2xu
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/volatiletech/strmangle v0.0.2 h1:amhpV9ATyq1DtkQ2D8WF94uqGpkYYmSmx7X2QIner/A=
github.com/volatiletech/strmangle v0.0.2/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/volatiletech/strmangle v0.0.3 h1:TltBd/LtxPrr1TY29OCroPbeklu+2c3Ih60srrpApSE=
github.com/volatiletech/strmangle v0.0.3/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
19 changes: 5 additions & 14 deletions templates/main/singleton/boil_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ It only titlecases the EnumValue portion if it's snake-cased.
($onceNull.Has $name)
)
) -}}
{{- if and (gt (len $vals) 0) (isEnumNormal $vals)}}
{{- if gt (len $vals) 0}}
{{- if $isNamed -}}
{{ $enumName = titleCase $name}}
{{- else -}}
Expand All @@ -99,12 +99,8 @@ It only titlecases the EnumValue portion if it's snake-cased.
// Enum values for {{$enumName}}
const (
{{range $val := $vals -}}
{{- $valStripped := stripWhitespace $val -}}
{{- $enumValue := $valStripped -}}
{{- if shouldTitleCaseEnum $valStripped -}}
{{$enumValue = titleCase $valStripped}}
{{- end -}}
{{$enumName}}{{$enumValue}} {{$enumType}} = "{{$val}}"
{{- $enumValue := titleCase $val -}}
{{$enumName}}{{$enumValue}} {{$enumType}} = {{printf "%q" $val}}
{{$allvals = printf "%s%s%s,\n" $allvals $enumName $enumValue -}}
{{end -}}
)
Expand All @@ -128,12 +124,7 @@ It only titlecases the EnumValue portion if it's snake-cased.
{{- $enumValues = printf "%s%s" $enumValues ", " -}}
{{- end -}}

{{- $valStripped := stripWhitespace $val -}}
{{- $enumValue := $valStripped -}}
{{- if shouldTitleCaseEnum $valStripped -}}
{{- $enumValue = titleCase $valStripped -}}
{{- end -}}

{{- $enumValue := titleCase $val -}}
{{- $enumValues = printf "%s%s%s" $enumValues $enumName $enumValue -}}
{{- end}}
switch e {
Expand Down Expand Up @@ -269,7 +260,7 @@ It only titlecases the EnumValue portion if it's snake-cased.
{{end -}}
{{end -}}
{{else}}
// Enum values for {{$enumName}} are not proper Go identifiers, cannot emit constants
// Enum values for {{$table.Name}} {{$col.Name}} are not proper Go identifiers, cannot emit constants
{{- end -}}
{{/* Save column type name after generation.
Needs to be at the bottom because we check for the first iteration
Expand Down

0 comments on commit 15560e6

Please sign in to comment.