package pgtype
import (
)
type Float4 struct {
Float32 float32
Valid bool
}
func ( *Float4) ( Float8) error {
* = Float4{Float32: float32(.Float64), Valid: .Valid}
return nil
}
func ( Float4) () (Float8, error) {
return Float8{Float64: float64(.Float32), Valid: .Valid}, nil
}
func ( *Float4) ( Int8) error {
* = Float4{Float32: float32(.Int64), Valid: .Valid}
return nil
}
func ( Float4) () (Int8, error) {
return Int8{Int64: int64(.Float32), Valid: .Valid}, nil
}
func ( *Float4) ( any) error {
if == nil {
* = Float4{}
return nil
}
switch src := .(type) {
case float64:
* = Float4{Float32: float32(), Valid: true}
return nil
case string:
, := strconv.ParseFloat(string(), 32)
if != nil {
return
}
* = Float4{Float32: float32(), Valid: true}
return nil
}
return fmt.Errorf("cannot scan %T", )
}
func ( Float4) () (driver.Value, error) {
if !.Valid {
return nil, nil
}
return float64(.Float32), nil
}
func ( Float4) () ([]byte, error) {
if !.Valid {
return []byte("null"), nil
}
return json.Marshal(.Float32)
}
func ( *Float4) ( []byte) error {
var *float32
:= json.Unmarshal(, &)
if != nil {
return
}
if == nil {
* = Float4{}
} else {
* = Float4{Float32: *, Valid: true}
}
return nil
}
type Float4Codec struct{}
func (Float4Codec) ( int16) bool {
return == TextFormatCode || == BinaryFormatCode
}
func (Float4Codec) () int16 {
return BinaryFormatCode
}
func (Float4Codec) ( *Map, uint32, int16, any) EncodePlan {
switch {
case BinaryFormatCode:
switch .(type) {
case float32:
return encodePlanFloat4CodecBinaryFloat32{}
case Float64Valuer:
return encodePlanFloat4CodecBinaryFloat64Valuer{}
case Int64Valuer:
return encodePlanFloat4CodecBinaryInt64Valuer{}
}
case TextFormatCode:
switch .(type) {
case float32:
return encodePlanTextFloat32{}
case Float64Valuer:
return encodePlanTextFloat64Valuer{}
case Int64Valuer:
return encodePlanTextInt64Valuer{}
}
}
return nil
}
type encodePlanFloat4CodecBinaryFloat32 struct{}
func (encodePlanFloat4CodecBinaryFloat32) ( any, []byte) ( []byte, error) {
:= .(float32)
return pgio.AppendUint32(, math.Float32bits()), nil
}
type encodePlanTextFloat32 struct{}
func (encodePlanTextFloat32) ( any, []byte) ( []byte, error) {
:= .(float32)
return append(, strconv.FormatFloat(float64(), 'f', -1, 32)...), nil
}
type encodePlanFloat4CodecBinaryFloat64Valuer struct{}
func (encodePlanFloat4CodecBinaryFloat64Valuer) ( any, []byte) ( []byte, error) {
, := .(Float64Valuer).Float64Value()
if != nil {
return nil,
}
if !.Valid {
return nil, nil
}
return pgio.AppendUint32(, math.Float32bits(float32(.Float64))), nil
}
type encodePlanFloat4CodecBinaryInt64Valuer struct{}
func (encodePlanFloat4CodecBinaryInt64Valuer) ( any, []byte) ( []byte, error) {
, := .(Int64Valuer).Int64Value()
if != nil {
return nil,
}
if !.Valid {
return nil, nil
}
:= float32(.Int64)
return pgio.AppendUint32(, math.Float32bits()), nil
}
func (Float4Codec) ( *Map, uint32, int16, any) ScanPlan {
switch {
case BinaryFormatCode:
switch .(type) {
case *float32:
return scanPlanBinaryFloat4ToFloat32{}
case Float64Scanner:
return scanPlanBinaryFloat4ToFloat64Scanner{}
case Int64Scanner:
return scanPlanBinaryFloat4ToInt64Scanner{}
case TextScanner:
return scanPlanBinaryFloat4ToTextScanner{}
}
case TextFormatCode:
switch .(type) {
case *float32:
return scanPlanTextAnyToFloat32{}
case Float64Scanner:
return scanPlanTextAnyToFloat64Scanner{}
case Int64Scanner:
return scanPlanTextAnyToInt64Scanner{}
}
}
return nil
}
type scanPlanBinaryFloat4ToFloat32 struct{}
func (scanPlanBinaryFloat4ToFloat32) ( []byte, any) error {
if == nil {
return fmt.Errorf("cannot scan NULL into %T", )
}
if len() != 4 {
return fmt.Errorf("invalid length for float4: %v", len())
}
:= int32(binary.BigEndian.Uint32())
:= ().(*float32)
* = math.Float32frombits(uint32())
return nil
}
type scanPlanBinaryFloat4ToFloat64Scanner struct{}
func (scanPlanBinaryFloat4ToFloat64Scanner) ( []byte, any) error {
:= ().(Float64Scanner)
if == nil {
return .ScanFloat64(Float8{})
}
if len() != 4 {
return fmt.Errorf("invalid length for float4: %v", len())
}
:= int32(binary.BigEndian.Uint32())
return .ScanFloat64(Float8{Float64: float64(math.Float32frombits(uint32())), Valid: true})
}
type scanPlanBinaryFloat4ToInt64Scanner struct{}
func (scanPlanBinaryFloat4ToInt64Scanner) ( []byte, any) error {
:= ().(Int64Scanner)
if == nil {
return .ScanInt64(Int8{})
}
if len() != 4 {
return fmt.Errorf("invalid length for float4: %v", len())
}
:= int32(binary.BigEndian.Uint32())
:= math.Float32frombits(uint32())
:= int64()
if != float32() {
return fmt.Errorf("cannot losslessly convert %v to int64", )
}
return .ScanInt64(Int8{Int64: , Valid: true})
}
type scanPlanBinaryFloat4ToTextScanner struct{}
func (scanPlanBinaryFloat4ToTextScanner) ( []byte, any) error {
:= ().(TextScanner)
if == nil {
return .ScanText(Text{})
}
if len() != 4 {
return fmt.Errorf("invalid length for float4: %v", len())
}
:= int32(binary.BigEndian.Uint32())
:= math.Float32frombits(uint32())
return .ScanText(Text{String: strconv.FormatFloat(float64(), 'f', -1, 32), Valid: true})
}
type scanPlanTextAnyToFloat32 struct{}
func (scanPlanTextAnyToFloat32) ( []byte, any) error {
if == nil {
return fmt.Errorf("cannot scan NULL into %T", )
}
, := strconv.ParseFloat(string(), 32)
if != nil {
return
}
:= ().(*float32)
* = float32()
return nil
}
func ( Float4Codec) ( *Map, uint32, int16, []byte) (driver.Value, error) {
if == nil {
return nil, nil
}
var float32
:= codecScan(, , , , , &)
if != nil {
return nil,
}
return float64(), nil
}
func ( Float4Codec) ( *Map, uint32, int16, []byte) (any, error) {
if == nil {
return nil, nil
}
var float32
:= codecScan(, , , , , &)
if != nil {
return nil,
}
return , nil
}