mirror of https://github.com/jackc/pgx.git
Add infinity support for Numeric Set/Get
parent
a29019de9d
commit
e0f9fc5212
26
numeric.go
26
numeric.go
|
@ -49,10 +49,11 @@ var bigNBaseX3 *big.Int = big.NewInt(nbase * nbase * nbase)
|
||||||
var bigNBaseX4 *big.Int = big.NewInt(nbase * nbase * nbase * nbase)
|
var bigNBaseX4 *big.Int = big.NewInt(nbase * nbase * nbase * nbase)
|
||||||
|
|
||||||
type Numeric struct {
|
type Numeric struct {
|
||||||
Int *big.Int
|
Int *big.Int
|
||||||
Exp int32
|
Exp int32
|
||||||
Status Status
|
Status Status
|
||||||
NaN bool
|
NaN bool
|
||||||
|
InfinityModifier InfinityModifier
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Numeric) Set(src interface{}) error {
|
func (dst *Numeric) Set(src interface{}) error {
|
||||||
|
@ -73,6 +74,12 @@ func (dst *Numeric) Set(src interface{}) error {
|
||||||
if math.IsNaN(float64(value)) {
|
if math.IsNaN(float64(value)) {
|
||||||
*dst = Numeric{Status: Present, NaN: true}
|
*dst = Numeric{Status: Present, NaN: true}
|
||||||
return nil
|
return nil
|
||||||
|
} else if math.IsInf(float64(value), 1) {
|
||||||
|
*dst = Numeric{Status: Present, InfinityModifier: Infinity}
|
||||||
|
return nil
|
||||||
|
} else if math.IsInf(float64(value), -1) {
|
||||||
|
*dst = Numeric{Status: Present, InfinityModifier: NegativeInfinity}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64))
|
num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -83,6 +90,12 @@ func (dst *Numeric) Set(src interface{}) error {
|
||||||
if math.IsNaN(value) {
|
if math.IsNaN(value) {
|
||||||
*dst = Numeric{Status: Present, NaN: true}
|
*dst = Numeric{Status: Present, NaN: true}
|
||||||
return nil
|
return nil
|
||||||
|
} else if math.IsInf(value, 1) {
|
||||||
|
*dst = Numeric{Status: Present, InfinityModifier: Infinity}
|
||||||
|
return nil
|
||||||
|
} else if math.IsInf(value, -1) {
|
||||||
|
*dst = Numeric{Status: Present, InfinityModifier: NegativeInfinity}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64))
|
num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,6 +206,8 @@ func (dst *Numeric) Set(src interface{}) error {
|
||||||
} else {
|
} else {
|
||||||
return dst.Set(*value)
|
return dst.Set(*value)
|
||||||
}
|
}
|
||||||
|
case InfinityModifier:
|
||||||
|
*dst = Numeric{InfinityModifier: value, Status: Present}
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingNumberType(src); ok {
|
if originalSrc, ok := underlyingNumberType(src); ok {
|
||||||
return dst.Set(originalSrc)
|
return dst.Set(originalSrc)
|
||||||
|
@ -206,6 +221,9 @@ func (dst *Numeric) Set(src interface{}) error {
|
||||||
func (dst Numeric) Get() interface{} {
|
func (dst Numeric) Get() interface{} {
|
||||||
switch dst.Status {
|
switch dst.Status {
|
||||||
case Present:
|
case Present:
|
||||||
|
if dst.InfinityModifier != None {
|
||||||
|
return dst.InfinityModifier
|
||||||
|
}
|
||||||
return dst
|
return dst
|
||||||
case Null:
|
case Null:
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -222,6 +222,12 @@ func TestNumericSet(t *testing.T) {
|
||||||
{source: float64(12345.678901), result: &pgtype.Numeric{Int: big.NewInt(12345678901), Exp: -6, 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.Present, NaN: true}},
|
{source: math.NaN(), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Present, NaN: true}},
|
||||||
{source: float32(math.NaN()), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Present, NaN: true}},
|
{source: float32(math.NaN()), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Present, NaN: true}},
|
||||||
|
{source: pgtype.Infinity, result: &pgtype.Numeric{InfinityModifier: pgtype.Infinity, Status: pgtype.Present}},
|
||||||
|
{source: math.Inf(1), result: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}},
|
||||||
|
{source: float32(math.Inf(1)), result: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}},
|
||||||
|
{source: pgtype.NegativeInfinity, result: &pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Status: pgtype.Present}},
|
||||||
|
{source: math.Inf(-1), result: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.NegativeInfinity}},
|
||||||
|
{source: float32(math.Inf(1)), result: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
|
|
Loading…
Reference in New Issue