Skip to content

Commit a6f63bd

Browse files
authored
fix: fixed generation of string enums with one char being a single or (#256)
double quote
1 parent 51a0897 commit a6f63bd

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

generator/enum_string.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const (
1010
{{- if $value.Comment}}
1111
// {{$value.Comment}}
1212
{{- end}}
13-
{{$value.PrefixedName}} {{$enumName}} = "{{$value.ValueStr}}"
13+
{{$value.PrefixedName}} {{$enumName}} = {{quote $value.ValueStr}}
1414
{{- end}}
1515
)
1616
{{if .names -}}

generator/generator.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func NewGenerator() *Generator {
9999
funcs["unmapify"] = Unmapify
100100
funcs["namify"] = Namify
101101
funcs["offset"] = Offset
102+
funcs["quote"] = strconv.Quote
102103

103104
g.t.Funcs(funcs)
104105

@@ -447,8 +448,8 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
447448
data = parsed
448449
valueStr = rawName
449450
}
450-
if isQuoted(dataVal) {
451-
valueStr = trimQuotes(dataVal)
451+
if q := identifyQuoted(dataVal); q != "" {
452+
valueStr = trimQuotes(q, dataVal)
452453
}
453454
} else if unsigned {
454455
newData, err := strconv.ParseUint(dataVal, 0, 64)
@@ -495,18 +496,19 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
495496
return enum, nil
496497
}
497498

498-
func isQuoted(s string) bool {
499+
func identifyQuoted(s string) string {
499500
s = strings.TrimSpace(s)
500-
return (strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`)) || (strings.HasPrefix(s, `'`) && strings.HasSuffix(s, `'`))
501+
if strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`) {
502+
return `"`
503+
}
504+
if strings.HasPrefix(s, `'`) && strings.HasSuffix(s, `'`) {
505+
return `'`
506+
}
507+
return ""
501508
}
502509

503-
func trimQuotes(s string) string {
504-
s = strings.TrimSpace(s)
505-
for _, quote := range []string{`"`, `'`} {
506-
s = strings.TrimPrefix(s, quote)
507-
s = strings.TrimSuffix(s, quote)
508-
}
509-
return s
510+
func trimQuotes(q, s string) string {
511+
return strings.TrimPrefix(strings.TrimSuffix(strings.TrimSpace(s), q), q)
510512
}
511513

512514
func increment(d interface{}) interface{} {

generator/generator_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,54 @@ func TestQuotedStrings(t *testing.T) {
389389
fmt.Println(string(output))
390390
}
391391
}
392+
393+
func TestStringWithSingleDoubleQuoteValue(t *testing.T) {
394+
input := `package test
395+
// ENUM(DoubleQuote='"')
396+
type Char string
397+
`
398+
g := NewGenerator()
399+
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
400+
assert.Nil(t, err, "Error parsing no struct input")
401+
402+
output, err := g.Generate(f)
403+
assert.Nil(t, err, "Error generating formatted code")
404+
assert.Contains(t, string(output), "CharDoubleQuote Char = \"\\\"\"")
405+
if false { // Debugging statement
406+
fmt.Println(string(output))
407+
}
408+
}
409+
410+
func TestStringWithSingleSingleQuoteValue(t *testing.T) {
411+
input := `package test
412+
// ENUM(SingleQuote="'")
413+
type Char string
414+
`
415+
g := NewGenerator()
416+
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
417+
assert.Nil(t, err, "Error parsing no struct input")
418+
419+
output, err := g.Generate(f)
420+
assert.Nil(t, err, "Error generating formatted code")
421+
assert.Contains(t, string(output), "CharSingleQuote Char = \"'\"")
422+
if false { // Debugging statement
423+
fmt.Println(string(output))
424+
}
425+
}
426+
427+
func TestStringWithSingleBacktickValue(t *testing.T) {
428+
input := `package test
429+
// ENUM(SingleQuote="` + "`" + `")
430+
type Char string
431+
`
432+
g := NewGenerator()
433+
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
434+
assert.Nil(t, err, "Error parsing no struct input")
435+
436+
output, err := g.Generate(f)
437+
assert.Nil(t, err, "Error generating formatted code")
438+
assert.Contains(t, string(output), "CharSingleQuote Char = \"`\"")
439+
if false { // Debugging statement
440+
fmt.Println(string(output))
441+
}
442+
}

0 commit comments

Comments
 (0)