Skip to content

Commit

Permalink
fix bug: enums of underscored number (#1581) (#1592)
Browse files Browse the repository at this point in the history
Signed-off-by: sdghchj <[email protected]>
Co-authored-by: sdghchj <[email protected]>
  • Loading branch information
Nerzal and sdghchj committed May 31, 2023
1 parent c7b796d commit 60ef6b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/token"
"reflect"
"strconv"
"strings"
)

// PackageDefinitions files and definition in a package.
Expand Down Expand Up @@ -94,6 +95,10 @@ func (pkg *PackageDefinitions) evaluateConstValue(file *ast.File, iota int, expr
case *ast.BasicLit:
switch valueExpr.Kind {
case token.INT:
// handle underscored number, such as 1_000_000
if strings.ContainsRune(valueExpr.Value, '_') {
valueExpr.Value = strings.Replace(valueExpr.Value, "_", "", -1)
}
// hexadecimal
if len(valueExpr.Value) > 2 && valueExpr.Value[0] == '0' && valueExpr.Value[1] == 'x' {
if x, err := strconv.ParseInt(valueExpr.Value[2:], 16, 64); err == nil {
Expand Down
1 change: 1 addition & 0 deletions testdata/enums/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ const octnum = 017
const nonescapestr = `aa\nbb\u8888cc`
const escapestr = "aa\nbb\u8888cc"
const escapechar = '\u8888'
const underscored = 1_000_000

0 comments on commit 60ef6b7

Please sign in to comment.