Skip to content

Commit 28240c6

Browse files
authored
pass 0 to let go parse the base value (#229)
1 parent f2bc4fe commit 28240c6

File tree

8 files changed

+202
-28
lines changed

8 files changed

+202
-28
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fmt:
4949
-$(GO) fmt ./...
5050

5151
test: gen-test generate
52-
$(GO) test -v -race -coverprofile=coverage.out ./...
53-
$(GO) test -v -race --tags=example ./example
52+
$(GO) test -v -race -shuffle on -coverprofile=coverage.out ./...
53+
$(GO) test -v -race -shuffle on --tags=example ./example
5454

5555
cover: gen-test test
5656
$(GO) tool cover -html=coverage.out -o coverage.html

example/diff_base.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package example
2+
3+
//go:generate ../bin/go-enum --forcelower -b example
4+
5+
/*
6+
ENUM(
7+
8+
B3 = 03
9+
B4 = 04
10+
B5 = 5
11+
B6 = 0b110
12+
B7 = 0b111
13+
B8 = 0x08
14+
B9 = 0x09
15+
B10 = 0x0B
16+
B11 = 0x2B
17+
18+
)
19+
*/
20+
type DiffBase int

example/diff_base_enum.go

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

example/diff_base_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build example
2+
// +build example
3+
4+
package example
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestDiffBase(t *testing.T) {
13+
tests := map[string]struct {
14+
actual int
15+
expected DiffBase
16+
}{
17+
"DiffBaseB3": {
18+
actual: 3,
19+
expected: DiffBaseB3,
20+
},
21+
"DiffBaseB4": {
22+
actual: 4,
23+
expected: DiffBaseB4,
24+
},
25+
"DiffBaseB5": {
26+
actual: 5,
27+
expected: DiffBaseB5,
28+
},
29+
"DiffBaseB6": {
30+
actual: 6,
31+
expected: DiffBaseB6,
32+
},
33+
"DiffBaseB7": {
34+
actual: 7,
35+
expected: DiffBaseB7,
36+
},
37+
"DiffBaseB8": {
38+
actual: 8,
39+
expected: DiffBaseB8,
40+
},
41+
"DiffBaseB9": {
42+
actual: 9,
43+
expected: DiffBaseB9,
44+
},
45+
"DiffBaseB10": {
46+
actual: 11,
47+
expected: DiffBaseB10,
48+
},
49+
"DiffBaseB11": {
50+
actual: 43,
51+
expected: DiffBaseB11,
52+
},
53+
}
54+
55+
for name, tc := range tests {
56+
t.Run(name, func(t *testing.T) {
57+
assert.Equal(t, int(tc.expected), tc.actual)
58+
})
59+
}
60+
}

generator/generator.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ const (
2525
parseCommentPrefix = `//`
2626
)
2727

28-
var replacementNames = map[string]string{}
29-
3028
// Generator is responsible for generating validation files for the given in a go source file.
3129
type Generator struct {
3230
Version string
@@ -56,6 +54,7 @@ type Generator struct {
5654
forceUpper bool
5755
noComments bool
5856
buildTags []string
57+
replacementNames map[string]string
5958
}
6059

6160
// Enum holds data for a discovered enum in the parsed source
@@ -90,6 +89,7 @@ func NewGenerator() *Generator {
9089
t: template.New("generator"),
9190
fileSet: token.NewFileSet(),
9291
noPrefix: false,
92+
replacementNames: map[string]string{},
9393
}
9494

9595
funcs := sprig.TxtFuncMap()
@@ -224,30 +224,35 @@ func (g *Generator) WithBuildTags(tags ...string) *Generator {
224224
return g
225225
}
226226

227+
// WithAliases will set up aliases for the generator.
228+
func (g *Generator) WithAliases(aliases map[string]string) *Generator {
229+
if aliases == nil {
230+
return g
231+
}
232+
g.replacementNames = aliases
233+
return g
234+
}
235+
227236
func (g *Generator) anySQLEnabled() bool {
228237
return g.sql || g.sqlNullStr || g.sqlint || g.sqlNullInt
229238
}
230239

231240
// ParseAliases is used to add aliases to replace during name sanitization.
232-
func ParseAliases(aliases []string) error {
241+
func ParseAliases(aliases []string) (map[string]string, error) {
233242
aliasMap := map[string]string{}
234243

235244
for _, str := range aliases {
236245
kvps := strings.Split(str, ",")
237246
for _, kvp := range kvps {
238247
parts := strings.Split(kvp, ":")
239248
if len(parts) != 2 {
240-
return fmt.Errorf("invalid formatted alias entry %q, must be in the format \"key:value\"", kvp)
249+
return nil, fmt.Errorf("invalid formatted alias entry %q, must be in the format \"key:value\"", kvp)
241250
}
242251
aliasMap[parts[0]] = parts[1]
243252
}
244253
}
245254

246-
for k, v := range aliasMap {
247-
replacementNames[k] = v
248-
}
249-
250-
return nil
255+
return aliasMap, nil
251256
}
252257

253258
// WithTemplates is used to provide the filenames of additional templates.
@@ -438,23 +443,23 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
438443
valueStr = dataVal
439444
rawName = value[:equalIndex]
440445
if enum.Type == "string" {
441-
if parsed, err := strconv.ParseInt(dataVal, 10, 64); err == nil {
446+
if parsed, err := strconv.ParseInt(dataVal, 0, 64); err == nil {
442447
data = parsed
443448
valueStr = rawName
444449
}
445450
if isQuoted(dataVal) {
446451
valueStr = trimQuotes(dataVal)
447452
}
448453
} else if unsigned {
449-
newData, err := strconv.ParseUint(dataVal, 10, 64)
454+
newData, err := strconv.ParseUint(dataVal, 0, 64)
450455
if err != nil {
451456
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
452457
fmt.Println(err)
453458
return nil, err
454459
}
455460
data = newData
456461
} else {
457-
newData, err := strconv.ParseInt(dataVal, 10, 64)
462+
newData, err := strconv.ParseInt(dataVal, 0, 64)
458463
if err != nil {
459464
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
460465
fmt.Println(err)
@@ -473,7 +478,7 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
473478
prefixedName := name
474479
if name != skipHolder {
475480
prefixedName = enum.Prefix + name
476-
prefixedName = sanitizeValue(prefixedName)
481+
prefixedName = g.sanitizeValue(prefixedName)
477482
if !g.leaveSnakeCase {
478483
prefixedName = snakeToCamelCase(prefixedName)
479484
}
@@ -526,14 +531,14 @@ func unescapeComment(comment string) string {
526531
// identifier syntax as described here: https://golang.org/ref/spec#Identifiers
527532
// identifier = letter { letter | unicode_digit }
528533
// where letter can be unicode_letter or '_'
529-
func sanitizeValue(value string) string {
534+
func (g *Generator) sanitizeValue(value string) string {
530535
// Keep skip value holders
531536
if value == skipHolder {
532537
return skipHolder
533538
}
534539

535540
replacedValue := value
536-
for k, v := range replacementNames {
541+
for k, v := range g.replacementNames {
537542
replacedValue = strings.ReplaceAll(replacedValue, k, v)
538543
}
539544

generator/generator_1.18_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ func Test118AliasParsing(t *testing.T) {
217217

218218
for name, tc := range tests {
219219
t.Run(name, func(t *testing.T) {
220-
defer func() {
221-
replacementNames = map[string]string{}
222-
}()
223-
err := ParseAliases(tc.input)
220+
replacementNames, err := ParseAliases(tc.input)
224221
if tc.err != nil {
225222
require.Error(t, err)
226223
require.EqualError(t, err, tc.err.Error())
@@ -306,9 +303,11 @@ func Test118Aliasing(t *testing.T) {
306303
// ENUM(a,b,CDEF) with some extra text
307304
type Animal int
308305
`
306+
aliases, err := ParseAliases([]string{"CDEF:C"})
307+
require.NoError(t, err)
309308
g := NewGenerator().
310-
WithoutSnakeToCamel()
311-
_ = ParseAliases([]string{"CDEF:C"})
309+
WithoutSnakeToCamel().
310+
WithAliases(aliases)
312311
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
313312
assert.Nil(t, err, "Error parsing no struct input")
314313

generator/generator_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ func TestAliasParsing(t *testing.T) {
240240

241241
for name, tc := range tests {
242242
t.Run(name, func(t *testing.T) {
243-
replacementNames = map[string]string{}
244-
err := ParseAliases(tc.input)
243+
replacementNames, err := ParseAliases(tc.input)
245244
if tc.err != nil {
246245
require.Error(t, err)
247246
require.EqualError(t, err, tc.err.Error())
@@ -327,9 +326,11 @@ func TestAliasing(t *testing.T) {
327326
// ENUM(a,b,CDEF) with some extra text
328327
type Animal int
329328
`
329+
aliases, err := ParseAliases([]string{"CDEF:C"})
330+
require.NoError(t, err)
330331
g := NewGenerator().
331-
WithoutSnakeToCamel()
332-
_ = ParseAliases([]string{"CDEF:C"})
332+
WithoutSnakeToCamel().
333+
WithAliases(aliases)
333334
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
334335
assert.Nil(t, err, "Error parsing no struct input")
335336

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ func main() {
176176
},
177177
},
178178
Action: func(ctx *cli.Context) error {
179-
if err := generator.ParseAliases(argv.Aliases.Value()); err != nil {
179+
aliases, err := generator.ParseAliases(argv.Aliases.Value())
180+
if err != nil {
180181
return err
181182
}
182183
for _, fileOption := range argv.FileNames.Value() {
@@ -188,6 +189,7 @@ func main() {
188189
g.BuiltBy = builtBy
189190

190191
g.WithBuildTags(argv.BuildTags.Value()...)
192+
g.WithAliases(aliases)
191193

192194
if argv.NoPrefix {
193195
g.WithNoPrefix()

0 commit comments

Comments
 (0)