From 3cbb81631a4047fa418c9ec594a63d861cd0e8fa Mon Sep 17 00:00:00 2001 From: leighhopcroft Date: Tue, 2 Jun 2020 18:35:58 +0100 Subject: [PATCH] added NaN support to Numeric.Set --- numeric.go | 6 ++++++ numeric_test.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/numeric.go b/numeric.go index fc8e1789..f4ddb789 100644 --- a/numeric.go +++ b/numeric.go @@ -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 diff --git a/numeric_test.go b/numeric_test.go index 263c78b6..3b099b55 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -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 {