added NaN support to Numeric.Set

non-blocking
leighhopcroft 2020-06-02 18:35:58 +01:00
parent 9d847241cb
commit 3cbb81631a
2 changed files with 8 additions and 0 deletions

View File

@ -64,12 +64,18 @@ func (dst *Numeric) Set(src interface{}) error {
switch value := src.(type) {
case float32:
if math.IsNaN(float64(value)) {
return nil
}
num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64))
if err != nil {
return err
}
*dst = Numeric{Int: num, Exp: exp, Status: Present}
case float64:
if math.IsNaN(value) {
return nil
}
num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64))
if err != nil {
return err

View File

@ -210,6 +210,8 @@ func TestNumericSet(t *testing.T) {
{source: float64(1234), result: &pgtype.Numeric{Int: big.NewInt(1234), Exp: 0, Status: pgtype.Present}},
{source: float64(12345678900), result: &pgtype.Numeric{Int: big.NewInt(123456789), Exp: 2, Status: pgtype.Present}},
{source: float64(12345.678901), result: &pgtype.Numeric{Int: big.NewInt(12345678901), Exp: -6, Status: pgtype.Present}},
{source: math.NaN(), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Undefined}},
{source: float32(math.NaN()), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Undefined}},
}
for i, tt := range successfulTests {