mirror of https://github.com/jackc/pgx.git
Standardize receiver variable name for pgtype
Conversion functions now use standardized src and dst depending on their role.v3-numeric-wip
parent
66712f8259
commit
272f095a44
|
@ -25,34 +25,34 @@ type ArrayDimension struct {
|
|||
LowerBound int32
|
||||
}
|
||||
|
||||
func (ah *ArrayHeader) DecodeBinary(r io.Reader) error {
|
||||
func (dst *ArrayHeader) DecodeBinary(r io.Reader) error {
|
||||
numDims, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if numDims > 0 {
|
||||
ah.Dimensions = make([]ArrayDimension, numDims)
|
||||
dst.Dimensions = make([]ArrayDimension, numDims)
|
||||
}
|
||||
|
||||
containsNull, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ah.ContainsNull = containsNull == 1
|
||||
dst.ContainsNull = containsNull == 1
|
||||
|
||||
ah.ElementOID, err = pgio.ReadInt32(r)
|
||||
dst.ElementOID, err = pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range ah.Dimensions {
|
||||
ah.Dimensions[i].Length, err = pgio.ReadInt32(r)
|
||||
for i := range dst.Dimensions {
|
||||
dst.Dimensions[i].Length, err = pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ah.Dimensions[i].LowerBound, err = pgio.ReadInt32(r)
|
||||
dst.Dimensions[i].LowerBound, err = pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -61,14 +61,14 @@ func (ah *ArrayHeader) DecodeBinary(r io.Reader) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (ah *ArrayHeader) EncodeBinary(w io.Writer) error {
|
||||
_, err := pgio.WriteInt32(w, int32(len(ah.Dimensions)))
|
||||
func (src *ArrayHeader) EncodeBinary(w io.Writer) error {
|
||||
_, err := pgio.WriteInt32(w, int32(len(src.Dimensions)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var containsNull int32
|
||||
if ah.ContainsNull {
|
||||
if src.ContainsNull {
|
||||
containsNull = 1
|
||||
}
|
||||
_, err = pgio.WriteInt32(w, containsNull)
|
||||
|
@ -76,18 +76,18 @@ func (ah *ArrayHeader) EncodeBinary(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt32(w, ah.ElementOID)
|
||||
_, err = pgio.WriteInt32(w, src.ElementOID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range ah.Dimensions {
|
||||
_, err = pgio.WriteInt32(w, ah.Dimensions[i].Length)
|
||||
for i := range src.Dimensions {
|
||||
_, err = pgio.WriteInt32(w, src.Dimensions[i].Length)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt32(w, ah.Dimensions[i].LowerBound)
|
||||
_, err = pgio.WriteInt32(w, src.Dimensions[i].LowerBound)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ type UntypedTextArray struct {
|
|||
}
|
||||
|
||||
func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
|
||||
uta := &UntypedTextArray{}
|
||||
dst := &UntypedTextArray{}
|
||||
|
||||
buf := bytes.NewBufferString(src)
|
||||
|
||||
|
@ -219,7 +219,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
|
|||
if currentDim == counterDim {
|
||||
implicitDimensions[currentDim].Length++
|
||||
}
|
||||
uta.Elements = append(uta.Elements, value)
|
||||
dst.Elements = append(dst.Elements, value)
|
||||
}
|
||||
|
||||
if currentDim < 0 {
|
||||
|
@ -233,15 +233,15 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
|
|||
return nil, fmt.Errorf("unexpected trailing data: %v", buf.String())
|
||||
}
|
||||
|
||||
if len(uta.Elements) == 0 {
|
||||
uta.Dimensions = nil
|
||||
if len(dst.Elements) == 0 {
|
||||
dst.Dimensions = nil
|
||||
} else if len(explicitDimensions) > 0 {
|
||||
uta.Dimensions = explicitDimensions
|
||||
dst.Dimensions = explicitDimensions
|
||||
} else {
|
||||
uta.Dimensions = implicitDimensions
|
||||
dst.Dimensions = implicitDimensions
|
||||
}
|
||||
|
||||
return uta, nil
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func skipWhitespace(buf *bytes.Buffer) {
|
||||
|
|
|
@ -14,21 +14,21 @@ type Bool struct {
|
|||
Status Status
|
||||
}
|
||||
|
||||
func (b *Bool) ConvertFrom(src interface{}) error {
|
||||
func (dst *Bool) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Bool:
|
||||
*b = value
|
||||
*dst = value
|
||||
case bool:
|
||||
*b = Bool{Bool: value, Status: Present}
|
||||
*dst = Bool{Bool: value, Status: Present}
|
||||
case string:
|
||||
bb, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*b = Bool{Bool: bb, Status: Present}
|
||||
*dst = Bool{Bool: bb, Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingBoolType(src); ok {
|
||||
return b.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Bool", value)
|
||||
}
|
||||
|
@ -36,20 +36,20 @@ func (b *Bool) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (b *Bool) AssignTo(dst interface{}) error {
|
||||
func (src *Bool) AssignTo(dst interface{}) error {
|
||||
switch v := dst.(type) {
|
||||
case *bool:
|
||||
if b.Status != Present {
|
||||
return fmt.Errorf("cannot assign %v to %T", b, dst)
|
||||
if src.Status != Present {
|
||||
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
||||
}
|
||||
*v = b.Bool
|
||||
*v = src.Bool
|
||||
default:
|
||||
if v := reflect.ValueOf(dst); v.Kind() == reflect.Ptr {
|
||||
el := v.Elem()
|
||||
switch el.Kind() {
|
||||
// if dst is a pointer to pointer, strip the pointer and try again
|
||||
case reflect.Ptr:
|
||||
if b.Status == Null {
|
||||
if src.Status == Null {
|
||||
el.Set(reflect.Zero(el.Type()))
|
||||
return nil
|
||||
}
|
||||
|
@ -57,29 +57,29 @@ func (b *Bool) AssignTo(dst interface{}) error {
|
|||
// allocate destination
|
||||
el.Set(reflect.New(el.Type().Elem()))
|
||||
}
|
||||
return b.AssignTo(el.Interface())
|
||||
return src.AssignTo(el.Interface())
|
||||
case reflect.Bool:
|
||||
if b.Status != Present {
|
||||
return fmt.Errorf("cannot assign %v to %T", b, dst)
|
||||
if src.Status != Present {
|
||||
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
||||
}
|
||||
el.SetBool(b.Bool)
|
||||
el.SetBool(src.Bool)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot put decode %v into %T", b, dst)
|
||||
return fmt.Errorf("cannot put decode %v into %T", src, dst)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bool) DecodeText(r io.Reader) error {
|
||||
func (dst *Bool) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*b = Bool{Status: Null}
|
||||
*dst = Bool{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -92,18 +92,18 @@ func (b *Bool) DecodeText(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*b = Bool{Bool: byt == 't', Status: Present}
|
||||
*dst = Bool{Bool: byt == 't', Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bool) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Bool) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*b = Bool{Status: Null}
|
||||
*dst = Bool{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -116,12 +116,12 @@ func (b *Bool) DecodeBinary(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*b = Bool{Bool: byt == 1, Status: Present}
|
||||
*dst = Bool{Bool: byt == 1, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Bool) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, b.Status); done {
|
||||
func (src Bool) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ func (b Bool) EncodeText(w io.Writer) error {
|
|||
}
|
||||
|
||||
var buf []byte
|
||||
if b.Bool {
|
||||
if src.Bool {
|
||||
buf = []byte{'t'}
|
||||
} else {
|
||||
buf = []byte{'f'}
|
||||
|
@ -141,8 +141,8 @@ func (b Bool) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (b Bool) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, b.Status); done {
|
||||
func (src Bool) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ func (b Bool) EncodeBinary(w io.Writer) error {
|
|||
}
|
||||
|
||||
var buf []byte
|
||||
if b.Bool {
|
||||
if src.Bool {
|
||||
buf = []byte{1}
|
||||
} else {
|
||||
buf = []byte{0}
|
||||
|
|
|
@ -20,15 +20,15 @@ const (
|
|||
infinityDayOffset = 2147483647
|
||||
)
|
||||
|
||||
func (d *Date) ConvertFrom(src interface{}) error {
|
||||
func (dst *Date) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Date:
|
||||
*d = value
|
||||
*dst = value
|
||||
case time.Time:
|
||||
*d = Date{Time: value, Status: Present}
|
||||
*dst = Date{Time: value, Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingTimeType(src); ok {
|
||||
return d.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Date", value)
|
||||
}
|
||||
|
@ -36,20 +36,20 @@ func (d *Date) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (d *Date) AssignTo(dst interface{}) error {
|
||||
func (src *Date) AssignTo(dst interface{}) error {
|
||||
switch v := dst.(type) {
|
||||
case *time.Time:
|
||||
if d.Status != Present || d.InfinityModifier != None {
|
||||
return fmt.Errorf("cannot assign %v to %T", d, dst)
|
||||
if src.Status != Present || src.InfinityModifier != None {
|
||||
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
||||
}
|
||||
*v = d.Time
|
||||
*v = src.Time
|
||||
default:
|
||||
if v := reflect.ValueOf(dst); v.Kind() == reflect.Ptr {
|
||||
el := v.Elem()
|
||||
switch el.Kind() {
|
||||
// if dst is a pointer to pointer, strip the pointer and try again
|
||||
case reflect.Ptr:
|
||||
if d.Status == Null {
|
||||
if src.Status == Null {
|
||||
if !el.IsNil() {
|
||||
// if the destination pointer is not nil, nil it out
|
||||
el.Set(reflect.Zero(el.Type()))
|
||||
|
@ -60,23 +60,23 @@ func (d *Date) AssignTo(dst interface{}) error {
|
|||
// allocate destination
|
||||
el.Set(reflect.New(el.Type().Elem()))
|
||||
}
|
||||
return d.AssignTo(el.Interface())
|
||||
return src.AssignTo(el.Interface())
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot decode %v into %T", d, dst)
|
||||
return fmt.Errorf("cannot decode %v into %T", src, dst)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Date) DecodeText(r io.Reader) error {
|
||||
func (dst *Date) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*d = Date{Status: Null}
|
||||
*dst = Date{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -89,29 +89,29 @@ func (d *Date) DecodeText(r io.Reader) error {
|
|||
sbuf := string(buf)
|
||||
switch sbuf {
|
||||
case "infinity":
|
||||
*d = Date{Status: Present, InfinityModifier: Infinity}
|
||||
*dst = Date{Status: Present, InfinityModifier: Infinity}
|
||||
case "-infinity":
|
||||
*d = Date{Status: Present, InfinityModifier: -Infinity}
|
||||
*dst = Date{Status: Present, InfinityModifier: -Infinity}
|
||||
default:
|
||||
t, err := time.ParseInLocation("2006-01-02", sbuf, time.UTC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d = Date{Time: t, Status: Present}
|
||||
*dst = Date{Time: t, Status: Present}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Date) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Date) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*d = Date{Status: Null}
|
||||
*dst = Date{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -126,27 +126,27 @@ func (d *Date) DecodeBinary(r io.Reader) error {
|
|||
|
||||
switch dayOffset {
|
||||
case infinityDayOffset:
|
||||
*d = Date{Status: Present, InfinityModifier: Infinity}
|
||||
*dst = Date{Status: Present, InfinityModifier: Infinity}
|
||||
case negativeInfinityDayOffset:
|
||||
*d = Date{Status: Present, InfinityModifier: -Infinity}
|
||||
*dst = Date{Status: Present, InfinityModifier: -Infinity}
|
||||
default:
|
||||
t := time.Date(2000, 1, int(1+dayOffset), 0, 0, 0, 0, time.UTC)
|
||||
*d = Date{Time: t, Status: Present}
|
||||
*dst = Date{Time: t, Status: Present}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Date) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, d.Status); done {
|
||||
func (src Date) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
var s string
|
||||
|
||||
switch d.InfinityModifier {
|
||||
switch src.InfinityModifier {
|
||||
case None:
|
||||
s = d.Time.Format("2006-01-02")
|
||||
s = src.Time.Format("2006-01-02")
|
||||
case Infinity:
|
||||
s = "infinity"
|
||||
case NegativeInfinity:
|
||||
|
@ -162,8 +162,8 @@ func (d Date) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (d Date) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, d.Status); done {
|
||||
func (src Date) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -173,9 +173,9 @@ func (d Date) EncodeBinary(w io.Writer) error {
|
|||
}
|
||||
|
||||
var daysSinceDateEpoch int32
|
||||
switch d.InfinityModifier {
|
||||
switch src.InfinityModifier {
|
||||
case None:
|
||||
tUnix := time.Date(d.Time.Year(), d.Time.Month(), d.Time.Day(), 0, 0, 0, 0, time.UTC).Unix()
|
||||
tUnix := time.Date(src.Time.Year(), src.Time.Month(), src.Time.Day(), 0, 0, 0, 0, time.UTC).Unix()
|
||||
dateEpoch := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
||||
|
||||
secSinceDateEpoch := tUnix - dateEpoch
|
||||
|
|
|
@ -14,21 +14,21 @@ type Int2 struct {
|
|||
Status Status
|
||||
}
|
||||
|
||||
func (i *Int2) ConvertFrom(src interface{}) error {
|
||||
func (dst *Int2) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Int2:
|
||||
*i = value
|
||||
*dst = value
|
||||
case int8:
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case uint8:
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case int16:
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case uint16:
|
||||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case int32:
|
||||
if value < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
|
@ -36,12 +36,12 @@ func (i *Int2) ConvertFrom(src interface{}) error {
|
|||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case uint32:
|
||||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case int64:
|
||||
if value < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
|
@ -49,12 +49,12 @@ func (i *Int2) ConvertFrom(src interface{}) error {
|
|||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case uint64:
|
||||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case int:
|
||||
if value < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
|
@ -62,21 +62,21 @@ func (i *Int2) ConvertFrom(src interface{}) error {
|
|||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case uint:
|
||||
if value > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||
}
|
||||
*i = Int2{Int: int16(value), Status: Present}
|
||||
*dst = Int2{Int: int16(value), Status: Present}
|
||||
case string:
|
||||
num, err := strconv.ParseInt(value, 10, 16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*i = Int2{Int: int16(num), Status: Present}
|
||||
*dst = Int2{Int: int16(num), Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingIntType(src); ok {
|
||||
return i.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Int2", value)
|
||||
}
|
||||
|
@ -84,18 +84,18 @@ func (i *Int2) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *Int2) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(i.Int), i.Status, dst)
|
||||
func (src *Int2) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(src.Int), src.Status, dst)
|
||||
}
|
||||
|
||||
func (i *Int2) DecodeText(r io.Reader) error {
|
||||
func (dst *Int2) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int2{Status: Null}
|
||||
*dst = Int2{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -110,18 +110,18 @@ func (i *Int2) DecodeText(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int2{Int: int16(n), Status: Present}
|
||||
*dst = Int2{Int: int16(n), Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Int2) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Int2) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int2{Status: Null}
|
||||
*dst = Int2{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -134,16 +134,16 @@ func (i *Int2) DecodeBinary(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int2{Int: int16(n), Status: Present}
|
||||
*dst = Int2{Int: int16(n), Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int2) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int2) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
s := strconv.FormatInt(int64(i.Int), 10)
|
||||
s := strconv.FormatInt(int64(src.Int), 10)
|
||||
_, err := pgio.WriteInt32(w, int32(len(s)))
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -152,8 +152,8 @@ func (i Int2) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (i Int2) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int2) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -162,6 +162,6 @@ func (i Int2) EncodeBinary(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt16(w, i.Int)
|
||||
_, err = pgio.WriteInt16(w, src.Int)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -14,15 +14,15 @@ type Int2Array struct {
|
|||
Status Status
|
||||
}
|
||||
|
||||
func (a *Int2Array) ConvertFrom(src interface{}) error {
|
||||
func (dst *Int2Array) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Int2Array:
|
||||
*a = value
|
||||
*dst = value
|
||||
case []int16:
|
||||
if value == nil {
|
||||
*a = Int2Array{Status: Null}
|
||||
*dst = Int2Array{Status: Null}
|
||||
} else if len(value) == 0 {
|
||||
*a = Int2Array{Status: Present}
|
||||
*dst = Int2Array{Status: Present}
|
||||
} else {
|
||||
elements := make([]Int2, len(value))
|
||||
for i := range value {
|
||||
|
@ -30,7 +30,7 @@ func (a *Int2Array) ConvertFrom(src interface{}) error {
|
|||
return err
|
||||
}
|
||||
}
|
||||
*a = Int2Array{
|
||||
*dst = Int2Array{
|
||||
Elements: elements,
|
||||
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
|
||||
Status: Present,
|
||||
|
@ -38,9 +38,9 @@ func (a *Int2Array) ConvertFrom(src interface{}) error {
|
|||
}
|
||||
case []uint16:
|
||||
if value == nil {
|
||||
*a = Int2Array{Status: Null}
|
||||
*dst = Int2Array{Status: Null}
|
||||
} else if len(value) == 0 {
|
||||
*a = Int2Array{Status: Present}
|
||||
*dst = Int2Array{Status: Present}
|
||||
} else {
|
||||
elements := make([]Int2, len(value))
|
||||
for i := range value {
|
||||
|
@ -48,7 +48,7 @@ func (a *Int2Array) ConvertFrom(src interface{}) error {
|
|||
return err
|
||||
}
|
||||
}
|
||||
*a = Int2Array{
|
||||
*dst = Int2Array{
|
||||
Elements: elements,
|
||||
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
|
||||
Status: Present,
|
||||
|
@ -56,7 +56,7 @@ func (a *Int2Array) ConvertFrom(src interface{}) error {
|
|||
}
|
||||
default:
|
||||
if originalSrc, ok := underlyingSliceType(src); ok {
|
||||
return a.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Int2", value)
|
||||
}
|
||||
|
@ -64,13 +64,13 @@ func (a *Int2Array) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (a *Int2Array) AssignTo(dst interface{}) error {
|
||||
func (src *Int2Array) AssignTo(dst interface{}) error {
|
||||
switch v := dst.(type) {
|
||||
case *[]int16:
|
||||
if a.Status == Present {
|
||||
*v = make([]int16, len(a.Elements))
|
||||
for i := range a.Elements {
|
||||
if err := a.Elements[i].AssignTo(&((*v)[i])); err != nil {
|
||||
if src.Status == Present {
|
||||
*v = make([]int16, len(src.Elements))
|
||||
for i := range src.Elements {
|
||||
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -78,10 +78,10 @@ func (a *Int2Array) AssignTo(dst interface{}) error {
|
|||
*v = nil
|
||||
}
|
||||
case *[]uint16:
|
||||
if a.Status == Present {
|
||||
*v = make([]uint16, len(a.Elements))
|
||||
for i := range a.Elements {
|
||||
if err := a.Elements[i].AssignTo(&((*v)[i])); err != nil {
|
||||
if src.Status == Present {
|
||||
*v = make([]uint16, len(src.Elements))
|
||||
for i := range src.Elements {
|
||||
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -89,20 +89,20 @@ func (a *Int2Array) AssignTo(dst interface{}) error {
|
|||
*v = nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("cannot put decode %v into %T", a, dst)
|
||||
return fmt.Errorf("cannot put decode %v into %T", src, dst)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Int2Array) DecodeText(r io.Reader) error {
|
||||
func (dst *Int2Array) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*a = Int2Array{Status: Null}
|
||||
*dst = Int2Array{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -135,19 +135,19 @@ func (a *Int2Array) DecodeText(r io.Reader) error {
|
|||
}
|
||||
}
|
||||
|
||||
*a = Int2Array{Elements: elements, Dimensions: uta.Dimensions, Status: Present}
|
||||
*dst = Int2Array{Elements: elements, Dimensions: uta.Dimensions, Status: Present}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Int2Array) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Int2Array) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*a = Int2Array{Status: Null}
|
||||
*dst = Int2Array{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ func (a *Int2Array) DecodeBinary(r io.Reader) error {
|
|||
}
|
||||
|
||||
if len(arrayHeader.Dimensions) == 0 {
|
||||
*a = Int2Array{Dimensions: arrayHeader.Dimensions, Status: Present}
|
||||
*dst = Int2Array{Dimensions: arrayHeader.Dimensions, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -176,16 +176,16 @@ func (a *Int2Array) DecodeBinary(r io.Reader) error {
|
|||
}
|
||||
}
|
||||
|
||||
*a = Int2Array{Elements: elements, Dimensions: arrayHeader.Dimensions, Status: Present}
|
||||
*dst = Int2Array{Elements: elements, Dimensions: arrayHeader.Dimensions, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Int2Array) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, a.Status); done {
|
||||
func (src *Int2Array) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(a.Dimensions) == 0 {
|
||||
if len(src.Dimensions) == 0 {
|
||||
_, err := pgio.WriteInt32(w, 2)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -197,7 +197,7 @@ func (a *Int2Array) EncodeText(w io.Writer) error {
|
|||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := EncodeTextArrayDimensions(buf, a.Dimensions)
|
||||
err := EncodeTextArrayDimensions(buf, src.Dimensions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -207,15 +207,15 @@ func (a *Int2Array) EncodeText(w io.Writer) error {
|
|||
// [4]. A multi-dimensional array of lengths [3,5,2] would have a
|
||||
// dimElemCounts of [30,10,2]. This is used to simplify when to render a '{'
|
||||
// or '}'.
|
||||
dimElemCounts := make([]int, len(a.Dimensions))
|
||||
dimElemCounts[len(a.Dimensions)-1] = int(a.Dimensions[len(a.Dimensions)-1].Length)
|
||||
for i := len(a.Dimensions) - 2; i > -1; i-- {
|
||||
dimElemCounts[i] = int(a.Dimensions[i].Length) * dimElemCounts[i+1]
|
||||
dimElemCounts := make([]int, len(src.Dimensions))
|
||||
dimElemCounts[len(src.Dimensions)-1] = int(src.Dimensions[len(src.Dimensions)-1].Length)
|
||||
for i := len(src.Dimensions) - 2; i > -1; i-- {
|
||||
dimElemCounts[i] = int(src.Dimensions[i].Length) * dimElemCounts[i+1]
|
||||
}
|
||||
|
||||
textElementWriter := NewTextElementWriter(buf)
|
||||
|
||||
for i, elem := range a.Elements {
|
||||
for i, elem := range src.Elements {
|
||||
if i > 0 {
|
||||
err = pgio.WriteByte(buf, ',')
|
||||
if err != nil {
|
||||
|
@ -257,8 +257,8 @@ func (a *Int2Array) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (a *Int2Array) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, a.Status); done {
|
||||
func (src *Int2Array) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -268,18 +268,18 @@ func (a *Int2Array) EncodeBinary(w io.Writer) error {
|
|||
// or how not pay allocations for the byte order conversions.
|
||||
elemBuf := &bytes.Buffer{}
|
||||
|
||||
for i := range a.Elements {
|
||||
err := a.Elements[i].EncodeBinary(elemBuf)
|
||||
for i := range src.Elements {
|
||||
err := src.Elements[i].EncodeBinary(elemBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if a.Elements[i].Status == Null {
|
||||
if src.Elements[i].Status == Null {
|
||||
arrayHeader.ContainsNull = true
|
||||
}
|
||||
}
|
||||
|
||||
arrayHeader.ElementOID = Int2OID
|
||||
arrayHeader.Dimensions = a.Dimensions
|
||||
arrayHeader.Dimensions = src.Dimensions
|
||||
|
||||
// TODO - consider how to avoid having to buffer array before writing length -
|
||||
// or how not pay allocations for the byte order conversions.
|
||||
|
|
|
@ -14,25 +14,25 @@ type Int4 struct {
|
|||
Status Status
|
||||
}
|
||||
|
||||
func (i *Int4) ConvertFrom(src interface{}) error {
|
||||
func (dst *Int4) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Int4:
|
||||
*i = value
|
||||
*dst = value
|
||||
case int8:
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case uint8:
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case int16:
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case uint16:
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case int32:
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case uint32:
|
||||
if value > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
}
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case int64:
|
||||
if value < math.MinInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
|
@ -40,12 +40,12 @@ func (i *Int4) ConvertFrom(src interface{}) error {
|
|||
if value > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
}
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case uint64:
|
||||
if value > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
}
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case int:
|
||||
if value < math.MinInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
|
@ -53,21 +53,21 @@ func (i *Int4) ConvertFrom(src interface{}) error {
|
|||
if value > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
}
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case uint:
|
||||
if value > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||
}
|
||||
*i = Int4{Int: int32(value), Status: Present}
|
||||
*dst = Int4{Int: int32(value), Status: Present}
|
||||
case string:
|
||||
num, err := strconv.ParseInt(value, 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*i = Int4{Int: int32(num), Status: Present}
|
||||
*dst = Int4{Int: int32(num), Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingIntType(src); ok {
|
||||
return i.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||
}
|
||||
|
@ -75,18 +75,18 @@ func (i *Int4) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *Int4) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(i.Int), i.Status, dst)
|
||||
func (src *Int4) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(src.Int), src.Status, dst)
|
||||
}
|
||||
|
||||
func (i *Int4) DecodeText(r io.Reader) error {
|
||||
func (dst *Int4) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int4{Status: Null}
|
||||
*dst = Int4{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -101,18 +101,18 @@ func (i *Int4) DecodeText(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int4{Int: int32(n), Status: Present}
|
||||
*dst = Int4{Int: int32(n), Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Int4) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Int4) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int4{Status: Null}
|
||||
*dst = Int4{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -125,16 +125,16 @@ func (i *Int4) DecodeBinary(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int4{Int: n, Status: Present}
|
||||
*dst = Int4{Int: n, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int4) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int4) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
s := strconv.FormatInt(int64(i.Int), 10)
|
||||
s := strconv.FormatInt(int64(src.Int), 10)
|
||||
_, err := pgio.WriteInt32(w, int32(len(s)))
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -143,8 +143,8 @@ func (i Int4) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (i Int4) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int4) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -153,6 +153,6 @@ func (i Int4) EncodeBinary(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt32(w, i.Int)
|
||||
_, err = pgio.WriteInt32(w, src.Int)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -14,29 +14,29 @@ type Int8 struct {
|
|||
Status Status
|
||||
}
|
||||
|
||||
func (i *Int8) ConvertFrom(src interface{}) error {
|
||||
func (dst *Int8) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Int8:
|
||||
*i = value
|
||||
*dst = value
|
||||
case int8:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case uint8:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case int16:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case uint16:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case int32:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case uint32:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case int64:
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case uint64:
|
||||
if value > math.MaxInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||
}
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case int:
|
||||
if int64(value) < math.MinInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||
|
@ -44,21 +44,21 @@ func (i *Int8) ConvertFrom(src interface{}) error {
|
|||
if int64(value) > math.MaxInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||
}
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case uint:
|
||||
if uint64(value) > math.MaxInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||
}
|
||||
*i = Int8{Int: int64(value), Status: Present}
|
||||
*dst = Int8{Int: int64(value), Status: Present}
|
||||
case string:
|
||||
num, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*i = Int8{Int: num, Status: Present}
|
||||
*dst = Int8{Int: num, Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingIntType(src); ok {
|
||||
return i.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||
}
|
||||
|
@ -66,18 +66,18 @@ func (i *Int8) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *Int8) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(i.Int), i.Status, dst)
|
||||
func (src *Int8) AssignTo(dst interface{}) error {
|
||||
return int64AssignTo(int64(src.Int), src.Status, dst)
|
||||
}
|
||||
|
||||
func (i *Int8) DecodeText(r io.Reader) error {
|
||||
func (dst *Int8) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int8{Status: Null}
|
||||
*dst = Int8{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -92,18 +92,18 @@ func (i *Int8) DecodeText(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int8{Int: n, Status: Present}
|
||||
*dst = Int8{Int: n, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Int8) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Int8) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*i = Int8{Status: Null}
|
||||
*dst = Int8{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -116,16 +116,16 @@ func (i *Int8) DecodeBinary(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*i = Int8{Int: n, Status: Present}
|
||||
*dst = Int8{Int: n, Status: Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i Int8) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int8) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
s := strconv.FormatInt(i.Int, 10)
|
||||
s := strconv.FormatInt(src.Int, 10)
|
||||
_, err := pgio.WriteInt32(w, int32(len(s)))
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -134,8 +134,8 @@ func (i Int8) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (i Int8) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, i.Status); done {
|
||||
func (src Int8) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -144,6 +144,6 @@ func (i Int8) EncodeBinary(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt64(w, i.Int)
|
||||
_, err = pgio.WriteInt64(w, src.Int)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -25,15 +25,15 @@ type Timestamptz struct {
|
|||
InfinityModifier
|
||||
}
|
||||
|
||||
func (t *Timestamptz) ConvertFrom(src interface{}) error {
|
||||
func (dst *Timestamptz) ConvertFrom(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case Timestamptz:
|
||||
*t = value
|
||||
*dst = value
|
||||
case time.Time:
|
||||
*t = Timestamptz{Time: value, Status: Present}
|
||||
*dst = Timestamptz{Time: value, Status: Present}
|
||||
default:
|
||||
if originalSrc, ok := underlyingTimeType(src); ok {
|
||||
return t.ConvertFrom(originalSrc)
|
||||
return dst.ConvertFrom(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Timestamptz", value)
|
||||
}
|
||||
|
@ -41,20 +41,20 @@ func (t *Timestamptz) ConvertFrom(src interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (t *Timestamptz) AssignTo(dst interface{}) error {
|
||||
func (src *Timestamptz) AssignTo(dst interface{}) error {
|
||||
switch v := dst.(type) {
|
||||
case *time.Time:
|
||||
if t.Status != Present || t.InfinityModifier != None {
|
||||
return fmt.Errorf("cannot assign %v to %T", t, dst)
|
||||
if src.Status != Present || src.InfinityModifier != None {
|
||||
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
||||
}
|
||||
*v = t.Time
|
||||
*v = src.Time
|
||||
default:
|
||||
if v := reflect.ValueOf(dst); v.Kind() == reflect.Ptr {
|
||||
el := v.Elem()
|
||||
switch el.Kind() {
|
||||
// if dst is a pointer to pointer, strip the pointer and try again
|
||||
case reflect.Ptr:
|
||||
if t.Status == Null {
|
||||
if src.Status == Null {
|
||||
if !el.IsNil() {
|
||||
// if the destination pointer is not nil, nil it out
|
||||
el.Set(reflect.Zero(el.Type()))
|
||||
|
@ -65,23 +65,23 @@ func (t *Timestamptz) AssignTo(dst interface{}) error {
|
|||
// allocate destination
|
||||
el.Set(reflect.New(el.Type().Elem()))
|
||||
}
|
||||
return t.AssignTo(el.Interface())
|
||||
return src.AssignTo(el.Interface())
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot assign %v into %T", t, dst)
|
||||
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Timestamptz) DecodeText(r io.Reader) error {
|
||||
func (dst *Timestamptz) DecodeText(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*t = Timestamptz{Status: Null}
|
||||
*dst = Timestamptz{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -94,9 +94,9 @@ func (t *Timestamptz) DecodeText(r io.Reader) error {
|
|||
sbuf := string(buf)
|
||||
switch sbuf {
|
||||
case "infinity":
|
||||
*t = Timestamptz{Status: Present, InfinityModifier: Infinity}
|
||||
*dst = Timestamptz{Status: Present, InfinityModifier: Infinity}
|
||||
case "-infinity":
|
||||
*t = Timestamptz{Status: Present, InfinityModifier: -Infinity}
|
||||
*dst = Timestamptz{Status: Present, InfinityModifier: -Infinity}
|
||||
default:
|
||||
var format string
|
||||
if sbuf[len(sbuf)-9] == '-' || sbuf[len(sbuf)-9] == '+' {
|
||||
|
@ -112,20 +112,20 @@ func (t *Timestamptz) DecodeText(r io.Reader) error {
|
|||
return err
|
||||
}
|
||||
|
||||
*t = Timestamptz{Time: tim, Status: Present}
|
||||
*dst = Timestamptz{Time: tim, Status: Present}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Timestamptz) DecodeBinary(r io.Reader) error {
|
||||
func (dst *Timestamptz) DecodeBinary(r io.Reader) error {
|
||||
size, err := pgio.ReadInt32(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if size == -1 {
|
||||
*t = Timestamptz{Status: Null}
|
||||
*dst = Timestamptz{Status: Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -140,28 +140,28 @@ func (t *Timestamptz) DecodeBinary(r io.Reader) error {
|
|||
|
||||
switch microsecSinceY2K {
|
||||
case infinityMicrosecondOffset:
|
||||
*t = Timestamptz{Status: Present, InfinityModifier: Infinity}
|
||||
*dst = Timestamptz{Status: Present, InfinityModifier: Infinity}
|
||||
case negativeInfinityMicrosecondOffset:
|
||||
*t = Timestamptz{Status: Present, InfinityModifier: -Infinity}
|
||||
*dst = Timestamptz{Status: Present, InfinityModifier: -Infinity}
|
||||
default:
|
||||
microsecSinceUnixEpoch := microsecFromUnixEpochToY2K + microsecSinceY2K
|
||||
tim := time.Unix(microsecSinceUnixEpoch/1000000, (microsecSinceUnixEpoch%1000000)*1000)
|
||||
*t = Timestamptz{Time: tim, Status: Present}
|
||||
*dst = Timestamptz{Time: tim, Status: Present}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Timestamptz) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, t.Status); done {
|
||||
func (src Timestamptz) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
var s string
|
||||
|
||||
switch t.InfinityModifier {
|
||||
switch src.InfinityModifier {
|
||||
case None:
|
||||
s = t.Time.UTC().Format(pgTimestamptzSecondFormat)
|
||||
s = src.Time.UTC().Format(pgTimestamptzSecondFormat)
|
||||
case Infinity:
|
||||
s = "infinity"
|
||||
case NegativeInfinity:
|
||||
|
@ -177,8 +177,8 @@ func (t Timestamptz) EncodeText(w io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (t Timestamptz) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, t.Status); done {
|
||||
func (src Timestamptz) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -188,9 +188,9 @@ func (t Timestamptz) EncodeBinary(w io.Writer) error {
|
|||
}
|
||||
|
||||
var microsecSinceY2K int64
|
||||
switch t.InfinityModifier {
|
||||
switch src.InfinityModifier {
|
||||
case None:
|
||||
microsecSinceUnixEpoch := t.Time.Unix()*1000000 + int64(t.Time.Nanosecond())/1000
|
||||
microsecSinceUnixEpoch := src.Time.Unix()*1000000 + int64(src.Time.Nanosecond())/1000
|
||||
microsecSinceY2K = microsecSinceUnixEpoch - microsecFromUnixEpochToY2K
|
||||
case Infinity:
|
||||
microsecSinceY2K = infinityMicrosecondOffset
|
||||
|
|
Loading…
Reference in New Issue