pgtype Float4 and Float8 fields include bit size

e.g. Instead of Float it is Float64. This matches the pattern set by the
database/sql types.
query-exec-mode
Jack Christensen 2022-03-05 09:20:03 -06:00
parent d723a4ab6f
commit 84a3d91322
9 changed files with 57 additions and 57 deletions

View File

@ -279,13 +279,13 @@ func (w *float32Wrapper) ScanFloat64(v Float8) error {
return fmt.Errorf("cannot scan NULL into *float32")
}
*w = float32Wrapper(v.Float)
*w = float32Wrapper(v.Float64)
return nil
}
func (w float32Wrapper) Float64Value() (Float8, error) {
return Float8{Float: float64(w), Valid: true}, nil
return Float8{Float64: float64(w), Valid: true}, nil
}
type float64Wrapper float64
@ -315,13 +315,13 @@ func (w *float64Wrapper) ScanFloat64(v Float8) error {
return fmt.Errorf("cannot scan NULL into *float64")
}
*w = float64Wrapper(v.Float)
*w = float64Wrapper(v.Float64)
return nil
}
func (w float64Wrapper) Float64Value() (Float8, error) {
return Float8{Float: float64(w), Valid: true}, nil
return Float8{Float64: float64(w), Valid: true}, nil
}
type stringWrapper string

View File

@ -11,27 +11,27 @@ import (
)
type Float4 struct {
Float float32
Valid bool
Float32 float32
Valid bool
}
// ScanFloat64 implements the Float64Scanner interface.
func (f *Float4) ScanFloat64(n Float8) error {
*f = Float4{Float: float32(n.Float), Valid: n.Valid}
*f = Float4{Float32: float32(n.Float64), Valid: n.Valid}
return nil
}
func (f Float4) Float64Value() (Float8, error) {
return Float8{Float: float64(f.Float), Valid: f.Valid}, nil
return Float8{Float64: float64(f.Float32), Valid: f.Valid}, nil
}
func (f *Float4) ScanInt64(n Int8) error {
*f = Float4{Float: float32(n.Int64), Valid: n.Valid}
*f = Float4{Float32: float32(n.Int64), Valid: n.Valid}
return nil
}
func (f Float4) Int64Value() (Int8, error) {
return Int8{Int64: int64(f.Float), Valid: f.Valid}, nil
return Int8{Int64: int64(f.Float32), Valid: f.Valid}, nil
}
// Scan implements the database/sql Scanner interface.
@ -43,14 +43,14 @@ func (f *Float4) Scan(src interface{}) error {
switch src := src.(type) {
case float64:
*f = Float4{Float: float32(src), Valid: true}
*f = Float4{Float32: float32(src), Valid: true}
return nil
case string:
n, err := strconv.ParseFloat(string(src), 32)
if err != nil {
return err
}
*f = Float4{Float: float32(n), Valid: true}
*f = Float4{Float32: float32(n), Valid: true}
return nil
}
@ -62,7 +62,7 @@ func (f Float4) Value() (driver.Value, error) {
if !f.Valid {
return nil, nil
}
return float64(f.Float), nil
return float64(f.Float32), nil
}
type Float4Codec struct{}
@ -126,7 +126,7 @@ func (encodePlanFloat4CodecBinaryFloat64Valuer) Encode(value interface{}, buf []
return nil, nil
}
return pgio.AppendUint32(buf, math.Float32bits(float32(n.Float))), nil
return pgio.AppendUint32(buf, math.Float32bits(float32(n.Float64))), nil
}
type encodePlanFloat4CodecBinaryInt64Valuer struct{}
@ -203,7 +203,7 @@ func (scanPlanBinaryFloat4ToFloat64Scanner) Scan(src []byte, dst interface{}) er
}
n := int32(binary.BigEndian.Uint32(src))
return s.ScanFloat64(Float8{Float: float64(math.Float32frombits(uint32(n))), Valid: true})
return s.ScanFloat64(Float8{Float64: float64(math.Float32frombits(uint32(n))), Valid: true})
}
type scanPlanBinaryFloat4ToInt64Scanner struct{}

View File

@ -9,9 +9,9 @@ import (
func TestFloat4Codec(t *testing.T) {
testutil.RunTranscodeTests(t, "float4", []testutil.TranscodeTestCase{
{pgtype.Float4{Float: -1, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float: -1, Valid: true})},
{pgtype.Float4{Float: 0, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float: 0, Valid: true})},
{pgtype.Float4{Float: 1, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float: 1, Valid: true})},
{pgtype.Float4{Float32: -1, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float32: -1, Valid: true})},
{pgtype.Float4{Float32: 0, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float32: 0, Valid: true})},
{pgtype.Float4{Float32: 1, Valid: true}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{Float32: 1, Valid: true})},
{float32(0.00001), new(float32), isExpectedEq(float32(0.00001))},
{float32(9999.99), new(float32), isExpectedEq(float32(9999.99))},
{pgtype.Float4{}, new(pgtype.Float4), isExpectedEq(pgtype.Float4{})},

View File

@ -19,8 +19,8 @@ type Float64Valuer interface {
}
type Float8 struct {
Float float64
Valid bool
Float64 float64
Valid bool
}
// ScanFloat64 implements the Float64Scanner interface.
@ -34,12 +34,12 @@ func (f Float8) Float64Value() (Float8, error) {
}
func (f *Float8) ScanInt64(n Int8) error {
*f = Float8{Float: float64(n.Int64), Valid: n.Valid}
*f = Float8{Float64: float64(n.Int64), Valid: n.Valid}
return nil
}
func (f Float8) Int64Value() (Int8, error) {
return Int8{Int64: int64(f.Float), Valid: f.Valid}, nil
return Int8{Int64: int64(f.Float64), Valid: f.Valid}, nil
}
// Scan implements the database/sql Scanner interface.
@ -51,14 +51,14 @@ func (f *Float8) Scan(src interface{}) error {
switch src := src.(type) {
case float64:
*f = Float8{Float: src, Valid: true}
*f = Float8{Float64: src, Valid: true}
return nil
case string:
n, err := strconv.ParseFloat(string(src), 64)
if err != nil {
return err
}
*f = Float8{Float: n, Valid: true}
*f = Float8{Float64: n, Valid: true}
return nil
}
@ -70,7 +70,7 @@ func (f Float8) Value() (driver.Value, error) {
if !f.Valid {
return nil, nil
}
return f.Float, nil
return f.Float64, nil
}
type Float8Codec struct{}
@ -134,7 +134,7 @@ func (encodePlanFloat8CodecBinaryFloat64Valuer) Encode(value interface{}, buf []
return nil, nil
}
return pgio.AppendUint64(buf, math.Float64bits(n.Float)), nil
return pgio.AppendUint64(buf, math.Float64bits(n.Float64)), nil
}
type encodePlanTextFloat64Valuer struct{}
@ -149,7 +149,7 @@ func (encodePlanTextFloat64Valuer) Encode(value interface{}, buf []byte) (newBuf
return nil, nil
}
return append(buf, strconv.FormatFloat(n.Float, 'f', -1, 64)...), nil
return append(buf, strconv.FormatFloat(n.Float64, 'f', -1, 64)...), nil
}
type encodePlanFloat8CodecBinaryInt64Valuer struct{}
@ -241,7 +241,7 @@ func (scanPlanBinaryFloat8ToFloat64Scanner) Scan(src []byte, dst interface{}) er
}
n := int64(binary.BigEndian.Uint64(src))
return s.ScanFloat64(Float8{Float: math.Float64frombits(uint64(n)), Valid: true})
return s.ScanFloat64(Float8{Float64: math.Float64frombits(uint64(n)), Valid: true})
}
type scanPlanBinaryFloat8ToInt64Scanner struct{}
@ -299,7 +299,7 @@ func (scanPlanTextAnyToFloat64Scanner) Scan(src []byte, dst interface{}) error {
return err
}
return s.ScanFloat64(Float8{Float: n, Valid: true})
return s.ScanFloat64(Float8{Float64: n, Valid: true})
}
func (c Float8Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) {

View File

@ -9,9 +9,9 @@ import (
func TestFloat8Codec(t *testing.T) {
testutil.RunTranscodeTests(t, "float8", []testutil.TranscodeTestCase{
{pgtype.Float8{Float: -1, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float: -1, Valid: true})},
{pgtype.Float8{Float: 0, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float: 0, Valid: true})},
{pgtype.Float8{Float: 1, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float: 1, Valid: true})},
{pgtype.Float8{Float64: -1, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float64: -1, Valid: true})},
{pgtype.Float8{Float64: 0, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float64: 0, Valid: true})},
{pgtype.Float8{Float64: 1, Valid: true}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{Float64: 1, Valid: true})},
{float64(0.00001), new(float64), isExpectedEq(float64(0.00001))},
{float64(9999.99), new(float64), isExpectedEq(float64(9999.99))},
{pgtype.Float8{}, new(pgtype.Float8), isExpectedEq(pgtype.Float8{})},

View File

@ -84,11 +84,11 @@ func (n Numeric) Float64Value() (Float8, error) {
if !n.Valid {
return Float8{}, nil
} else if n.NaN {
return Float8{Float: math.NaN(), Valid: true}, nil
return Float8{Float64: math.NaN(), Valid: true}, nil
} else if n.InfinityModifier == Infinity {
return Float8{Float: math.Inf(1), Valid: true}, nil
return Float8{Float64: math.Inf(1), Valid: true}, nil
} else if n.InfinityModifier == NegativeInfinity {
return Float8{Float: math.Inf(-1), Valid: true}, nil
return Float8{Float64: math.Inf(-1), Valid: true}, nil
}
buf := make([]byte, 0, 32)
@ -106,7 +106,7 @@ func (n Numeric) Float64Value() (Float8, error) {
return Float8{}, err
}
return Float8{Float: f, Valid: true}, nil
return Float8{Float64: f, Valid: true}, nil
}
func (n *Numeric) toBigInt() (*big.Int, error) {
@ -297,14 +297,14 @@ func (encodePlanNumericCodecBinaryFloat64Valuer) Encode(value interface{}, buf [
return nil, nil
}
if math.IsNaN(n.Float) {
if math.IsNaN(n.Float64) {
return encodeNumericBinary(Numeric{NaN: true, Valid: true}, buf)
} else if math.IsInf(n.Float, 1) {
} else if math.IsInf(n.Float64, 1) {
return encodeNumericBinary(Numeric{InfinityModifier: Infinity, Valid: true}, buf)
} else if math.IsInf(n.Float, -1) {
} else if math.IsInf(n.Float64, -1) {
return encodeNumericBinary(Numeric{InfinityModifier: NegativeInfinity, Valid: true}, buf)
}
num, exp, err := parseNumericString(strconv.FormatFloat(n.Float, 'f', -1, 64))
num, exp, err := parseNumericString(strconv.FormatFloat(n.Float64, 'f', -1, 64))
if err != nil {
return nil, err
}
@ -449,14 +449,14 @@ func (encodePlanNumericCodecTextFloat64Valuer) Encode(value interface{}, buf []b
return nil, nil
}
if math.IsNaN(n.Float) {
if math.IsNaN(n.Float64) {
return encodeNumericBinary(Numeric{NaN: true, Valid: true}, buf)
} else if math.IsInf(n.Float, 1) {
} else if math.IsInf(n.Float64, 1) {
return encodeNumericBinary(Numeric{InfinityModifier: Infinity, Valid: true}, buf)
} else if math.IsInf(n.Float, -1) {
} else if math.IsInf(n.Float64, -1) {
return encodeNumericBinary(Numeric{InfinityModifier: NegativeInfinity, Valid: true}, buf)
}
num, exp, err := parseNumericString(strconv.FormatFloat(n.Float, 'f', -1, 64))
num, exp, err := parseNumericString(strconv.FormatFloat(n.Float64, 'f', -1, 64))
if err != nil {
return nil, err
}

View File

@ -123,11 +123,11 @@ func TestNumericFloat64Valuer(t *testing.T) {
n pgtype.Numeric
f pgtype.Float8
}{
{mustParseNumeric(t, "1"), pgtype.Float8{Float: 1, Valid: true}},
{mustParseNumeric(t, "0.0000000000000000001"), pgtype.Float8{Float: 0.0000000000000000001, Valid: true}},
{mustParseNumeric(t, "-99999999999"), pgtype.Float8{Float: -99999999999, Valid: true}},
{pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true}, pgtype.Float8{Float: math.Inf(1), Valid: true}},
{pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, pgtype.Float8{Float: math.Inf(-1), Valid: true}},
{mustParseNumeric(t, "1"), pgtype.Float8{Float64: 1, Valid: true}},
{mustParseNumeric(t, "0.0000000000000000001"), pgtype.Float8{Float64: 0.0000000000000000001, Valid: true}},
{mustParseNumeric(t, "-99999999999"), pgtype.Float8{Float64: -99999999999, Valid: true}},
{pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true}, pgtype.Float8{Float64: math.Inf(1), Valid: true}},
{pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, pgtype.Float8{Float64: math.Inf(-1), Valid: true}},
{pgtype.Numeric{Valid: true}, pgtype.Float8{Valid: true}},
{pgtype.Numeric{}, pgtype.Float8{}},
} {
@ -138,7 +138,7 @@ func TestNumericFloat64Valuer(t *testing.T) {
f, err := pgtype.Numeric{NaN: true, Valid: true}.Float64Value()
assert.NoError(t, err)
assert.True(t, math.IsNaN(f.Float))
assert.True(t, math.IsNaN(f.Float64))
assert.True(t, f.Valid)
}

View File

@ -46,15 +46,15 @@ func TestRangeCodecTranscodeCompatibleRangeElementTypes(t *testing.T) {
{
pgtype.Float8range{
LowerType: pgtype.Inclusive,
Lower: pgtype.Float8{Float: 1, Valid: true},
Upper: pgtype.Float8{Float: 5, Valid: true},
Lower: pgtype.Float8{Float64: 1, Valid: true},
Upper: pgtype.Float8{Float64: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
},
new(pgtype.Float8range),
isExpectedEq(pgtype.Float8range{
LowerType: pgtype.Inclusive,
Lower: pgtype.Float8{Float: 1, Valid: true},
Upper: pgtype.Float8{Float: 5, Valid: true},
Lower: pgtype.Float8{Float64: 1, Valid: true},
Upper: pgtype.Float8{Float64: 5, Valid: true},
UpperType: pgtype.Exclusive, Valid: true,
}),
},

View File

@ -17,7 +17,7 @@ func (f *Float8) ScanFloat64(n pgtype.Float8) error {
return nil
}
*f = Float8(n.Float)
*f = Float8(n.Float64)
return nil
}
@ -26,7 +26,7 @@ func (f Float8) Float64Value() (pgtype.Float8, error) {
if f == 0 {
return pgtype.Float8{}, nil
}
return pgtype.Float8{Float: float64(f), Valid: true}, nil
return pgtype.Float8{Float64: float64(f), Valid: true}, nil
}
// Scan implements the database/sql Scanner interface.
@ -42,7 +42,7 @@ func (f *Float8) Scan(src interface{}) error {
return err
}
*f = Float8(nullable.Float)
*f = Float8(nullable.Float64)
return nil
}