Merge pull request #467 from zenixls2/fix/fixedpoint_empty_check

fix: exception on parsing empty string in dnum
This commit is contained in:
Yo-An Lin 2022-03-07 12:07:00 +08:00 committed by GitHub
commit 62b6f7bf6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -491,6 +491,9 @@ func (v *Value) Scan(src interface{}) error {
// NewFromString parses a numeric string and returns a Value representation. // NewFromString parses a numeric string and returns a Value representation.
func NewFromString(s string) (Value, error) { func NewFromString(s string) (Value, error) {
length := len(s) length := len(s)
if length == 0 {
return Zero, nil
}
isPercentage := s[length-1] == '%' isPercentage := s[length-1] == '%'
if isPercentage { if isPercentage {
s = s[:length-1] s = s[:length-1]
@ -538,6 +541,9 @@ func MustNewFromString(input string) Value {
func NewFromBytes(s []byte) (Value, error) { func NewFromBytes(s []byte) (Value, error) {
length := len(s) length := len(s)
if length == 0 {
return Zero, nil
}
isPercentage := s[length-1] == '%' isPercentage := s[length-1] == '%'
if isPercentage { if isPercentage {
s = s[:length-1] s = s[:length-1]

View File

@ -126,6 +126,8 @@ func TestFromString(t *testing.T) {
assert.Equal(t, "0.00000011", f.String()) assert.Equal(t, "0.00000011", f.String())
f = MustNewFromString(".0%") f = MustNewFromString(".0%")
assert.Equal(t, Zero, f) assert.Equal(t, Zero, f)
f = MustNewFromString("")
assert.Equal(t, Zero, f)
} }
func TestJson(t *testing.T) { func TestJson(t *testing.T) {