Decode(Text|Binary) now accepts []byte instead of io.Reader
This commit is contained in:
+9
-27
@@ -1,6 +1,7 @@
|
||||
package pgtype
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
@@ -72,24 +73,13 @@ func (src *Timestamp) AssignTo(dst interface{}) error {
|
||||
|
||||
// DecodeText decodes from src into dst. The decoded time is considered to
|
||||
// be in UTC.
|
||||
func (dst *Timestamp) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
func (dst *Timestamp) DecodeText(src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Timestamp{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
buf := make([]byte, int(size))
|
||||
_, err = r.Read(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sbuf := string(buf)
|
||||
sbuf := string(src)
|
||||
switch sbuf {
|
||||
case "infinity":
|
||||
*dst = Timestamp{Status: Present, InfinityModifier: Infinity}
|
||||
@@ -109,25 +99,17 @@ func (dst *Timestamp) DecodeText(r io.Reader) error {
|
||||
|
||||
// DecodeBinary decodes from src into dst. The decoded time is considered to
|
||||
// be in UTC.
|
||||
func (dst *Timestamp) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
func (dst *Timestamp) DecodeBinary(src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Timestamp{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
if size != 8 {
|
||||
return fmt.Errorf("invalid length for timestamp: %v", size)
|
||||
if len(src) != 8 {
|
||||
return fmt.Errorf("invalid length for timestamp: %v", len(src))
|
||||
}
|
||||
|
||||
microsecSinceY2K, err := pgio.ReadInt64(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
microsecSinceY2K := int64(binary.BigEndian.Uint64(src))
|
||||
|
||||
switch microsecSinceY2K {
|
||||
case infinityMicrosecondOffset:
|
||||
|
||||
Reference in New Issue
Block a user