Skip to content

Commit b9b2668

Browse files
committed
report more errors when parsing numbers
Previously numbers' parsing could miss parse errors. Properly handle such cases now. Updates VictoriaMetrics/vmctl#25
1 parent bd9a6f1 commit b9b2668

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

parser.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,7 @@ func (v *Value) Float64() (float64, error) {
897897
if v.Type() != TypeNumber {
898898
return 0, fmt.Errorf("value doesn't contain number; it contains %s", v.Type())
899899
}
900-
f := fastfloat.ParseBestEffort(v.s)
901-
return f, nil
900+
return fastfloat.Parse(v.s)
902901
}
903902

904903
// Int returns the underlying JSON int for the v.
@@ -908,9 +907,9 @@ func (v *Value) Int() (int, error) {
908907
if v.Type() != TypeNumber {
909908
return 0, fmt.Errorf("value doesn't contain number; it contains %s", v.Type())
910909
}
911-
n := fastfloat.ParseInt64BestEffort(v.s)
912-
if n == 0 && v.s != "0" {
913-
return 0, fmt.Errorf("cannot parse int %q", v.s)
910+
n, err := fastfloat.ParseInt64(v.s)
911+
if err != nil {
912+
return 0, err
914913
}
915914
nn := int(n)
916915
if int64(nn) != n {
@@ -926,9 +925,9 @@ func (v *Value) Uint() (uint, error) {
926925
if v.Type() != TypeNumber {
927926
return 0, fmt.Errorf("value doesn't contain number; it contains %s", v.Type())
928927
}
929-
n := fastfloat.ParseUint64BestEffort(v.s)
930-
if n == 0 && v.s != "0" {
931-
return 0, fmt.Errorf("cannot parse uint %q", v.s)
928+
n, err := fastfloat.ParseUint64(v.s)
929+
if err != nil {
930+
return 0, err
932931
}
933932
nn := uint(n)
934933
if uint64(nn) != n {
@@ -944,11 +943,7 @@ func (v *Value) Int64() (int64, error) {
944943
if v.Type() != TypeNumber {
945944
return 0, fmt.Errorf("value doesn't contain number; it contains %s", v.Type())
946945
}
947-
n := fastfloat.ParseInt64BestEffort(v.s)
948-
if n == 0 && v.s != "0" {
949-
return 0, fmt.Errorf("cannot parse int64 %q", v.s)
950-
}
951-
return n, nil
946+
return fastfloat.ParseInt64(v.s)
952947
}
953948

954949
// Uint64 returns the underlying JSON uint64 for the v.
@@ -958,11 +953,7 @@ func (v *Value) Uint64() (uint64, error) {
958953
if v.Type() != TypeNumber {
959954
return 0, fmt.Errorf("value doesn't contain number; it contains %s", v.Type())
960955
}
961-
n := fastfloat.ParseUint64BestEffort(v.s)
962-
if n == 0 && v.s != "0" {
963-
return 0, fmt.Errorf("cannot parse uint64 %q", v.s)
964-
}
965-
return n, nil
956+
return fastfloat.ParseUint64(v.s)
966957
}
967958

968959
// Bool returns the underlying JSON bool for the v.

0 commit comments

Comments
 (0)