shopspring-numeric extension does not panic on NaN
https://github.com/jackc/pgtype/issues/169
This commit is contained in:
@@ -263,6 +263,16 @@ func (dst *Numeric) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if num.NaN {
|
||||||
|
return errors.New("cannot decode 'NaN'")
|
||||||
|
}
|
||||||
|
if num.InfinityModifier == pgtype.Infinity {
|
||||||
|
return errors.New("cannot decode 'Infinity'")
|
||||||
|
}
|
||||||
|
if num.InfinityModifier == pgtype.NegativeInfinity {
|
||||||
|
return errors.New("cannot decode '-Infinity'")
|
||||||
|
}
|
||||||
|
|
||||||
*dst = Numeric{Decimal: decimal.NewFromBigInt(num.Int, num.Exp), Status: pgtype.Present}
|
*dst = Numeric{Decimal: decimal.NewFromBigInt(num.Int, num.Exp), Status: pgtype.Present}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package numeric_test
|
package numeric_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -93,6 +94,15 @@ func TestNumericNormalize(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNumericNaN(t *testing.T) {
|
||||||
|
conn := testutil.MustConnectPgx(t)
|
||||||
|
defer testutil.MustCloseContext(t, conn)
|
||||||
|
|
||||||
|
var n shopspring.Numeric
|
||||||
|
err := conn.QueryRow(context.Background(), `select 'NaN'::numeric`).Scan(&n)
|
||||||
|
require.EqualError(t, err, `can't scan into dest[0]: cannot decode 'NaN'`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestNumericTranscode(t *testing.T) {
|
func TestNumericTranscode(t *testing.T) {
|
||||||
testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{
|
testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{
|
||||||
&shopspring.Numeric{Decimal: mustParseDecimal(t, "0"), Status: pgtype.Present},
|
&shopspring.Numeric{Decimal: mustParseDecimal(t, "0"), Status: pgtype.Present},
|
||||||
|
|||||||
Reference in New Issue
Block a user