fix: exception on parsing empty string in dnum

This commit is contained in:
zenix 2022-03-07 12:46:03 +09:00
parent d01fcb304d
commit 1f27ef653b
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.
func NewFromString(s string) (Value, error) {
length := len(s)
if length == 0 {
return Zero, nil
}
isPercentage := s[length-1] == '%'
if isPercentage {
s = s[:length-1]
@ -538,6 +541,9 @@ func MustNewFromString(input string) Value {
func NewFromBytes(s []byte) (Value, error) {
length := len(s)
if length == 0 {
return Zero, nil
}
isPercentage := s[length-1] == '%'
if isPercentage {
s = s[:length-1]

View File

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