2
0

Decode(Text|Binary) now accepts []byte instead of io.Reader

This commit is contained in:
Jack Christensen
2017-03-10 16:08:47 -06:00
parent ac9228a1a3
commit 8162634259
38 changed files with 506 additions and 855 deletions
+9 -27
View File
@@ -1,6 +1,7 @@
package pgtype
import (
"encoding/binary"
"fmt"
"io"
"math"
@@ -92,24 +93,13 @@ func (src *Float4) AssignTo(dst interface{}) error {
return float64AssignTo(float64(src.Float), src.Status, dst)
}
func (dst *Float4) DecodeText(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {
return err
}
if size == -1 {
func (dst *Float4) DecodeText(src []byte) error {
if src == nil {
*dst = Float4{Status: Null}
return nil
}
buf := make([]byte, int(size))
_, err = r.Read(buf)
if err != nil {
return err
}
n, err := strconv.ParseFloat(string(buf), 32)
n, err := strconv.ParseFloat(string(src), 32)
if err != nil {
return err
}
@@ -118,25 +108,17 @@ func (dst *Float4) DecodeText(r io.Reader) error {
return nil
}
func (dst *Float4) DecodeBinary(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {
return err
}
if size == -1 {
func (dst *Float4) DecodeBinary(src []byte) error {
if src == nil {
*dst = Float4{Status: Null}
return nil
}
if size != 4 {
return fmt.Errorf("invalid length for float4: %v", size)
if len(src) != 4 {
return fmt.Errorf("invalid length for float4: %v", len(src))
}
n, err := pgio.ReadInt32(r)
if err != nil {
return err
}
n := int32(binary.BigEndian.Uint32(src))
*dst = Float4{Float: math.Float32frombits(uint32(n)), Status: Present}
return nil