Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports numbers of different bases #219

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions example/example_diff_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package example

//go:generate ../bin/go-enum --forcelower -b example

/*
ENUM(

B3 = 03
B4 = 04
B5 = 5
B6 = 0b110
B7 = 0b111
B8 = 0x08
B9 = 0x09
B10 = 0x0B
B11 = 0x2B

)
*/
type DiffBase int
87 changes: 87 additions & 0 deletions example/example_diff_base_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions example/example_diff_base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//go:build example
// +build example

package example

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestDiffBase(t *testing.T) {

tests := []struct {
name string
actual int
expected DiffBase
}{
{
name: "DiffBaseB3",
actual: 3,
expected: DiffBaseB3,
},
{
name: "DiffBaseB4",
actual: 4,
expected: DiffBaseB4,
},
{
name: "DiffBaseB5",
actual: 5,
expected: DiffBaseB5,
}, {
name: "DiffBaseB6",
actual: 6,
expected: DiffBaseB6,
}, {
name: "DiffBaseB7",
actual: 7,
expected: DiffBaseB7,
}, {
name: "DiffBaseB8",
actual: 8,
expected: DiffBaseB8,
}, {
name: "DiffBaseB9",
actual: 9,
expected: DiffBaseB9,
}, {
name: "DiffBaseB10",
actual: 0x0B,
expected: DiffBaseB10,
}, {
name: "DiffBaseB11",
actual: 0x2B,
expected: DiffBaseB11,
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
assert.Equal(tt, int(test.expected), test.actual)
})
}
}
18 changes: 16 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,17 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
valueStr = trimQuotes(dataVal)
}
} else if unsigned {
newData, err := strconv.ParseUint(dataVal, 10, 64)
dataVal, base := getNumberBase(dataVal)
newData, err := strconv.ParseUint(dataVal, base, 64)
if err != nil {
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
fmt.Println(err)
return nil, err
}
data = newData
} else {
newData, err := strconv.ParseInt(dataVal, 10, 64)
dataVal, base := getNumberBase(dataVal)
newData, err := strconv.ParseInt(dataVal, base, 64)
if err != nil {
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
fmt.Println(err)
Expand Down Expand Up @@ -522,6 +524,18 @@ func unescapeComment(comment string) string {
return val
}

// getNumberBase will return the base of the number if it is prefixed with 0x, 0, or 0b
func getNumberBase(value string) (string, int) {
if strings.HasPrefix(strings.ToLower(value), "0x") {
return value[2:], 16
} else if strings.HasPrefix(strings.ToLower(value), "0b") {
return value[2:], 2
} else if strings.HasPrefix(value, "0") && len(value) > 1 {
return value[1:], 8
}
Comment on lines +533 to +535
Copy link
Owner

@abice abice Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the benefit of this case. Do you mind elaborating a bit on why you would want this?

And what happens when you specify a value larger than the max?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intention is to support different representations of values in different binaries, in my project I usually use hexadecimal identifiers for these numbers, and if I convert to decimal for these values, it would be a problem to read the code. Of course, I could add comments to it and state the given value in the comments, but it feels like I'm adding to the problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0XX is the number written in the base of the eight.

return value, 10
}

// sanitizeValue will ensure the value name generated adheres to golang's
// identifier syntax as described here: https://golang.org/ref/spec#Identifiers
// identifier = letter { letter | unicode_digit }
Expand Down