2
0

Use Go 1.13 errors instead of xerrors

This commit is contained in:
Jack Christensen
2021-03-25 09:01:59 -04:00
parent aa89720576
commit dd160540c4
80 changed files with 927 additions and 956 deletions
+5 -6
View File
@@ -2,8 +2,7 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
errors "golang.org/x/xerrors"
) )
// ACLItem is used for PostgreSQL's aclitem data type. A sample aclitem // ACLItem is used for PostgreSQL's aclitem data type. A sample aclitem
@@ -49,7 +48,7 @@ func (dst *ACLItem) Set(src interface{}) error {
if originalSrc, ok := underlyingStringType(src); ok { if originalSrc, ok := underlyingStringType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to ACLItem", value) return fmt.Errorf("cannot convert %v to ACLItem", value)
} }
return nil return nil
@@ -77,13 +76,13 @@ func (src *ACLItem) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *ACLItem) DecodeText(ci *ConnInfo, src []byte) error { func (dst *ACLItem) DecodeText(ci *ConnInfo, src []byte) error {
@@ -123,7 +122,7 @@ func (dst *ACLItem) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+15 -16
View File
@@ -4,9 +4,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"reflect" "reflect"
errors "golang.org/x/xerrors"
) )
type ACLItemArray struct { type ACLItemArray struct {
@@ -94,7 +93,7 @@ func (dst *ACLItemArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for ACLItemArray", src) return fmt.Errorf("cannot find dimensions of %v for ACLItemArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = ACLItemArray{Status: Present} *dst = ACLItemArray{Status: Present}
@@ -104,7 +103,7 @@ func (dst *ACLItemArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to ACLItemArray", src) return fmt.Errorf("cannot convert %v to ACLItemArray", src)
} }
*dst = ACLItemArray{ *dst = ACLItemArray{
@@ -135,7 +134,7 @@ func (dst *ACLItemArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to ACLItemArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to ACLItemArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -153,7 +152,7 @@ func (dst *ACLItemArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -166,10 +165,10 @@ func (dst *ACLItemArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to ACLItemArray") return 0, fmt.Errorf("cannot convert all values to ACLItemArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in ACLItemArray", err) return 0, fmt.Errorf("%v in ACLItemArray", err)
} }
index++ index++
@@ -231,7 +230,7 @@ func (src *ACLItemArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -246,7 +245,7 @@ func (src *ACLItemArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -254,7 +253,7 @@ func (src *ACLItemArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *ACLItemArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *ACLItemArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -270,7 +269,7 @@ func (src *ACLItemArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -288,14 +287,14 @@ func (src *ACLItemArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from ACLItemArray") return 0, fmt.Errorf("cannot assign all values from ACLItemArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from ACLItemArray") return 0, fmt.Errorf("cannot assign all values from ACLItemArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -412,7 +411,7 @@ func (dst *ACLItemArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+18 -18
View File
@@ -3,6 +3,7 @@ package pgtype
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
"io" "io"
"reflect" "reflect"
"strconv" "strconv"
@@ -10,7 +11,6 @@ import (
"unicode" "unicode"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// Information on the internals of PostgreSQL arrays can be found in // Information on the internals of PostgreSQL arrays can be found in
@@ -30,7 +30,7 @@ type ArrayDimension struct {
func (dst *ArrayHeader) DecodeBinary(ci *ConnInfo, src []byte) (int, error) { func (dst *ArrayHeader) DecodeBinary(ci *ConnInfo, src []byte) (int, error) {
if len(src) < 12 { if len(src) < 12 {
return 0, errors.Errorf("array header too short: %d", len(src)) return 0, fmt.Errorf("array header too short: %d", len(src))
} }
rp := 0 rp := 0
@@ -48,7 +48,7 @@ func (dst *ArrayHeader) DecodeBinary(ci *ConnInfo, src []byte) (int, error) {
dst.Dimensions = make([]ArrayDimension, numDims) dst.Dimensions = make([]ArrayDimension, numDims)
} }
if len(src) < 12+numDims*8 { if len(src) < 12+numDims*8 {
return 0, errors.Errorf("array header too short for %d dimensions: %d", numDims, len(src)) return 0, fmt.Errorf("array header too short for %d dimensions: %d", numDims, len(src))
} }
for i := range dst.Dimensions { for i := range dst.Dimensions {
dst.Dimensions[i].Length = int32(binary.BigEndian.Uint32(src[rp:])) dst.Dimensions[i].Length = int32(binary.BigEndian.Uint32(src[rp:]))
@@ -95,7 +95,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
r, _, err := buf.ReadRune() r, _, err := buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
var explicitDimensions []ArrayDimension var explicitDimensions []ArrayDimension
@@ -107,41 +107,41 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
for { for {
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
if r == '=' { if r == '=' {
break break
} else if r != '[' { } else if r != '[' {
return nil, errors.Errorf("invalid array, expected '[' or '=' got %v", r) return nil, fmt.Errorf("invalid array, expected '[' or '=' got %v", r)
} }
lower, err := arrayParseInteger(buf) lower, err := arrayParseInteger(buf)
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
if r != ':' { if r != ':' {
return nil, errors.Errorf("invalid array, expected ':' got %v", r) return nil, fmt.Errorf("invalid array, expected ':' got %v", r)
} }
upper, err := arrayParseInteger(buf) upper, err := arrayParseInteger(buf)
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
if r != ']' { if r != ']' {
return nil, errors.Errorf("invalid array, expected ']' got %v", r) return nil, fmt.Errorf("invalid array, expected ']' got %v", r)
} }
explicitDimensions = append(explicitDimensions, ArrayDimension{LowerBound: lower, Length: upper - lower + 1}) explicitDimensions = append(explicitDimensions, ArrayDimension{LowerBound: lower, Length: upper - lower + 1})
@@ -149,12 +149,12 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
} }
if r != '{' { if r != '{' {
return nil, errors.Errorf("invalid array, expected '{': %v", err) return nil, fmt.Errorf("invalid array, expected '{': %v", err)
} }
implicitDimensions := []ArrayDimension{{LowerBound: 1, Length: 0}} implicitDimensions := []ArrayDimension{{LowerBound: 1, Length: 0}}
@@ -163,7 +163,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
for { for {
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
if r == '{' { if r == '{' {
@@ -180,7 +180,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
for { for {
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array: %v", err) return nil, fmt.Errorf("invalid array: %v", err)
} }
switch r { switch r {
@@ -199,7 +199,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
buf.UnreadRune() buf.UnreadRune()
value, quoted, err := arrayParseValue(buf) value, quoted, err := arrayParseValue(buf)
if err != nil { if err != nil {
return nil, errors.Errorf("invalid array value: %v", err) return nil, fmt.Errorf("invalid array value: %v", err)
} }
if currentDim == counterDim { if currentDim == counterDim {
implicitDimensions[currentDim].Length++ implicitDimensions[currentDim].Length++
@@ -216,7 +216,7 @@ func ParseUntypedTextArray(src string) (*UntypedTextArray, error) {
skipWhitespace(buf) skipWhitespace(buf)
if buf.Len() > 0 { if buf.Len() > 0 {
return nil, errors.Errorf("unexpected trailing data: %v", buf.String()) return nil, fmt.Errorf("unexpected trailing data: %v", buf.String())
} }
if len(dst.Elements) == 0 { if len(dst.Elements) == 0 {
+6 -6
View File
@@ -3,10 +3,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// ArrayType represents an array type. While it implements Value, this is only in service of its type conversion duties // ArrayType represents an array type. While it implements Value, this is only in service of its type conversion duties
@@ -58,7 +58,7 @@ func (dst *ArrayType) Set(src interface{}) error {
sliceVal := reflect.ValueOf(src) sliceVal := reflect.ValueOf(src)
if sliceVal.Kind() != reflect.Slice { if sliceVal.Kind() != reflect.Slice {
return errors.Errorf("cannot set non-slice") return fmt.Errorf("cannot set non-slice")
} }
if sliceVal.IsNil() { if sliceVal.IsNil() {
@@ -100,14 +100,14 @@ func (dst ArrayType) Get() interface{} {
func (src *ArrayType) AssignTo(dst interface{}) error { func (src *ArrayType) AssignTo(dst interface{}) error {
ptrSlice := reflect.ValueOf(dst) ptrSlice := reflect.ValueOf(dst)
if ptrSlice.Kind() != reflect.Ptr { if ptrSlice.Kind() != reflect.Ptr {
return errors.Errorf("cannot assign to non-pointer") return fmt.Errorf("cannot assign to non-pointer")
} }
sliceVal := ptrSlice.Elem() sliceVal := ptrSlice.Elem()
sliceType := sliceVal.Type() sliceType := sliceVal.Type()
if sliceType.Kind() != reflect.Slice { if sliceType.Kind() != reflect.Slice {
return errors.Errorf("cannot assign to pointer to non-slice") return fmt.Errorf("cannot assign to pointer to non-slice")
} }
switch src.status { switch src.status {
@@ -132,7 +132,7 @@ func (src *ArrayType) AssignTo(dst interface{}) error {
return nil return nil
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *ArrayType) DecodeText(ci *ConnInfo, src []byte) error { func (dst *ArrayType) DecodeText(ci *ConnInfo, src []byte) error {
@@ -336,7 +336,7 @@ func (dst *ArrayType) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+7 -8
View File
@@ -3,9 +3,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/json" "encoding/json"
"fmt"
"strconv" "strconv"
errors "golang.org/x/xerrors"
) )
type Bool struct { type Bool struct {
@@ -51,7 +50,7 @@ func (dst *Bool) Set(src interface{}) error {
if originalSrc, ok := underlyingBoolType(src); ok { if originalSrc, ok := underlyingBoolType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Bool", value) return fmt.Errorf("cannot convert %v to Bool", value)
} }
return nil return nil
@@ -79,13 +78,13 @@ func (src *Bool) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error {
@@ -95,7 +94,7 @@ func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) != 1 { if len(src) != 1 {
return errors.Errorf("invalid length for bool: %v", len(src)) return fmt.Errorf("invalid length for bool: %v", len(src))
} }
*dst = Bool{Bool: src[0] == 't', Status: Present} *dst = Bool{Bool: src[0] == 't', Status: Present}
@@ -109,7 +108,7 @@ func (dst *Bool) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 1 { if len(src) != 1 {
return errors.Errorf("invalid length for bool: %v", len(src)) return fmt.Errorf("invalid length for bool: %v", len(src))
} }
*dst = Bool{Bool: src[0] == 1, Status: Present} *dst = Bool{Bool: src[0] == 1, Status: Present}
@@ -169,7 +168,7 @@ func (dst *Bool) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type BoolArray struct { type BoolArray struct {
@@ -96,7 +96,7 @@ func (dst *BoolArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for BoolArray", src) return fmt.Errorf("cannot find dimensions of %v for BoolArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = BoolArray{Status: Present} *dst = BoolArray{Status: Present}
@@ -106,7 +106,7 @@ func (dst *BoolArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to BoolArray", src) return fmt.Errorf("cannot convert %v to BoolArray", src)
} }
*dst = BoolArray{ *dst = BoolArray{
@@ -137,7 +137,7 @@ func (dst *BoolArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to BoolArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to BoolArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *BoolArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *BoolArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to BoolArray") return 0, fmt.Errorf("cannot convert all values to BoolArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in BoolArray", err) return 0, fmt.Errorf("%v in BoolArray", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *BoolArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *BoolArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *BoolArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *BoolArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *BoolArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *BoolArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *BoolArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from BoolArray") return 0, fmt.Errorf("cannot assign all values from BoolArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from BoolArray") return 0, fmt.Errorf("cannot assign all values from BoolArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src BoolArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("bool"); ok { if dt, ok := ci.DataTypeForName("bool"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "bool") return nil, fmt.Errorf("unable to find oid for type name %v", "bool")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *BoolArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+5 -6
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Box struct { type Box struct {
@@ -18,7 +17,7 @@ type Box struct {
} }
func (dst *Box) Set(src interface{}) error { func (dst *Box) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Box", src) return fmt.Errorf("cannot convert %v to Box", src)
} }
func (dst Box) Get() interface{} { func (dst Box) Get() interface{} {
@@ -33,7 +32,7 @@ func (dst Box) Get() interface{} {
} }
func (src *Box) AssignTo(dst interface{}) error { func (src *Box) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Box) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Box) DecodeText(ci *ConnInfo, src []byte) error {
@@ -43,7 +42,7 @@ func (dst *Box) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 11 { if len(src) < 11 {
return errors.Errorf("invalid length for Box: %v", len(src)) return fmt.Errorf("invalid length for Box: %v", len(src))
} }
str := string(src[1:]) str := string(src[1:])
@@ -90,7 +89,7 @@ func (dst *Box) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 32 { if len(src) != 32 {
return errors.Errorf("invalid length for Box: %v", len(src)) return fmt.Errorf("invalid length for Box: %v", len(src))
} }
x1 := binary.BigEndian.Uint64(src) x1 := binary.BigEndian.Uint64(src)
@@ -157,7 +156,7 @@ func (dst *Box) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type BPCharArray struct { type BPCharArray struct {
@@ -96,7 +96,7 @@ func (dst *BPCharArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for BPCharArray", src) return fmt.Errorf("cannot find dimensions of %v for BPCharArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = BPCharArray{Status: Present} *dst = BPCharArray{Status: Present}
@@ -106,7 +106,7 @@ func (dst *BPCharArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to BPCharArray", src) return fmt.Errorf("cannot convert %v to BPCharArray", src)
} }
*dst = BPCharArray{ *dst = BPCharArray{
@@ -137,7 +137,7 @@ func (dst *BPCharArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to BPCharArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to BPCharArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *BPCharArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *BPCharArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to BPCharArray") return 0, fmt.Errorf("cannot convert all values to BPCharArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in BPCharArray", err) return 0, fmt.Errorf("%v in BPCharArray", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *BPCharArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *BPCharArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *BPCharArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *BPCharArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *BPCharArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *BPCharArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *BPCharArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from BPCharArray") return 0, fmt.Errorf("cannot assign all values from BPCharArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from BPCharArray") return 0, fmt.Errorf("cannot assign all values from BPCharArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src BPCharArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("bpchar"); ok { if dt, ok := ci.DataTypeForName("bpchar"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "bpchar") return nil, fmt.Errorf("unable to find oid for type name %v", "bpchar")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *BPCharArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+6 -7
View File
@@ -3,8 +3,7 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/hex" "encoding/hex"
"fmt"
errors "golang.org/x/xerrors"
) )
type Bytea struct { type Bytea struct {
@@ -36,7 +35,7 @@ func (dst *Bytea) Set(src interface{}) error {
if originalSrc, ok := underlyingBytesType(src); ok { if originalSrc, ok := underlyingBytesType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Bytea", value) return fmt.Errorf("cannot convert %v to Bytea", value)
} }
return nil return nil
@@ -66,13 +65,13 @@ func (src *Bytea) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
// DecodeText only supports the hex format. This has been the default since // DecodeText only supports the hex format. This has been the default since
@@ -84,7 +83,7 @@ func (dst *Bytea) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 2 || src[0] != '\\' || src[1] != 'x' { if len(src) < 2 || src[0] != '\\' || src[1] != 'x' {
return errors.Errorf("invalid hex format") return fmt.Errorf("invalid hex format")
} }
buf := make([]byte, (len(src)-2)/2) buf := make([]byte, (len(src)-2)/2)
@@ -148,7 +147,7 @@ func (dst *Bytea) Scan(src interface{}) error {
return nil return nil
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type ByteaArray struct { type ByteaArray struct {
@@ -77,7 +77,7 @@ func (dst *ByteaArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for ByteaArray", src) return fmt.Errorf("cannot find dimensions of %v for ByteaArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = ByteaArray{Status: Present} *dst = ByteaArray{Status: Present}
@@ -87,7 +87,7 @@ func (dst *ByteaArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to ByteaArray", src) return fmt.Errorf("cannot convert %v to ByteaArray", src)
} }
*dst = ByteaArray{ *dst = ByteaArray{
@@ -118,7 +118,7 @@ func (dst *ByteaArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to ByteaArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to ByteaArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -136,7 +136,7 @@ func (dst *ByteaArray) setRecursive(value reflect.Value, index, dimension int) (
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -149,10 +149,10 @@ func (dst *ByteaArray) setRecursive(value reflect.Value, index, dimension int) (
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to ByteaArray") return 0, fmt.Errorf("cannot convert all values to ByteaArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in ByteaArray", err) return 0, fmt.Errorf("%v in ByteaArray", err)
} }
index++ index++
@@ -205,7 +205,7 @@ func (src *ByteaArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -220,7 +220,7 @@ func (src *ByteaArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -228,7 +228,7 @@ func (src *ByteaArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *ByteaArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *ByteaArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -244,7 +244,7 @@ func (src *ByteaArray) assignToRecursive(value reflect.Value, index, dimension i
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -262,14 +262,14 @@ func (src *ByteaArray) assignToRecursive(value reflect.Value, index, dimension i
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from ByteaArray") return 0, fmt.Errorf("cannot assign all values from ByteaArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from ByteaArray") return 0, fmt.Errorf("cannot assign all values from ByteaArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -428,7 +428,7 @@ func (src ByteaArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("bytea"); ok { if dt, ok := ci.DataTypeForName("bytea"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "bytea") return nil, fmt.Errorf("unable to find oid for type name %v", "bytea")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -472,7 +472,7 @@ func (dst *ByteaArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"net" "net"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type CIDRArray struct { type CIDRArray struct {
@@ -116,7 +116,7 @@ func (dst *CIDRArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for CIDRArray", src) return fmt.Errorf("cannot find dimensions of %v for CIDRArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = CIDRArray{Status: Present} *dst = CIDRArray{Status: Present}
@@ -126,7 +126,7 @@ func (dst *CIDRArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to CIDRArray", src) return fmt.Errorf("cannot convert %v to CIDRArray", src)
} }
*dst = CIDRArray{ *dst = CIDRArray{
@@ -157,7 +157,7 @@ func (dst *CIDRArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to CIDRArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to CIDRArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -175,7 +175,7 @@ func (dst *CIDRArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -188,10 +188,10 @@ func (dst *CIDRArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to CIDRArray") return 0, fmt.Errorf("cannot convert all values to CIDRArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in CIDRArray", err) return 0, fmt.Errorf("%v in CIDRArray", err)
} }
index++ index++
@@ -262,7 +262,7 @@ func (src *CIDRArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -277,7 +277,7 @@ func (src *CIDRArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -285,7 +285,7 @@ func (src *CIDRArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *CIDRArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *CIDRArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -301,7 +301,7 @@ func (src *CIDRArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -319,14 +319,14 @@ func (src *CIDRArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from CIDRArray") return 0, fmt.Errorf("cannot assign all values from CIDRArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from CIDRArray") return 0, fmt.Errorf("cannot assign all values from CIDRArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -485,7 +485,7 @@ func (src CIDRArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("cidr"); ok { if dt, ok := ci.DataTypeForName("cidr"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "cidr") return nil, fmt.Errorf("unable to find oid for type name %v", "cidr")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -529,7 +529,7 @@ func (dst *CIDRArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+5 -6
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Circle struct { type Circle struct {
@@ -19,7 +18,7 @@ type Circle struct {
} }
func (dst *Circle) Set(src interface{}) error { func (dst *Circle) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Circle", src) return fmt.Errorf("cannot convert %v to Circle", src)
} }
func (dst Circle) Get() interface{} { func (dst Circle) Get() interface{} {
@@ -34,7 +33,7 @@ func (dst Circle) Get() interface{} {
} }
func (src *Circle) AssignTo(dst interface{}) error { func (src *Circle) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Circle) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Circle) DecodeText(ci *ConnInfo, src []byte) error {
@@ -44,7 +43,7 @@ func (dst *Circle) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 9 { if len(src) < 9 {
return errors.Errorf("invalid length for Circle: %v", len(src)) return fmt.Errorf("invalid length for Circle: %v", len(src))
} }
str := string(src[2:]) str := string(src[2:])
@@ -80,7 +79,7 @@ func (dst *Circle) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 24 { if len(src) != 24 {
return errors.Errorf("invalid length for Circle: %v", len(src)) return fmt.Errorf("invalid length for Circle: %v", len(src))
} }
x := binary.BigEndian.Uint64(src) x := binary.BigEndian.Uint64(src)
@@ -142,7 +141,7 @@ func (dst *Circle) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+7 -9
View File
@@ -1,8 +1,6 @@
package pgtype package pgtype
import ( import "fmt"
errors "golang.org/x/xerrors"
)
// CompositeFields scans the fields of a composite type into the elements of the CompositeFields value. To scan a // CompositeFields scans the fields of a composite type into the elements of the CompositeFields value. To scan a
// nullable value use a *CompositeFields. It will be set to nil in case of null. // nullable value use a *CompositeFields. It will be set to nil in case of null.
@@ -13,11 +11,11 @@ type CompositeFields []interface{}
func (cf CompositeFields) DecodeBinary(ci *ConnInfo, src []byte) error { func (cf CompositeFields) DecodeBinary(ci *ConnInfo, src []byte) error {
if len(cf) == 0 { if len(cf) == 0 {
return errors.Errorf("cannot decode into empty CompositeFields") return fmt.Errorf("cannot decode into empty CompositeFields")
} }
if src == nil { if src == nil {
return errors.Errorf("cannot decode unexpected null into CompositeFields") return fmt.Errorf("cannot decode unexpected null into CompositeFields")
} }
scanner := NewCompositeBinaryScanner(ci, src) scanner := NewCompositeBinaryScanner(ci, src)
@@ -35,11 +33,11 @@ func (cf CompositeFields) DecodeBinary(ci *ConnInfo, src []byte) error {
func (cf CompositeFields) DecodeText(ci *ConnInfo, src []byte) error { func (cf CompositeFields) DecodeText(ci *ConnInfo, src []byte) error {
if len(cf) == 0 { if len(cf) == 0 {
return errors.Errorf("cannot decode into empty CompositeFields") return fmt.Errorf("cannot decode into empty CompositeFields")
} }
if src == nil { if src == nil {
return errors.Errorf("cannot decode unexpected null into CompositeFields") return fmt.Errorf("cannot decode unexpected null into CompositeFields")
} }
scanner := NewCompositeTextScanner(ci, src) scanner := NewCompositeTextScanner(ci, src)
@@ -87,7 +85,7 @@ func (cf CompositeFields) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
for _, f := range cf { for _, f := range cf {
dt, ok := ci.DataTypeForValue(f) dt, ok := ci.DataTypeForValue(f)
if !ok { if !ok {
return nil, errors.Errorf("Unknown OID for %#v", f) return nil, fmt.Errorf("Unknown OID for %#v", f)
} }
if binaryEncoder, ok := f.(BinaryEncoder); ok { if binaryEncoder, ok := f.(BinaryEncoder); ok {
@@ -100,7 +98,7 @@ func (cf CompositeFields) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
if binaryEncoder, ok := dt.Value.(BinaryEncoder); ok { if binaryEncoder, ok := dt.Value.(BinaryEncoder); ok {
b.AppendEncoder(dt.OID, binaryEncoder) b.AppendEncoder(dt.OID, binaryEncoder)
} else { } else {
return nil, errors.Errorf("Cannot encode binary format for %v", f) return nil, fmt.Errorf("Cannot encode binary format for %v", f)
} }
} }
} }
+21 -20
View File
@@ -2,11 +2,12 @@ package pgtype
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"fmt"
"reflect" "reflect"
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type CompositeTypeField struct { type CompositeTypeField struct {
@@ -31,13 +32,13 @@ func NewCompositeType(typeName string, fields []CompositeTypeField, ci *ConnInfo
for i := range fields { for i := range fields {
dt, ok := ci.DataTypeForOID(fields[i].OID) dt, ok := ci.DataTypeForOID(fields[i].OID)
if !ok { if !ok {
return nil, errors.Errorf("no data type registered for oid: %d", fields[i].OID) return nil, fmt.Errorf("no data type registered for oid: %d", fields[i].OID)
} }
value := NewValue(dt.Value) value := NewValue(dt.Value)
valueTranscoder, ok := value.(ValueTranscoder) valueTranscoder, ok := value.(ValueTranscoder)
if !ok { if !ok {
return nil, errors.Errorf("data type for oid does not implement ValueTranscoder: %d", fields[i].OID) return nil, fmt.Errorf("data type for oid does not implement ValueTranscoder: %d", fields[i].OID)
} }
valueTranscoders[i] = valueTranscoder valueTranscoders[i] = valueTranscoder
@@ -102,7 +103,7 @@ func (dst *CompositeType) Set(src interface{}) error {
switch value := src.(type) { switch value := src.(type) {
case []interface{}: case []interface{}:
if len(value) != len(dst.valueTranscoders) { if len(value) != len(dst.valueTranscoders) {
return errors.Errorf("Number of fields don't match. CompositeType has %d fields", len(dst.valueTranscoders)) return fmt.Errorf("Number of fields don't match. CompositeType has %d fields", len(dst.valueTranscoders))
} }
for i, v := range value { for i, v := range value {
if err := dst.valueTranscoders[i].Set(v); err != nil { if err := dst.valueTranscoders[i].Set(v); err != nil {
@@ -117,7 +118,7 @@ func (dst *CompositeType) Set(src interface{}) error {
} }
return dst.Set(*value) return dst.Set(*value)
default: default:
return errors.Errorf("Can not convert %v to Composite", src) return fmt.Errorf("Can not convert %v to Composite", src)
} }
return nil return nil
@@ -130,7 +131,7 @@ func (src CompositeType) AssignTo(dst interface{}) error {
switch v := dst.(type) { switch v := dst.(type) {
case []interface{}: case []interface{}:
if len(v) != len(src.valueTranscoders) { if len(v) != len(src.valueTranscoders) {
return errors.Errorf("Number of fields don't match. CompositeType has %d fields", len(src.valueTranscoders)) return fmt.Errorf("Number of fields don't match. CompositeType has %d fields", len(src.valueTranscoders))
} }
for i := range src.valueTranscoders { for i := range src.valueTranscoders {
if v[i] == nil { if v[i] == nil {
@@ -139,7 +140,7 @@ func (src CompositeType) AssignTo(dst interface{}) error {
err := assignToOrSet(src.valueTranscoders[i], v[i]) err := assignToOrSet(src.valueTranscoders[i], v[i])
if err != nil { if err != nil {
return errors.Errorf("unable to assign to dst[%d]: %v", i, err) return fmt.Errorf("unable to assign to dst[%d]: %v", i, err)
} }
} }
return nil return nil
@@ -153,12 +154,12 @@ func (src CompositeType) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func assignToOrSet(src Value, dst interface{}) error { func assignToOrSet(src Value, dst interface{}) error {
@@ -210,7 +211,7 @@ func (src CompositeType) assignToPtrStruct(dst interface{}) (bool, error) {
for i := range exportedFields { for i := range exportedFields {
err := assignToOrSet(src.valueTranscoders[i], dstElemValue.Field(exportedFields[i]).Addr().Interface()) err := assignToOrSet(src.valueTranscoders[i], dstElemValue.Field(exportedFields[i]).Addr().Interface())
if err != nil { if err != nil {
return true, errors.Errorf("unable to assign to field %s: %v", dstElemType.Field(exportedFields[i]).Name, err) return true, fmt.Errorf("unable to assign to field %s: %v", dstElemType.Field(exportedFields[i]).Name, err)
} }
} }
@@ -310,7 +311,7 @@ type CompositeBinaryScanner struct {
func NewCompositeBinaryScanner(ci *ConnInfo, src []byte) *CompositeBinaryScanner { func NewCompositeBinaryScanner(ci *ConnInfo, src []byte) *CompositeBinaryScanner {
rp := 0 rp := 0
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return &CompositeBinaryScanner{err: errors.Errorf("Record incomplete %v", src)} return &CompositeBinaryScanner{err: fmt.Errorf("Record incomplete %v", src)}
} }
fieldCount := int32(binary.BigEndian.Uint32(src[rp:])) fieldCount := int32(binary.BigEndian.Uint32(src[rp:]))
@@ -362,7 +363,7 @@ func (cfs *CompositeBinaryScanner) Next() bool {
} }
if len(cfs.src[cfs.rp:]) < 8 { if len(cfs.src[cfs.rp:]) < 8 {
cfs.err = errors.Errorf("Record incomplete %v", cfs.src) cfs.err = fmt.Errorf("Record incomplete %v", cfs.src)
return false return false
} }
cfs.fieldOID = binary.BigEndian.Uint32(cfs.src[cfs.rp:]) cfs.fieldOID = binary.BigEndian.Uint32(cfs.src[cfs.rp:])
@@ -373,7 +374,7 @@ func (cfs *CompositeBinaryScanner) Next() bool {
if fieldLen >= 0 { if fieldLen >= 0 {
if len(cfs.src[cfs.rp:]) < fieldLen { if len(cfs.src[cfs.rp:]) < fieldLen {
cfs.err = errors.Errorf("Record incomplete rp=%d src=%v", cfs.rp, cfs.src) cfs.err = fmt.Errorf("Record incomplete rp=%d src=%v", cfs.rp, cfs.src)
return false return false
} }
cfs.fieldBytes = cfs.src[cfs.rp : cfs.rp+fieldLen] cfs.fieldBytes = cfs.src[cfs.rp : cfs.rp+fieldLen]
@@ -416,15 +417,15 @@ type CompositeTextScanner struct {
// NewCompositeTextScanner a scanner over a text encoded composite value. // NewCompositeTextScanner a scanner over a text encoded composite value.
func NewCompositeTextScanner(ci *ConnInfo, src []byte) *CompositeTextScanner { func NewCompositeTextScanner(ci *ConnInfo, src []byte) *CompositeTextScanner {
if len(src) < 2 { if len(src) < 2 {
return &CompositeTextScanner{err: errors.Errorf("Record incomplete %v", src)} return &CompositeTextScanner{err: fmt.Errorf("Record incomplete %v", src)}
} }
if src[0] != '(' { if src[0] != '(' {
return &CompositeTextScanner{err: errors.Errorf("composite text format must start with '('")} return &CompositeTextScanner{err: fmt.Errorf("composite text format must start with '('")}
} }
if src[len(src)-1] != ')' { if src[len(src)-1] != ')' {
return &CompositeTextScanner{err: errors.Errorf("composite text format must end with ')'")} return &CompositeTextScanner{err: fmt.Errorf("composite text format must end with ')'")}
} }
return &CompositeTextScanner{ return &CompositeTextScanner{
@@ -543,7 +544,7 @@ func (b *CompositeBinaryBuilder) AppendValue(oid uint32, field interface{}) {
dt, ok := b.ci.DataTypeForOID(oid) dt, ok := b.ci.DataTypeForOID(oid)
if !ok { if !ok {
b.err = errors.Errorf("unknown data type for OID: %d", oid) b.err = fmt.Errorf("unknown data type for OID: %d", oid)
return return
} }
@@ -555,7 +556,7 @@ func (b *CompositeBinaryBuilder) AppendValue(oid uint32, field interface{}) {
binaryEncoder, ok := dt.Value.(BinaryEncoder) binaryEncoder, ok := dt.Value.(BinaryEncoder)
if !ok { if !ok {
b.err = errors.Errorf("unable to encode binary for OID: %d", oid) b.err = fmt.Errorf("unable to encode binary for OID: %d", oid)
return return
} }
@@ -618,7 +619,7 @@ func (b *CompositeTextBuilder) AppendValue(field interface{}) {
dt, ok := b.ci.DataTypeForValue(field) dt, ok := b.ci.DataTypeForValue(field)
if !ok { if !ok {
b.err = errors.Errorf("unknown data type for field: %v", field) b.err = fmt.Errorf("unknown data type for field: %v", field)
return return
} }
@@ -630,7 +631,7 @@ func (b *CompositeTextBuilder) AppendValue(field interface{}) {
textEncoder, ok := dt.Value.(TextEncoder) textEncoder, ok := dt.Value.(TextEncoder)
if !ok { if !ok {
b.err = errors.Errorf("unable to encode text for value: %v", field) b.err = fmt.Errorf("unable to encode text for value: %v", field)
return return
} }
+27 -28
View File
@@ -2,11 +2,10 @@ package pgtype
import ( import (
"database/sql" "database/sql"
"fmt"
"math" "math"
"reflect" "reflect"
"time" "time"
errors "golang.org/x/xerrors"
) )
const maxUint = ^uint(0) const maxUint = ^uint(0)
@@ -212,70 +211,70 @@ func int64AssignTo(srcVal int64, srcStatus Status, dst interface{}) error {
switch v := dst.(type) { switch v := dst.(type) {
case *int: case *int:
if srcVal < int64(minInt) { if srcVal < int64(minInt) {
return errors.Errorf("%d is less than minimum value for int", srcVal) return fmt.Errorf("%d is less than minimum value for int", srcVal)
} else if srcVal > int64(maxInt) { } else if srcVal > int64(maxInt) {
return errors.Errorf("%d is greater than maximum value for int", srcVal) return fmt.Errorf("%d is greater than maximum value for int", srcVal)
} }
*v = int(srcVal) *v = int(srcVal)
case *int8: case *int8:
if srcVal < math.MinInt8 { if srcVal < math.MinInt8 {
return errors.Errorf("%d is less than minimum value for int8", srcVal) return fmt.Errorf("%d is less than minimum value for int8", srcVal)
} else if srcVal > math.MaxInt8 { } else if srcVal > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for int8", srcVal) return fmt.Errorf("%d is greater than maximum value for int8", srcVal)
} }
*v = int8(srcVal) *v = int8(srcVal)
case *int16: case *int16:
if srcVal < math.MinInt16 { if srcVal < math.MinInt16 {
return errors.Errorf("%d is less than minimum value for int16", srcVal) return fmt.Errorf("%d is less than minimum value for int16", srcVal)
} else if srcVal > math.MaxInt16 { } else if srcVal > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for int16", srcVal) return fmt.Errorf("%d is greater than maximum value for int16", srcVal)
} }
*v = int16(srcVal) *v = int16(srcVal)
case *int32: case *int32:
if srcVal < math.MinInt32 { if srcVal < math.MinInt32 {
return errors.Errorf("%d is less than minimum value for int32", srcVal) return fmt.Errorf("%d is less than minimum value for int32", srcVal)
} else if srcVal > math.MaxInt32 { } else if srcVal > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for int32", srcVal) return fmt.Errorf("%d is greater than maximum value for int32", srcVal)
} }
*v = int32(srcVal) *v = int32(srcVal)
case *int64: case *int64:
if srcVal < math.MinInt64 { if srcVal < math.MinInt64 {
return errors.Errorf("%d is less than minimum value for int64", srcVal) return fmt.Errorf("%d is less than minimum value for int64", srcVal)
} else if srcVal > math.MaxInt64 { } else if srcVal > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for int64", srcVal) return fmt.Errorf("%d is greater than maximum value for int64", srcVal)
} }
*v = int64(srcVal) *v = int64(srcVal)
case *uint: case *uint:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for uint", srcVal) return fmt.Errorf("%d is less than zero for uint", srcVal)
} else if uint64(srcVal) > uint64(maxUint) { } else if uint64(srcVal) > uint64(maxUint) {
return errors.Errorf("%d is greater than maximum value for uint", srcVal) return fmt.Errorf("%d is greater than maximum value for uint", srcVal)
} }
*v = uint(srcVal) *v = uint(srcVal)
case *uint8: case *uint8:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for uint8", srcVal) return fmt.Errorf("%d is less than zero for uint8", srcVal)
} else if srcVal > math.MaxUint8 { } else if srcVal > math.MaxUint8 {
return errors.Errorf("%d is greater than maximum value for uint8", srcVal) return fmt.Errorf("%d is greater than maximum value for uint8", srcVal)
} }
*v = uint8(srcVal) *v = uint8(srcVal)
case *uint16: case *uint16:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for uint32", srcVal) return fmt.Errorf("%d is less than zero for uint32", srcVal)
} else if srcVal > math.MaxUint16 { } else if srcVal > math.MaxUint16 {
return errors.Errorf("%d is greater than maximum value for uint16", srcVal) return fmt.Errorf("%d is greater than maximum value for uint16", srcVal)
} }
*v = uint16(srcVal) *v = uint16(srcVal)
case *uint32: case *uint32:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for uint32", srcVal) return fmt.Errorf("%d is less than zero for uint32", srcVal)
} else if srcVal > math.MaxUint32 { } else if srcVal > math.MaxUint32 {
return errors.Errorf("%d is greater than maximum value for uint32", srcVal) return fmt.Errorf("%d is greater than maximum value for uint32", srcVal)
} }
*v = uint32(srcVal) *v = uint32(srcVal)
case *uint64: case *uint64:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for uint64", srcVal) return fmt.Errorf("%d is less than zero for uint64", srcVal)
} }
*v = uint64(srcVal) *v = uint64(srcVal)
case sql.Scanner: case sql.Scanner:
@@ -293,22 +292,22 @@ func int64AssignTo(srcVal int64, srcStatus Status, dst interface{}) error {
return int64AssignTo(srcVal, srcStatus, el.Interface()) return int64AssignTo(srcVal, srcStatus, el.Interface())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if el.OverflowInt(int64(srcVal)) { if el.OverflowInt(int64(srcVal)) {
return errors.Errorf("cannot put %d into %T", srcVal, dst) return fmt.Errorf("cannot put %d into %T", srcVal, dst)
} }
el.SetInt(int64(srcVal)) el.SetInt(int64(srcVal))
return nil return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if srcVal < 0 { if srcVal < 0 {
return errors.Errorf("%d is less than zero for %T", srcVal, dst) return fmt.Errorf("%d is less than zero for %T", srcVal, dst)
} }
if el.OverflowUint(uint64(srcVal)) { if el.OverflowUint(uint64(srcVal)) {
return errors.Errorf("cannot put %d into %T", srcVal, dst) return fmt.Errorf("cannot put %d into %T", srcVal, dst)
} }
el.SetUint(uint64(srcVal)) el.SetUint(uint64(srcVal))
return nil return nil
} }
} }
return errors.Errorf("cannot assign %v into %T", srcVal, dst) return fmt.Errorf("cannot assign %v into %T", srcVal, dst)
} }
return nil return nil
} }
@@ -322,7 +321,7 @@ func int64AssignTo(srcVal int64, srcStatus Status, dst interface{}) error {
} }
} }
return errors.Errorf("cannot assign %v %v into %T", srcVal, srcStatus, dst) return fmt.Errorf("cannot assign %v %v into %T", srcVal, srcStatus, dst)
} }
func float64AssignTo(srcVal float64, srcStatus Status, dst interface{}) error { func float64AssignTo(srcVal float64, srcStatus Status, dst interface{}) error {
@@ -350,7 +349,7 @@ func float64AssignTo(srcVal float64, srcStatus Status, dst interface{}) error {
} }
} }
} }
return errors.Errorf("cannot assign %v into %T", srcVal, dst) return fmt.Errorf("cannot assign %v into %T", srcVal, dst)
} }
return nil return nil
} }
@@ -364,7 +363,7 @@ func float64AssignTo(srcVal float64, srcStatus Status, dst interface{}) error {
} }
} }
return errors.Errorf("cannot assign %v %v into %T", srcVal, srcStatus, dst) return fmt.Errorf("cannot assign %v %v into %T", srcVal, srcStatus, dst)
} }
func NullAssignTo(dst interface{}) error { func NullAssignTo(dst interface{}) error {
+1 -1
View File
@@ -2,12 +2,12 @@ package pgtype_test
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"github.com/jackc/pgtype" "github.com/jackc/pgtype"
pgx "github.com/jackc/pgx/v4" pgx "github.com/jackc/pgx/v4"
errors "golang.org/x/xerrors"
) )
type MyType struct { type MyType struct {
+1 -2
View File
@@ -2,8 +2,7 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"errors"
errors "golang.org/x/xerrors"
) )
func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) { func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) {
+7 -7
View File
@@ -4,10 +4,10 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Date struct { type Date struct {
@@ -55,7 +55,7 @@ func (dst *Date) Set(src interface{}) error {
if originalSrc, ok := underlyingTimeType(src); ok { if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Date", value) return fmt.Errorf("cannot convert %v to Date", value)
} }
return nil return nil
@@ -81,7 +81,7 @@ func (src *Date) AssignTo(dst interface{}) error {
switch v := dst.(type) { switch v := dst.(type) {
case *time.Time: case *time.Time:
if src.InfinityModifier != None { if src.InfinityModifier != None {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
*v = src.Time *v = src.Time
return nil return nil
@@ -89,13 +89,13 @@ func (src *Date) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Date) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Date) DecodeText(ci *ConnInfo, src []byte) error {
@@ -129,7 +129,7 @@ func (dst *Date) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length for date: %v", len(src)) return fmt.Errorf("invalid length for date: %v", len(src))
} }
dayOffset := int32(binary.BigEndian.Uint32(src)) dayOffset := int32(binary.BigEndian.Uint32(src))
@@ -213,7 +213,7 @@ func (dst *Date) Scan(src interface{}) error {
return nil return nil
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type DateArray struct { type DateArray struct {
@@ -97,7 +97,7 @@ func (dst *DateArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for DateArray", src) return fmt.Errorf("cannot find dimensions of %v for DateArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = DateArray{Status: Present} *dst = DateArray{Status: Present}
@@ -107,7 +107,7 @@ func (dst *DateArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to DateArray", src) return fmt.Errorf("cannot convert %v to DateArray", src)
} }
*dst = DateArray{ *dst = DateArray{
@@ -138,7 +138,7 @@ func (dst *DateArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to DateArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to DateArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -156,7 +156,7 @@ func (dst *DateArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -169,10 +169,10 @@ func (dst *DateArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to DateArray") return 0, fmt.Errorf("cannot convert all values to DateArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in DateArray", err) return 0, fmt.Errorf("%v in DateArray", err)
} }
index++ index++
@@ -234,7 +234,7 @@ func (src *DateArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -249,7 +249,7 @@ func (src *DateArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -257,7 +257,7 @@ func (src *DateArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *DateArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *DateArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -273,7 +273,7 @@ func (src *DateArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -291,14 +291,14 @@ func (src *DateArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from DateArray") return 0, fmt.Errorf("cannot assign all values from DateArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from DateArray") return 0, fmt.Errorf("cannot assign all values from DateArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -457,7 +457,7 @@ func (src DateArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("date"); ok { if dt, ok := ci.DataTypeForName("date"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "date") return nil, fmt.Errorf("unable to find oid for type name %v", "date")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -501,7 +501,7 @@ func (dst *DateArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Daterange struct { type Daterange struct {
@@ -30,7 +30,7 @@ func (dst *Daterange) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Daterange", src) return fmt.Errorf("cannot convert %v to Daterange", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Daterange) Get() interface{} {
} }
func (src *Daterange) AssignTo(dst interface{}) error { func (src *Daterange) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Daterange) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Daterange) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Daterange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Daterange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Daterange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Daterange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Daterange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Daterange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Daterange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Daterange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Daterange) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+15 -16
View File
@@ -4,9 +4,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"reflect" "reflect"
errors "golang.org/x/xerrors"
) )
type EnumArray struct { type EnumArray struct {
@@ -94,7 +93,7 @@ func (dst *EnumArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for EnumArray", src) return fmt.Errorf("cannot find dimensions of %v for EnumArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = EnumArray{Status: Present} *dst = EnumArray{Status: Present}
@@ -104,7 +103,7 @@ func (dst *EnumArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to EnumArray", src) return fmt.Errorf("cannot convert %v to EnumArray", src)
} }
*dst = EnumArray{ *dst = EnumArray{
@@ -135,7 +134,7 @@ func (dst *EnumArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to EnumArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to EnumArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -153,7 +152,7 @@ func (dst *EnumArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -166,10 +165,10 @@ func (dst *EnumArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to EnumArray") return 0, fmt.Errorf("cannot convert all values to EnumArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in EnumArray", err) return 0, fmt.Errorf("%v in EnumArray", err)
} }
index++ index++
@@ -231,7 +230,7 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -246,7 +245,7 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -254,7 +253,7 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *EnumArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *EnumArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -270,7 +269,7 @@ func (src *EnumArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -288,14 +287,14 @@ func (src *EnumArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from EnumArray") return 0, fmt.Errorf("cannot assign all values from EnumArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from EnumArray") return 0, fmt.Errorf("cannot assign all values from EnumArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -412,7 +411,7 @@ func (dst *EnumArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+4 -4
View File
@@ -1,6 +1,6 @@
package pgtype package pgtype
import errors "golang.org/x/xerrors" import "fmt"
// EnumType represents a enum type. While it implements Value, this is only in service of its type conversion duties // EnumType represents a enum type. While it implements Value, this is only in service of its type conversion duties
// when registered as a data type in a ConnType. It should not be used directly as a Value. // when registered as a data type in a ConnType. It should not be used directly as a Value.
@@ -79,7 +79,7 @@ func (dst *EnumType) Set(src interface{}) error {
if originalSrc, ok := underlyingStringType(src); ok { if originalSrc, ok := underlyingStringType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to enum %s", value, dst.typeName) return fmt.Errorf("cannot convert %v to enum %s", value, dst.typeName)
} }
return nil return nil
@@ -111,13 +111,13 @@ func (src *EnumType) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (EnumType) PreferredResultFormat() int16 { func (EnumType) PreferredResultFormat() int16 {
+8 -8
View File
@@ -2,8 +2,8 @@ package uuid
import ( import (
"database/sql/driver" "database/sql/driver"
"errors"
errors "golang.org/x/xerrors" "fmt"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/jackc/pgtype" "github.com/jackc/pgtype"
@@ -37,7 +37,7 @@ func (dst *UUID) Set(src interface{}) error {
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present} *dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
case []byte: case []byte:
if len(value) != 16 { if len(value) != 16 {
return errors.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value)) return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
} }
*dst = UUID{Status: pgtype.Present} *dst = UUID{Status: pgtype.Present}
copy(dst.UUID[:], value) copy(dst.UUID[:], value)
@@ -51,7 +51,7 @@ func (dst *UUID) Set(src interface{}) error {
// If all else fails see if pgtype.UUID can handle it. If so, translate through that. // If all else fails see if pgtype.UUID can handle it. If so, translate through that.
pgUUID := &pgtype.UUID{} pgUUID := &pgtype.UUID{}
if err := pgUUID.Set(value); err != nil { if err := pgUUID.Set(value); err != nil {
return errors.Errorf("cannot convert %v to UUID", value) return fmt.Errorf("cannot convert %v to UUID", value)
} }
*dst = UUID{UUID: uuid.UUID(pgUUID.Bytes), Status: pgUUID.Status} *dst = UUID{UUID: uuid.UUID(pgUUID.Bytes), Status: pgUUID.Status}
@@ -92,13 +92,13 @@ func (src *UUID) AssignTo(dst interface{}) error {
if nextDst, retry := pgtype.GetAssignToDstType(v); retry { if nextDst, retry := pgtype.GetAssignToDstType(v); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case pgtype.Null: case pgtype.Null:
return pgtype.NullAssignTo(dst) return pgtype.NullAssignTo(dst)
} }
return errors.Errorf("cannot assign %v into %T", src, dst) return fmt.Errorf("cannot assign %v into %T", src, dst)
} }
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error { func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
@@ -123,7 +123,7 @@ func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
} }
if len(src) != 16 { if len(src) != 16 {
return errors.Errorf("invalid length for UUID: %v", len(src)) return fmt.Errorf("invalid length for UUID: %v", len(src))
} }
*dst = UUID{Status: pgtype.Present} *dst = UUID{Status: pgtype.Present}
@@ -167,7 +167,7 @@ func (dst *UUID) Scan(src interface{}) error {
return dst.DecodeText(nil, src) return dst.DecodeText(nil, src)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+27 -27
View File
@@ -2,10 +2,10 @@ package numeric
import ( import (
"database/sql/driver" "database/sql/driver"
"errors"
"fmt"
"strconv" "strconv"
errors "golang.org/x/xerrors"
"github.com/jackc/pgtype" "github.com/jackc/pgtype"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
@@ -78,17 +78,17 @@ func (dst *Numeric) Set(src interface{}) error {
// If all else fails see if pgtype.Numeric can handle it. If so, translate through that. // If all else fails see if pgtype.Numeric can handle it. If so, translate through that.
num := &pgtype.Numeric{} num := &pgtype.Numeric{}
if err := num.Set(value); err != nil { if err := num.Set(value); err != nil {
return errors.Errorf("cannot convert %v to Numeric", value) return fmt.Errorf("cannot convert %v to Numeric", value)
} }
buf, err := num.EncodeText(nil, nil) buf, err := num.EncodeText(nil, nil)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to Numeric", value) return fmt.Errorf("cannot convert %v to Numeric", value)
} }
dec, err := decimal.NewFromString(string(buf)) dec, err := decimal.NewFromString(string(buf))
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to Numeric", value) return fmt.Errorf("cannot convert %v to Numeric", value)
} }
*dst = Numeric{Decimal: dec, Status: pgtype.Present} *dst = Numeric{Decimal: dec, Status: pgtype.Present}
} }
@@ -121,99 +121,99 @@ func (src *Numeric) AssignTo(dst interface{}) error {
*v = f *v = f
case *int: case *int:
if src.Decimal.Exponent() < 0 { if src.Decimal.Exponent() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseInt(src.Decimal.String(), 10, strconv.IntSize) n, err := strconv.ParseInt(src.Decimal.String(), 10, strconv.IntSize)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = int(n) *v = int(n)
case *int8: case *int8:
if src.Decimal.Exponent() < 0 { if src.Decimal.Exponent() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseInt(src.Decimal.String(), 10, 8) n, err := strconv.ParseInt(src.Decimal.String(), 10, 8)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = int8(n) *v = int8(n)
case *int16: case *int16:
if src.Decimal.Exponent() < 0 { if src.Decimal.Exponent() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseInt(src.Decimal.String(), 10, 16) n, err := strconv.ParseInt(src.Decimal.String(), 10, 16)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = int16(n) *v = int16(n)
case *int32: case *int32:
if src.Decimal.Exponent() < 0 { if src.Decimal.Exponent() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseInt(src.Decimal.String(), 10, 32) n, err := strconv.ParseInt(src.Decimal.String(), 10, 32)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = int32(n) *v = int32(n)
case *int64: case *int64:
if src.Decimal.Exponent() < 0 { if src.Decimal.Exponent() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseInt(src.Decimal.String(), 10, 64) n, err := strconv.ParseInt(src.Decimal.String(), 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = int64(n) *v = int64(n)
case *uint: case *uint:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 { if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseUint(src.Decimal.String(), 10, strconv.IntSize) n, err := strconv.ParseUint(src.Decimal.String(), 10, strconv.IntSize)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = uint(n) *v = uint(n)
case *uint8: case *uint8:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 { if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseUint(src.Decimal.String(), 10, 8) n, err := strconv.ParseUint(src.Decimal.String(), 10, 8)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = uint8(n) *v = uint8(n)
case *uint16: case *uint16:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 { if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseUint(src.Decimal.String(), 10, 16) n, err := strconv.ParseUint(src.Decimal.String(), 10, 16)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = uint16(n) *v = uint16(n)
case *uint32: case *uint32:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 { if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseUint(src.Decimal.String(), 10, 32) n, err := strconv.ParseUint(src.Decimal.String(), 10, 32)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = uint32(n) *v = uint32(n)
case *uint64: case *uint64:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 { if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
n, err := strconv.ParseUint(src.Decimal.String(), 10, 64) n, err := strconv.ParseUint(src.Decimal.String(), 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot convert %v to %T", dst, *v) return fmt.Errorf("cannot convert %v to %T", dst, *v)
} }
*v = uint64(n) *v = uint64(n)
default: default:
if nextDst, retry := pgtype.GetAssignToDstType(dst); retry { if nextDst, retry := pgtype.GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case pgtype.Null: case pgtype.Null:
return pgtype.NullAssignTo(dst) return pgtype.NullAssignTo(dst)
@@ -300,7 +300,7 @@ func (dst *Numeric) Scan(src interface{}) error {
return dst.DecodeText(nil, src) return dst.DecodeText(nil, src)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+10 -10
View File
@@ -3,11 +3,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Float4 struct { type Float4 struct {
@@ -46,42 +46,42 @@ func (dst *Float4) Set(src interface{}) error {
if int32(f32) == value { if int32(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case uint32: case uint32:
f32 := float32(value) f32 := float32(value)
if uint32(f32) == value { if uint32(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case int64: case int64:
f32 := float32(value) f32 := float32(value)
if int64(f32) == value { if int64(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case uint64: case uint64:
f32 := float32(value) f32 := float32(value)
if uint64(f32) == value { if uint64(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case int: case int:
f32 := float32(value) f32 := float32(value)
if int(f32) == value { if int(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case uint: case uint:
f32 := float32(value) f32 := float32(value)
if uint(f32) == value { if uint(f32) == value {
*dst = Float4{Float: f32, Status: Present} *dst = Float4{Float: f32, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float32", value) return fmt.Errorf("%v cannot be exactly represented as float32", value)
} }
case string: case string:
num, err := strconv.ParseFloat(value, 32) num, err := strconv.ParseFloat(value, 32)
@@ -171,7 +171,7 @@ func (dst *Float4) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Float8", value) return fmt.Errorf("cannot convert %v to Float8", value)
} }
return nil return nil
@@ -214,7 +214,7 @@ func (dst *Float4) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length for float4: %v", len(src)) return fmt.Errorf("invalid length for float4: %v", len(src))
} }
n := int32(binary.BigEndian.Uint32(src)) n := int32(binary.BigEndian.Uint32(src))
@@ -266,7 +266,7 @@ func (dst *Float4) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Float4Array struct { type Float4Array struct {
@@ -96,7 +96,7 @@ func (dst *Float4Array) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for Float4Array", src) return fmt.Errorf("cannot find dimensions of %v for Float4Array", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = Float4Array{Status: Present} *dst = Float4Array{Status: Present}
@@ -106,7 +106,7 @@ func (dst *Float4Array) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Float4Array", src) return fmt.Errorf("cannot convert %v to Float4Array", src)
} }
*dst = Float4Array{ *dst = Float4Array{
@@ -137,7 +137,7 @@ func (dst *Float4Array) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to Float4Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to Float4Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *Float4Array) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *Float4Array) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to Float4Array") return 0, fmt.Errorf("cannot convert all values to Float4Array")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in Float4Array", err) return 0, fmt.Errorf("%v in Float4Array", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *Float4Array) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *Float4Array) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *Float4Array) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *Float4Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *Float4Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *Float4Array) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *Float4Array) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from Float4Array") return 0, fmt.Errorf("cannot assign all values from Float4Array")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from Float4Array") return 0, fmt.Errorf("cannot assign all values from Float4Array")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src Float4Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("float4"); ok { if dt, ok := ci.DataTypeForName("float4"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "float4") return nil, fmt.Errorf("unable to find oid for type name %v", "float4")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *Float4Array) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+8 -8
View File
@@ -3,11 +3,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Float8 struct { type Float8 struct {
@@ -50,28 +50,28 @@ func (dst *Float8) Set(src interface{}) error {
if int64(f64) == value { if int64(f64) == value {
*dst = Float8{Float: f64, Status: Present} *dst = Float8{Float: f64, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float64", value) return fmt.Errorf("%v cannot be exactly represented as float64", value)
} }
case uint64: case uint64:
f64 := float64(value) f64 := float64(value)
if uint64(f64) == value { if uint64(f64) == value {
*dst = Float8{Float: f64, Status: Present} *dst = Float8{Float: f64, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float64", value) return fmt.Errorf("%v cannot be exactly represented as float64", value)
} }
case int: case int:
f64 := float64(value) f64 := float64(value)
if int(f64) == value { if int(f64) == value {
*dst = Float8{Float: f64, Status: Present} *dst = Float8{Float: f64, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float64", value) return fmt.Errorf("%v cannot be exactly represented as float64", value)
} }
case uint: case uint:
f64 := float64(value) f64 := float64(value)
if uint(f64) == value { if uint(f64) == value {
*dst = Float8{Float: f64, Status: Present} *dst = Float8{Float: f64, Status: Present}
} else { } else {
return errors.Errorf("%v cannot be exactly represented as float64", value) return fmt.Errorf("%v cannot be exactly represented as float64", value)
} }
case string: case string:
num, err := strconv.ParseFloat(value, 64) num, err := strconv.ParseFloat(value, 64)
@@ -161,7 +161,7 @@ func (dst *Float8) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Float8", value) return fmt.Errorf("cannot convert %v to Float8", value)
} }
return nil return nil
@@ -204,7 +204,7 @@ func (dst *Float8) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for float4: %v", len(src)) return fmt.Errorf("invalid length for float4: %v", len(src))
} }
n := int64(binary.BigEndian.Uint64(src)) n := int64(binary.BigEndian.Uint64(src))
@@ -256,7 +256,7 @@ func (dst *Float8) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Float8Array struct { type Float8Array struct {
@@ -96,7 +96,7 @@ func (dst *Float8Array) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for Float8Array", src) return fmt.Errorf("cannot find dimensions of %v for Float8Array", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = Float8Array{Status: Present} *dst = Float8Array{Status: Present}
@@ -106,7 +106,7 @@ func (dst *Float8Array) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Float8Array", src) return fmt.Errorf("cannot convert %v to Float8Array", src)
} }
*dst = Float8Array{ *dst = Float8Array{
@@ -137,7 +137,7 @@ func (dst *Float8Array) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to Float8Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to Float8Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *Float8Array) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *Float8Array) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to Float8Array") return 0, fmt.Errorf("cannot convert all values to Float8Array")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in Float8Array", err) return 0, fmt.Errorf("%v in Float8Array", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *Float8Array) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *Float8Array) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *Float8Array) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *Float8Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *Float8Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *Float8Array) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *Float8Array) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from Float8Array") return 0, fmt.Errorf("cannot assign all values from Float8Array")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from Float8Array") return 0, fmt.Errorf("cannot assign all values from Float8Array")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src Float8Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("float8"); ok { if dt, ok := ci.DataTypeForName("float8"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "float8") return nil, fmt.Errorf("unable to find oid for type name %v", "float8")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *Float8Array) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
-1
View File
@@ -10,5 +10,4 @@ require (
github.com/lib/pq v1.3.0 github.com/lib/pq v1.3.0
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
) )
+17 -17
View File
@@ -4,12 +4,12 @@ import (
"bytes" "bytes"
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"errors"
"fmt"
"strings" "strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
errors "golang.org/x/xerrors"
"github.com/jackc/pgio" "github.com/jackc/pgio"
) )
@@ -41,7 +41,7 @@ func (dst *Hstore) Set(src interface{}) error {
} }
*dst = Hstore{Map: m, Status: Present} *dst = Hstore{Map: m, Status: Present}
default: default:
return errors.Errorf("cannot convert %v to Hstore", src) return fmt.Errorf("cannot convert %v to Hstore", src)
} }
return nil return nil
@@ -66,7 +66,7 @@ func (src *Hstore) AssignTo(dst interface{}) error {
*v = make(map[string]string, len(src.Map)) *v = make(map[string]string, len(src.Map))
for k, val := range src.Map { for k, val := range src.Map {
if val.Status != Present { if val.Status != Present {
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
(*v)[k] = val.String (*v)[k] = val.String
} }
@@ -75,13 +75,13 @@ func (src *Hstore) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Hstore) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Hstore) DecodeText(ci *ConnInfo, src []byte) error {
@@ -113,7 +113,7 @@ func (dst *Hstore) DecodeBinary(ci *ConnInfo, src []byte) error {
rp := 0 rp := 0
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return errors.Errorf("hstore incomplete %v", src) return fmt.Errorf("hstore incomplete %v", src)
} }
pairCount := int(int32(binary.BigEndian.Uint32(src[rp:]))) pairCount := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4 rp += 4
@@ -122,19 +122,19 @@ func (dst *Hstore) DecodeBinary(ci *ConnInfo, src []byte) error {
for i := 0; i < pairCount; i++ { for i := 0; i < pairCount; i++ {
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return errors.Errorf("hstore incomplete %v", src) return fmt.Errorf("hstore incomplete %v", src)
} }
keyLen := int(int32(binary.BigEndian.Uint32(src[rp:]))) keyLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4 rp += 4
if len(src[rp:]) < keyLen { if len(src[rp:]) < keyLen {
return errors.Errorf("hstore incomplete %v", src) return fmt.Errorf("hstore incomplete %v", src)
} }
key := string(src[rp : rp+keyLen]) key := string(src[rp : rp+keyLen])
rp += keyLen rp += keyLen
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return errors.Errorf("hstore incomplete %v", src) return fmt.Errorf("hstore incomplete %v", src)
} }
valueLen := int(int32(binary.BigEndian.Uint32(src[rp:]))) valueLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4 rp += 4
@@ -338,13 +338,13 @@ func parseHstore(s string) (k []string, v []Text, err error) {
case r == 'N': case r == 'N':
state = hsNul state = hsNul
default: default:
err = errors.Errorf("Invalid character '%c' after '=>', expecting '\"' or 'NULL'", r) err = fmt.Errorf("Invalid character '%c' after '=>', expecting '\"' or 'NULL'", r)
} }
default: default:
err = errors.Errorf("Invalid character after '=', expecting '>'") err = fmt.Errorf("Invalid character after '=', expecting '>'")
} }
} else { } else {
err = errors.Errorf("Invalid character '%c' after value, expecting '='", r) err = fmt.Errorf("Invalid character '%c' after value, expecting '='", r)
} }
case hsVal: case hsVal:
switch r { switch r {
@@ -381,7 +381,7 @@ func parseHstore(s string) (k []string, v []Text, err error) {
values = append(values, Text{Status: Null}) values = append(values, Text{Status: Null})
state = hsNext state = hsNext
} else { } else {
err = errors.Errorf("Invalid NULL value: 'N%s'", string(nulBuf)) err = fmt.Errorf("Invalid NULL value: 'N%s'", string(nulBuf))
} }
case hsNext: case hsNext:
if r == ',' { if r == ',' {
@@ -393,10 +393,10 @@ func parseHstore(s string) (k []string, v []Text, err error) {
r, end = p.Consume() r, end = p.Consume()
state = hsKey state = hsKey
default: default:
err = errors.Errorf("Invalid character '%c' after ', ', expecting \"", r) err = fmt.Errorf("Invalid character '%c' after ', ', expecting \"", r)
} }
} else { } else {
err = errors.Errorf("Invalid character '%c' after value, expecting ','", r) err = fmt.Errorf("Invalid character '%c' after value, expecting ','", r)
} }
} }
@@ -430,7 +430,7 @@ func (dst *Hstore) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type HstoreArray struct { type HstoreArray struct {
@@ -77,7 +77,7 @@ func (dst *HstoreArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for HstoreArray", src) return fmt.Errorf("cannot find dimensions of %v for HstoreArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = HstoreArray{Status: Present} *dst = HstoreArray{Status: Present}
@@ -87,7 +87,7 @@ func (dst *HstoreArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to HstoreArray", src) return fmt.Errorf("cannot convert %v to HstoreArray", src)
} }
*dst = HstoreArray{ *dst = HstoreArray{
@@ -118,7 +118,7 @@ func (dst *HstoreArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to HstoreArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to HstoreArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -136,7 +136,7 @@ func (dst *HstoreArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -149,10 +149,10 @@ func (dst *HstoreArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to HstoreArray") return 0, fmt.Errorf("cannot convert all values to HstoreArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in HstoreArray", err) return 0, fmt.Errorf("%v in HstoreArray", err)
} }
index++ index++
@@ -205,7 +205,7 @@ func (src *HstoreArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -220,7 +220,7 @@ func (src *HstoreArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -228,7 +228,7 @@ func (src *HstoreArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *HstoreArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *HstoreArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -244,7 +244,7 @@ func (src *HstoreArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -262,14 +262,14 @@ func (src *HstoreArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from HstoreArray") return 0, fmt.Errorf("cannot assign all values from HstoreArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from HstoreArray") return 0, fmt.Errorf("cannot assign all values from HstoreArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -428,7 +428,7 @@ func (src HstoreArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("hstore"); ok { if dt, ok := ci.DataTypeForName("hstore"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "hstore") return nil, fmt.Errorf("unable to find oid for type name %v", "hstore")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -472,7 +472,7 @@ func (dst *HstoreArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+8 -9
View File
@@ -2,9 +2,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"net" "net"
errors "golang.org/x/xerrors"
) )
// Network address family is dependent on server socket.h value for AF_INET. // Network address family is dependent on server socket.h value for AF_INET.
@@ -73,7 +72,7 @@ func (dst *Inet) Set(src interface{}) error {
if originalSrc, ok := underlyingPtrType(src); ok { if originalSrc, ok := underlyingPtrType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Inet", value) return fmt.Errorf("cannot convert %v to Inet", value)
} }
return nil return nil
@@ -104,7 +103,7 @@ func (src *Inet) AssignTo(dst interface{}) error {
return nil return nil
case *net.IP: case *net.IP:
if oneCount, bitCount := src.IPNet.Mask.Size(); oneCount != bitCount { if oneCount, bitCount := src.IPNet.Mask.Size(); oneCount != bitCount {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
*v = make(net.IP, len(src.IPNet.IP)) *v = make(net.IP, len(src.IPNet.IP))
copy(*v, src.IPNet.IP) copy(*v, src.IPNet.IP)
@@ -113,13 +112,13 @@ func (src *Inet) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Inet) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Inet) DecodeText(ci *ConnInfo, src []byte) error {
@@ -157,7 +156,7 @@ func (dst *Inet) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 && len(src) != 20 { if len(src) != 8 && len(src) != 20 {
return errors.Errorf("Received an invalid size for a inet: %d", len(src)) return fmt.Errorf("Received an invalid size for a inet: %d", len(src))
} }
// ignore family // ignore family
@@ -202,7 +201,7 @@ func (src Inet) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case net.IPv6len: case net.IPv6len:
family = defaultAFInet6 family = defaultAFInet6
default: default:
return nil, errors.Errorf("Unexpected IP length: %v", len(src.IPNet.IP)) return nil, fmt.Errorf("Unexpected IP length: %v", len(src.IPNet.IP))
} }
buf = append(buf, family) buf = append(buf, family)
@@ -234,7 +233,7 @@ func (dst *Inet) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"net" "net"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type InetArray struct { type InetArray struct {
@@ -116,7 +116,7 @@ func (dst *InetArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for InetArray", src) return fmt.Errorf("cannot find dimensions of %v for InetArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = InetArray{Status: Present} *dst = InetArray{Status: Present}
@@ -126,7 +126,7 @@ func (dst *InetArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to InetArray", src) return fmt.Errorf("cannot convert %v to InetArray", src)
} }
*dst = InetArray{ *dst = InetArray{
@@ -157,7 +157,7 @@ func (dst *InetArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to InetArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to InetArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -175,7 +175,7 @@ func (dst *InetArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -188,10 +188,10 @@ func (dst *InetArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to InetArray") return 0, fmt.Errorf("cannot convert all values to InetArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in InetArray", err) return 0, fmt.Errorf("%v in InetArray", err)
} }
index++ index++
@@ -262,7 +262,7 @@ func (src *InetArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -277,7 +277,7 @@ func (src *InetArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -285,7 +285,7 @@ func (src *InetArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *InetArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *InetArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -301,7 +301,7 @@ func (src *InetArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -319,14 +319,14 @@ func (src *InetArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from InetArray") return 0, fmt.Errorf("cannot assign all values from InetArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from InetArray") return 0, fmt.Errorf("cannot assign all values from InetArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -485,7 +485,7 @@ func (src InetArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("inet"); ok { if dt, ok := ci.DataTypeForName("inet"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "inet") return nil, fmt.Errorf("unable to find oid for type name %v", "inet")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -529,7 +529,7 @@ func (dst *InetArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+18 -18
View File
@@ -3,11 +3,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int2 struct { type Int2 struct {
@@ -37,46 +37,46 @@ func (dst *Int2) Set(src interface{}) error {
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case uint16: case uint16:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case int32: case int32:
if value < math.MinInt16 { if value < math.MinInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case uint32: case uint32:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case int64: case int64:
if value < math.MinInt16 { if value < math.MinInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case uint64: case uint64:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case int: case int:
if value < math.MinInt16 { if value < math.MinInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case uint: case uint:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%d is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case string: case string:
@@ -87,12 +87,12 @@ func (dst *Int2) Set(src interface{}) error {
*dst = Int2{Int: int16(num), Status: Present} *dst = Int2{Int: int16(num), Status: Present}
case float32: case float32:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%f is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case float64: case float64:
if value > math.MaxInt16 { if value > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", value) return fmt.Errorf("%f is greater than maximum value for Int2", value)
} }
*dst = Int2{Int: int16(value), Status: Present} *dst = Int2{Int: int16(value), Status: Present}
case *int8: case *int8:
@@ -177,7 +177,7 @@ func (dst *Int2) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int2", value) return fmt.Errorf("cannot convert %v to Int2", value)
} }
return nil return nil
@@ -220,7 +220,7 @@ func (dst *Int2) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 2 { if len(src) != 2 {
return errors.Errorf("invalid length for int2: %v", len(src)) return fmt.Errorf("invalid length for int2: %v", len(src))
} }
n := int16(binary.BigEndian.Uint16(src)) n := int16(binary.BigEndian.Uint16(src))
@@ -260,10 +260,10 @@ func (dst *Int2) Scan(src interface{}) error {
switch src := src.(type) { switch src := src.(type) {
case int64: case int64:
if src < math.MinInt16 { if src < math.MinInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", src) return fmt.Errorf("%d is greater than maximum value for Int2", src)
} }
if src > math.MaxInt16 { if src > math.MaxInt16 {
return errors.Errorf("%d is greater than maximum value for Int2", src) return fmt.Errorf("%d is greater than maximum value for Int2", src)
} }
*dst = Int2{Int: int16(src), Status: Present} *dst = Int2{Int: int16(src), Status: Present}
return nil return nil
@@ -275,7 +275,7 @@ func (dst *Int2) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int2Array struct { type Int2Array struct {
@@ -362,7 +362,7 @@ func (dst *Int2Array) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for Int2Array", src) return fmt.Errorf("cannot find dimensions of %v for Int2Array", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = Int2Array{Status: Present} *dst = Int2Array{Status: Present}
@@ -372,7 +372,7 @@ func (dst *Int2Array) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int2Array", src) return fmt.Errorf("cannot convert %v to Int2Array", src)
} }
*dst = Int2Array{ *dst = Int2Array{
@@ -403,7 +403,7 @@ func (dst *Int2Array) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to Int2Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to Int2Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -421,7 +421,7 @@ func (dst *Int2Array) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -434,10 +434,10 @@ func (dst *Int2Array) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to Int2Array") return 0, fmt.Errorf("cannot convert all values to Int2Array")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in Int2Array", err) return 0, fmt.Errorf("%v in Int2Array", err)
} }
index++ index++
@@ -625,7 +625,7 @@ func (src *Int2Array) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -640,7 +640,7 @@ func (src *Int2Array) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -648,7 +648,7 @@ func (src *Int2Array) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *Int2Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *Int2Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -664,7 +664,7 @@ func (src *Int2Array) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -682,14 +682,14 @@ func (src *Int2Array) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from Int2Array") return 0, fmt.Errorf("cannot assign all values from Int2Array")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from Int2Array") return 0, fmt.Errorf("cannot assign all values from Int2Array")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -848,7 +848,7 @@ func (src Int2Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("int2"); ok { if dt, ok := ci.DataTypeForName("int2"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "int2") return nil, fmt.Errorf("unable to find oid for type name %v", "int2")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -892,7 +892,7 @@ func (dst *Int2Array) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+15 -15
View File
@@ -4,11 +4,11 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int4 struct { type Int4 struct {
@@ -42,33 +42,33 @@ func (dst *Int4) Set(src interface{}) error {
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case uint32: case uint32:
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case int64: case int64:
if value < math.MinInt32 { if value < math.MinInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case uint64: case uint64:
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case int: case int:
if value < math.MinInt32 { if value < math.MinInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case uint: case uint:
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%d is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case string: case string:
@@ -79,12 +79,12 @@ func (dst *Int4) Set(src interface{}) error {
*dst = Int4{Int: int32(num), Status: Present} *dst = Int4{Int: int32(num), Status: Present}
case float32: case float32:
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%f is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case float64: case float64:
if value > math.MaxInt32 { if value > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", value) return fmt.Errorf("%f is greater than maximum value for Int4", value)
} }
*dst = Int4{Int: int32(value), Status: Present} *dst = Int4{Int: int32(value), Status: Present}
case *int8: case *int8:
@@ -169,7 +169,7 @@ func (dst *Int4) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int4", value) return fmt.Errorf("cannot convert %v to Int4", value)
} }
return nil return nil
@@ -212,7 +212,7 @@ func (dst *Int4) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length for int4: %v", len(src)) return fmt.Errorf("invalid length for int4: %v", len(src))
} }
n := int32(binary.BigEndian.Uint32(src)) n := int32(binary.BigEndian.Uint32(src))
@@ -252,10 +252,10 @@ func (dst *Int4) Scan(src interface{}) error {
switch src := src.(type) { switch src := src.(type) {
case int64: case int64:
if src < math.MinInt32 { if src < math.MinInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", src) return fmt.Errorf("%d is greater than maximum value for Int4", src)
} }
if src > math.MaxInt32 { if src > math.MaxInt32 {
return errors.Errorf("%d is greater than maximum value for Int4", src) return fmt.Errorf("%d is greater than maximum value for Int4", src)
} }
*dst = Int4{Int: int32(src), Status: Present} *dst = Int4{Int: int32(src), Status: Present}
return nil return nil
@@ -267,7 +267,7 @@ func (dst *Int4) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int4Array struct { type Int4Array struct {
@@ -362,7 +362,7 @@ func (dst *Int4Array) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for Int4Array", src) return fmt.Errorf("cannot find dimensions of %v for Int4Array", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = Int4Array{Status: Present} *dst = Int4Array{Status: Present}
@@ -372,7 +372,7 @@ func (dst *Int4Array) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int4Array", src) return fmt.Errorf("cannot convert %v to Int4Array", src)
} }
*dst = Int4Array{ *dst = Int4Array{
@@ -403,7 +403,7 @@ func (dst *Int4Array) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to Int4Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to Int4Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -421,7 +421,7 @@ func (dst *Int4Array) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -434,10 +434,10 @@ func (dst *Int4Array) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to Int4Array") return 0, fmt.Errorf("cannot convert all values to Int4Array")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in Int4Array", err) return 0, fmt.Errorf("%v in Int4Array", err)
} }
index++ index++
@@ -625,7 +625,7 @@ func (src *Int4Array) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -640,7 +640,7 @@ func (src *Int4Array) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -648,7 +648,7 @@ func (src *Int4Array) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *Int4Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *Int4Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -664,7 +664,7 @@ func (src *Int4Array) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -682,14 +682,14 @@ func (src *Int4Array) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from Int4Array") return 0, fmt.Errorf("cannot assign all values from Int4Array")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from Int4Array") return 0, fmt.Errorf("cannot assign all values from Int4Array")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -848,7 +848,7 @@ func (src Int4Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("int4"); ok { if dt, ok := ci.DataTypeForName("int4"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "int4") return nil, fmt.Errorf("unable to find oid for type name %v", "int4")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -892,7 +892,7 @@ func (dst *Int4Array) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int4range struct { type Int4range struct {
@@ -30,7 +30,7 @@ func (dst *Int4range) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Int4range", src) return fmt.Errorf("cannot convert %v to Int4range", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Int4range) Get() interface{} {
} }
func (src *Int4range) AssignTo(dst interface{}) error { func (src *Int4range) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Int4range) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Int4range) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Int4range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Int4range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Int4range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Int4range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Int4range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Int4range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Int4range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Int4range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Int4range) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+10 -10
View File
@@ -4,11 +4,11 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int8 struct { type Int8 struct {
@@ -46,20 +46,20 @@ func (dst *Int8) Set(src interface{}) error {
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case uint64: case uint64:
if value > math.MaxInt64 { if value > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%d is greater than maximum value for Int8", value)
} }
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case int: case int:
if int64(value) < math.MinInt64 { if int64(value) < math.MinInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%d is greater than maximum value for Int8", value)
} }
if int64(value) > math.MaxInt64 { if int64(value) > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%d is greater than maximum value for Int8", value)
} }
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case uint: case uint:
if uint64(value) > math.MaxInt64 { if uint64(value) > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%d is greater than maximum value for Int8", value)
} }
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case string: case string:
@@ -70,12 +70,12 @@ func (dst *Int8) Set(src interface{}) error {
*dst = Int8{Int: num, Status: Present} *dst = Int8{Int: num, Status: Present}
case float32: case float32:
if value > math.MaxInt64 { if value > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%f is greater than maximum value for Int8", value)
} }
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case float64: case float64:
if value > math.MaxInt64 { if value > math.MaxInt64 {
return errors.Errorf("%d is greater than maximum value for Int8", value) return fmt.Errorf("%f is greater than maximum value for Int8", value)
} }
*dst = Int8{Int: int64(value), Status: Present} *dst = Int8{Int: int64(value), Status: Present}
case *int8: case *int8:
@@ -160,7 +160,7 @@ func (dst *Int8) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int8", value) return fmt.Errorf("cannot convert %v to Int8", value)
} }
return nil return nil
@@ -203,7 +203,7 @@ func (dst *Int8) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for int8: %v", len(src)) return fmt.Errorf("invalid length for int8: %v", len(src))
} }
n := int64(binary.BigEndian.Uint64(src)) n := int64(binary.BigEndian.Uint64(src))
@@ -253,7 +253,7 @@ func (dst *Int8) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int8Array struct { type Int8Array struct {
@@ -362,7 +362,7 @@ func (dst *Int8Array) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for Int8Array", src) return fmt.Errorf("cannot find dimensions of %v for Int8Array", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = Int8Array{Status: Present} *dst = Int8Array{Status: Present}
@@ -372,7 +372,7 @@ func (dst *Int8Array) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Int8Array", src) return fmt.Errorf("cannot convert %v to Int8Array", src)
} }
*dst = Int8Array{ *dst = Int8Array{
@@ -403,7 +403,7 @@ func (dst *Int8Array) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to Int8Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to Int8Array, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -421,7 +421,7 @@ func (dst *Int8Array) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -434,10 +434,10 @@ func (dst *Int8Array) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to Int8Array") return 0, fmt.Errorf("cannot convert all values to Int8Array")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in Int8Array", err) return 0, fmt.Errorf("%v in Int8Array", err)
} }
index++ index++
@@ -625,7 +625,7 @@ func (src *Int8Array) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -640,7 +640,7 @@ func (src *Int8Array) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -648,7 +648,7 @@ func (src *Int8Array) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *Int8Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *Int8Array) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -664,7 +664,7 @@ func (src *Int8Array) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -682,14 +682,14 @@ func (src *Int8Array) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from Int8Array") return 0, fmt.Errorf("cannot assign all values from Int8Array")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from Int8Array") return 0, fmt.Errorf("cannot assign all values from Int8Array")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -848,7 +848,7 @@ func (src Int8Array) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("int8"); ok { if dt, ok := ci.DataTypeForName("int8"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "int8") return nil, fmt.Errorf("unable to find oid for type name %v", "int8")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -892,7 +892,7 @@ func (dst *Int8Array) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Int8range struct { type Int8range struct {
@@ -30,7 +30,7 @@ func (dst *Int8range) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Int8range", src) return fmt.Errorf("cannot convert %v to Int8range", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Int8range) Get() interface{} {
} }
func (src *Int8range) AssignTo(dst interface{}) error { func (src *Int8range) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Int8range) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Int8range) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Int8range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Int8range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Int8range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Int8range) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Int8range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Int8range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Int8range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Int8range) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Int8range) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+11 -12
View File
@@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
const ( const (
@@ -47,7 +46,7 @@ func (dst *Interval) Set(src interface{}) error {
if originalSrc, ok := underlyingPtrType(src); ok { if originalSrc, ok := underlyingPtrType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Interval", value) return fmt.Errorf("cannot convert %v to Interval", value)
} }
return nil return nil
@@ -76,13 +75,13 @@ func (src *Interval) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Interval) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Interval) DecodeText(ci *ConnInfo, src []byte) error {
@@ -100,7 +99,7 @@ func (dst *Interval) DecodeText(ci *ConnInfo, src []byte) error {
for i := 0; i < len(parts)-1; i += 2 { for i := 0; i < len(parts)-1; i += 2 {
scalar, err := strconv.ParseInt(parts[i], 10, 64) scalar, err := strconv.ParseInt(parts[i], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("bad interval format") return fmt.Errorf("bad interval format")
} }
switch parts[i+1] { switch parts[i+1] {
@@ -116,7 +115,7 @@ func (dst *Interval) DecodeText(ci *ConnInfo, src []byte) error {
if len(parts)%2 == 1 { if len(parts)%2 == 1 {
timeParts := strings.SplitN(parts[len(parts)-1], ":", 3) timeParts := strings.SplitN(parts[len(parts)-1], ":", 3)
if len(timeParts) != 3 { if len(timeParts) != 3 {
return errors.Errorf("bad interval format") return fmt.Errorf("bad interval format")
} }
var negative bool var negative bool
@@ -127,26 +126,26 @@ func (dst *Interval) DecodeText(ci *ConnInfo, src []byte) error {
hours, err := strconv.ParseInt(timeParts[0], 10, 64) hours, err := strconv.ParseInt(timeParts[0], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("bad interval hour format: %s", timeParts[0]) return fmt.Errorf("bad interval hour format: %s", timeParts[0])
} }
minutes, err := strconv.ParseInt(timeParts[1], 10, 64) minutes, err := strconv.ParseInt(timeParts[1], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("bad interval minute format: %s", timeParts[1]) return fmt.Errorf("bad interval minute format: %s", timeParts[1])
} }
secondParts := strings.SplitN(timeParts[2], ".", 2) secondParts := strings.SplitN(timeParts[2], ".", 2)
seconds, err := strconv.ParseInt(secondParts[0], 10, 64) seconds, err := strconv.ParseInt(secondParts[0], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("bad interval second format: %s", secondParts[0]) return fmt.Errorf("bad interval second format: %s", secondParts[0])
} }
var uSeconds int64 var uSeconds int64
if len(secondParts) == 2 { if len(secondParts) == 2 {
uSeconds, err = strconv.ParseInt(secondParts[1], 10, 64) uSeconds, err = strconv.ParseInt(secondParts[1], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("bad interval decimal format: %s", secondParts[1]) return fmt.Errorf("bad interval decimal format: %s", secondParts[1])
} }
for i := 0; i < 6-len(secondParts[1]); i++ { for i := 0; i < 6-len(secondParts[1]); i++ {
@@ -175,7 +174,7 @@ func (dst *Interval) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 16 { if len(src) != 16 {
return errors.Errorf("Received an invalid size for a interval: %d", len(src)) return fmt.Errorf("Received an invalid size for a interval: %d", len(src))
} }
microseconds := int64(binary.BigEndian.Uint64(src)) microseconds := int64(binary.BigEndian.Uint64(src))
@@ -249,7 +248,7 @@ func (dst *Interval) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+4 -4
View File
@@ -3,8 +3,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/json" "encoding/json"
"errors"
errors "golang.org/x/xerrors" "fmt"
) )
type JSON struct { type JSON struct {
@@ -82,7 +82,7 @@ func (src *JSON) AssignTo(dst interface{}) error {
if src.Status == Present { if src.Status == Present {
*v = string(src.Bytes) *v = string(src.Bytes)
} else { } else {
return errors.Errorf("cannot assign non-present status to %T", dst) return fmt.Errorf("cannot assign non-present status to %T", dst)
} }
case **string: case **string:
if src.Status == Present { if src.Status == Present {
@@ -166,7 +166,7 @@ func (dst *JSON) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+3 -4
View File
@@ -2,8 +2,7 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
errors "golang.org/x/xerrors"
) )
type JSONB JSON type JSONB JSON
@@ -35,11 +34,11 @@ func (dst *JSONB) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) == 0 { if len(src) == 0 {
return errors.Errorf("jsonb too short") return fmt.Errorf("jsonb too short")
} }
if src[0] != 1 { if src[0] != 1 {
return errors.Errorf("unknown jsonb version number %d", src[0]) return fmt.Errorf("unknown jsonb version number %d", src[0])
} }
*dst = JSONB{Bytes: src[1:], Status: Present} *dst = JSONB{Bytes: src[1:], Status: Present}
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type JSONBArray struct { type JSONBArray struct {
@@ -96,7 +96,7 @@ func (dst *JSONBArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for JSONBArray", src) return fmt.Errorf("cannot find dimensions of %v for JSONBArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = JSONBArray{Status: Present} *dst = JSONBArray{Status: Present}
@@ -106,7 +106,7 @@ func (dst *JSONBArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to JSONBArray", src) return fmt.Errorf("cannot convert %v to JSONBArray", src)
} }
*dst = JSONBArray{ *dst = JSONBArray{
@@ -137,7 +137,7 @@ func (dst *JSONBArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to JSONBArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to JSONBArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *JSONBArray) setRecursive(value reflect.Value, index, dimension int) (
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *JSONBArray) setRecursive(value reflect.Value, index, dimension int) (
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to JSONBArray") return 0, fmt.Errorf("cannot convert all values to JSONBArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in JSONBArray", err) return 0, fmt.Errorf("%v in JSONBArray", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *JSONBArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *JSONBArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *JSONBArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *JSONBArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *JSONBArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *JSONBArray) assignToRecursive(value reflect.Value, index, dimension i
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *JSONBArray) assignToRecursive(value reflect.Value, index, dimension i
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from JSONBArray") return 0, fmt.Errorf("cannot assign all values from JSONBArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from JSONBArray") return 0, fmt.Errorf("cannot assign all values from JSONBArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src JSONBArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("jsonb"); ok { if dt, ok := ci.DataTypeForName("jsonb"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "jsonb") return nil, fmt.Errorf("unable to find oid for type name %v", "jsonb")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *JSONBArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+6 -7
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Line struct { type Line struct {
@@ -18,7 +17,7 @@ type Line struct {
} }
func (dst *Line) Set(src interface{}) error { func (dst *Line) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Line", src) return fmt.Errorf("cannot convert %v to Line", src)
} }
func (dst Line) Get() interface{} { func (dst Line) Get() interface{} {
@@ -33,7 +32,7 @@ func (dst Line) Get() interface{} {
} }
func (src *Line) AssignTo(dst interface{}) error { func (src *Line) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Line) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Line) DecodeText(ci *ConnInfo, src []byte) error {
@@ -43,12 +42,12 @@ func (dst *Line) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 7 { if len(src) < 7 {
return errors.Errorf("invalid length for Line: %v", len(src)) return fmt.Errorf("invalid length for Line: %v", len(src))
} }
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 3) parts := strings.SplitN(string(src[1:len(src)-1]), ",", 3)
if len(parts) < 3 { if len(parts) < 3 {
return errors.Errorf("invalid format for line") return fmt.Errorf("invalid format for line")
} }
a, err := strconv.ParseFloat(parts[0], 64) a, err := strconv.ParseFloat(parts[0], 64)
@@ -77,7 +76,7 @@ func (dst *Line) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 24 { if len(src) != 24 {
return errors.Errorf("invalid length for Line: %v", len(src)) return fmt.Errorf("invalid length for Line: %v", len(src))
} }
a := binary.BigEndian.Uint64(src) a := binary.BigEndian.Uint64(src)
@@ -140,7 +139,7 @@ func (dst *Line) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+5 -6
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Lseg struct { type Lseg struct {
@@ -18,7 +17,7 @@ type Lseg struct {
} }
func (dst *Lseg) Set(src interface{}) error { func (dst *Lseg) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Lseg", src) return fmt.Errorf("cannot convert %v to Lseg", src)
} }
func (dst Lseg) Get() interface{} { func (dst Lseg) Get() interface{} {
@@ -33,7 +32,7 @@ func (dst Lseg) Get() interface{} {
} }
func (src *Lseg) AssignTo(dst interface{}) error { func (src *Lseg) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Lseg) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Lseg) DecodeText(ci *ConnInfo, src []byte) error {
@@ -43,7 +42,7 @@ func (dst *Lseg) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 11 { if len(src) < 11 {
return errors.Errorf("invalid length for Lseg: %v", len(src)) return fmt.Errorf("invalid length for Lseg: %v", len(src))
} }
str := string(src[2:]) str := string(src[2:])
@@ -90,7 +89,7 @@ func (dst *Lseg) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 32 { if len(src) != 32 {
return errors.Errorf("invalid length for Lseg: %v", len(src)) return fmt.Errorf("invalid length for Lseg: %v", len(src))
} }
x1 := binary.BigEndian.Uint64(src) x1 := binary.BigEndian.Uint64(src)
@@ -157,7 +156,7 @@ func (dst *Lseg) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+6 -7
View File
@@ -2,9 +2,8 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"net" "net"
errors "golang.org/x/xerrors"
) )
type Macaddr struct { type Macaddr struct {
@@ -52,7 +51,7 @@ func (dst *Macaddr) Set(src interface{}) error {
if originalSrc, ok := underlyingPtrType(src); ok { if originalSrc, ok := underlyingPtrType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Macaddr", value) return fmt.Errorf("cannot convert %v to Macaddr", value)
} }
return nil return nil
@@ -84,13 +83,13 @@ func (src *Macaddr) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Macaddr) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Macaddr) DecodeText(ci *ConnInfo, src []byte) error {
@@ -115,7 +114,7 @@ func (dst *Macaddr) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 6 { if len(src) != 6 {
return errors.Errorf("Received an invalid size for a macaddr: %d", len(src)) return fmt.Errorf("Received an invalid size for a macaddr: %d", len(src))
} }
addr := make(net.HardwareAddr, 6) addr := make(net.HardwareAddr, 6)
@@ -165,7 +164,7 @@ func (dst *Macaddr) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"net" "net"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type MacaddrArray struct { type MacaddrArray struct {
@@ -97,7 +97,7 @@ func (dst *MacaddrArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for MacaddrArray", src) return fmt.Errorf("cannot find dimensions of %v for MacaddrArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = MacaddrArray{Status: Present} *dst = MacaddrArray{Status: Present}
@@ -107,7 +107,7 @@ func (dst *MacaddrArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to MacaddrArray", src) return fmt.Errorf("cannot convert %v to MacaddrArray", src)
} }
*dst = MacaddrArray{ *dst = MacaddrArray{
@@ -138,7 +138,7 @@ func (dst *MacaddrArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to MacaddrArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to MacaddrArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -156,7 +156,7 @@ func (dst *MacaddrArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -169,10 +169,10 @@ func (dst *MacaddrArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to MacaddrArray") return 0, fmt.Errorf("cannot convert all values to MacaddrArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in MacaddrArray", err) return 0, fmt.Errorf("%v in MacaddrArray", err)
} }
index++ index++
@@ -234,7 +234,7 @@ func (src *MacaddrArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -249,7 +249,7 @@ func (src *MacaddrArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -257,7 +257,7 @@ func (src *MacaddrArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *MacaddrArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *MacaddrArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -273,7 +273,7 @@ func (src *MacaddrArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -291,14 +291,14 @@ func (src *MacaddrArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from MacaddrArray") return 0, fmt.Errorf("cannot assign all values from MacaddrArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from MacaddrArray") return 0, fmt.Errorf("cannot assign all values from MacaddrArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -457,7 +457,7 @@ func (src MacaddrArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("macaddr"); ok { if dt, ok := ci.DataTypeForName("macaddr"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "macaddr") return nil, fmt.Errorf("unable to find oid for type name %v", "macaddr")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -501,7 +501,7 @@ func (dst *MacaddrArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+29 -29
View File
@@ -3,13 +3,13 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"math" "math"
"math/big" "math/big"
"strconv" "strconv"
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// PostgreSQL internal numeric storage uses 16-bit "digits" with base of 10,000 // PostgreSQL internal numeric storage uses 16-bit "digits" with base of 10,000
@@ -197,7 +197,7 @@ func (dst *Numeric) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Numeric", value) return fmt.Errorf("cannot convert %v to Numeric", value)
} }
return nil return nil
@@ -236,10 +236,10 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(bigMaxInt) > 0 { if normalizedInt.Cmp(bigMaxInt) > 0 {
return errors.Errorf("%v is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%v is greater than maximum value for %T", normalizedInt, *v)
} }
if normalizedInt.Cmp(bigMinInt) < 0 { if normalizedInt.Cmp(bigMinInt) < 0 {
return errors.Errorf("%v is less than minimum value for %T", normalizedInt, *v) return fmt.Errorf("%v is less than minimum value for %T", normalizedInt, *v)
} }
*v = int(normalizedInt.Int64()) *v = int(normalizedInt.Int64())
case *int8: case *int8:
@@ -248,10 +248,10 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(bigMaxInt8) > 0 { if normalizedInt.Cmp(bigMaxInt8) > 0 {
return errors.Errorf("%v is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%v is greater than maximum value for %T", normalizedInt, *v)
} }
if normalizedInt.Cmp(bigMinInt8) < 0 { if normalizedInt.Cmp(bigMinInt8) < 0 {
return errors.Errorf("%v is less than minimum value for %T", normalizedInt, *v) return fmt.Errorf("%v is less than minimum value for %T", normalizedInt, *v)
} }
*v = int8(normalizedInt.Int64()) *v = int8(normalizedInt.Int64())
case *int16: case *int16:
@@ -260,10 +260,10 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(bigMaxInt16) > 0 { if normalizedInt.Cmp(bigMaxInt16) > 0 {
return errors.Errorf("%v is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%v is greater than maximum value for %T", normalizedInt, *v)
} }
if normalizedInt.Cmp(bigMinInt16) < 0 { if normalizedInt.Cmp(bigMinInt16) < 0 {
return errors.Errorf("%v is less than minimum value for %T", normalizedInt, *v) return fmt.Errorf("%v is less than minimum value for %T", normalizedInt, *v)
} }
*v = int16(normalizedInt.Int64()) *v = int16(normalizedInt.Int64())
case *int32: case *int32:
@@ -272,10 +272,10 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(bigMaxInt32) > 0 { if normalizedInt.Cmp(bigMaxInt32) > 0 {
return errors.Errorf("%v is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%v is greater than maximum value for %T", normalizedInt, *v)
} }
if normalizedInt.Cmp(bigMinInt32) < 0 { if normalizedInt.Cmp(bigMinInt32) < 0 {
return errors.Errorf("%v is less than minimum value for %T", normalizedInt, *v) return fmt.Errorf("%v is less than minimum value for %T", normalizedInt, *v)
} }
*v = int32(normalizedInt.Int64()) *v = int32(normalizedInt.Int64())
case *int64: case *int64:
@@ -284,10 +284,10 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(bigMaxInt64) > 0 { if normalizedInt.Cmp(bigMaxInt64) > 0 {
return errors.Errorf("%v is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%v is greater than maximum value for %T", normalizedInt, *v)
} }
if normalizedInt.Cmp(bigMinInt64) < 0 { if normalizedInt.Cmp(bigMinInt64) < 0 {
return errors.Errorf("%v is less than minimum value for %T", normalizedInt, *v) return fmt.Errorf("%v is less than minimum value for %T", normalizedInt, *v)
} }
*v = normalizedInt.Int64() *v = normalizedInt.Int64()
case *uint: case *uint:
@@ -296,9 +296,9 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(big0) < 0 { if normalizedInt.Cmp(big0) < 0 {
return errors.Errorf("%d is less than zero for %T", normalizedInt, *v) return fmt.Errorf("%d is less than zero for %T", normalizedInt, *v)
} else if normalizedInt.Cmp(bigMaxUint) > 0 { } else if normalizedInt.Cmp(bigMaxUint) > 0 {
return errors.Errorf("%d is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%d is greater than maximum value for %T", normalizedInt, *v)
} }
*v = uint(normalizedInt.Uint64()) *v = uint(normalizedInt.Uint64())
case *uint8: case *uint8:
@@ -307,9 +307,9 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(big0) < 0 { if normalizedInt.Cmp(big0) < 0 {
return errors.Errorf("%d is less than zero for %T", normalizedInt, *v) return fmt.Errorf("%d is less than zero for %T", normalizedInt, *v)
} else if normalizedInt.Cmp(bigMaxUint8) > 0 { } else if normalizedInt.Cmp(bigMaxUint8) > 0 {
return errors.Errorf("%d is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%d is greater than maximum value for %T", normalizedInt, *v)
} }
*v = uint8(normalizedInt.Uint64()) *v = uint8(normalizedInt.Uint64())
case *uint16: case *uint16:
@@ -318,9 +318,9 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(big0) < 0 { if normalizedInt.Cmp(big0) < 0 {
return errors.Errorf("%d is less than zero for %T", normalizedInt, *v) return fmt.Errorf("%d is less than zero for %T", normalizedInt, *v)
} else if normalizedInt.Cmp(bigMaxUint16) > 0 { } else if normalizedInt.Cmp(bigMaxUint16) > 0 {
return errors.Errorf("%d is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%d is greater than maximum value for %T", normalizedInt, *v)
} }
*v = uint16(normalizedInt.Uint64()) *v = uint16(normalizedInt.Uint64())
case *uint32: case *uint32:
@@ -329,9 +329,9 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(big0) < 0 { if normalizedInt.Cmp(big0) < 0 {
return errors.Errorf("%d is less than zero for %T", normalizedInt, *v) return fmt.Errorf("%d is less than zero for %T", normalizedInt, *v)
} else if normalizedInt.Cmp(bigMaxUint32) > 0 { } else if normalizedInt.Cmp(bigMaxUint32) > 0 {
return errors.Errorf("%d is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%d is greater than maximum value for %T", normalizedInt, *v)
} }
*v = uint32(normalizedInt.Uint64()) *v = uint32(normalizedInt.Uint64())
case *uint64: case *uint64:
@@ -340,16 +340,16 @@ func (src *Numeric) AssignTo(dst interface{}) error {
return err return err
} }
if normalizedInt.Cmp(big0) < 0 { if normalizedInt.Cmp(big0) < 0 {
return errors.Errorf("%d is less than zero for %T", normalizedInt, *v) return fmt.Errorf("%d is less than zero for %T", normalizedInt, *v)
} else if normalizedInt.Cmp(bigMaxUint64) > 0 { } else if normalizedInt.Cmp(bigMaxUint64) > 0 {
return errors.Errorf("%d is greater than maximum value for %T", normalizedInt, *v) return fmt.Errorf("%d is greater than maximum value for %T", normalizedInt, *v)
} }
*v = normalizedInt.Uint64() *v = normalizedInt.Uint64()
default: default:
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
@@ -377,7 +377,7 @@ func (dst *Numeric) toBigInt() (*big.Int, error) {
remainder := &big.Int{} remainder := &big.Int{}
num.DivMod(num, div, remainder) num.DivMod(num, div, remainder)
if remainder.Cmp(big0) != 0 { if remainder.Cmp(big0) != 0 {
return nil, errors.Errorf("cannot convert %v to integer", dst) return nil, fmt.Errorf("cannot convert %v to integer", dst)
} }
return num, nil return num, nil
} }
@@ -435,7 +435,7 @@ func parseNumericString(str string) (n *big.Int, exp int32, err error) {
accum := &big.Int{} accum := &big.Int{}
if _, ok := accum.SetString(digits, 10); !ok { if _, ok := accum.SetString(digits, 10); !ok {
return nil, 0, errors.Errorf("%s is not a number", str) return nil, 0, fmt.Errorf("%s is not a number", str)
} }
return accum, exp, nil return accum, exp, nil
@@ -448,7 +448,7 @@ func (dst *Numeric) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) < 8 { if len(src) < 8 {
return errors.Errorf("numeric incomplete %v", src) return fmt.Errorf("numeric incomplete %v", src)
} }
rp := 0 rp := 0
@@ -472,7 +472,7 @@ func (dst *Numeric) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src[rp:]) < int(ndigits)*2 { if len(src[rp:]) < int(ndigits)*2 {
return errors.Errorf("numeric incomplete %v", src) return fmt.Errorf("numeric incomplete %v", src)
} }
accum := &big.Int{} accum := &big.Int{}
@@ -493,7 +493,7 @@ func (dst *Numeric) DecodeBinary(ci *ConnInfo, src []byte) error {
case 4: case 4:
mul = bigNBaseX4 mul = bigNBaseX4
default: default:
return errors.Errorf("invalid digitsRead: %d (this can't happen)", digitsRead) return fmt.Errorf("invalid digitsRead: %d (this can't happen)", digitsRead)
} }
accum.Mul(accum, mul) accum.Mul(accum, mul)
} }
@@ -695,7 +695,7 @@ func (dst *Numeric) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type NumericArray struct { type NumericArray struct {
@@ -210,7 +210,7 @@ func (dst *NumericArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for NumericArray", src) return fmt.Errorf("cannot find dimensions of %v for NumericArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = NumericArray{Status: Present} *dst = NumericArray{Status: Present}
@@ -220,7 +220,7 @@ func (dst *NumericArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to NumericArray", src) return fmt.Errorf("cannot convert %v to NumericArray", src)
} }
*dst = NumericArray{ *dst = NumericArray{
@@ -251,7 +251,7 @@ func (dst *NumericArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to NumericArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to NumericArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -269,7 +269,7 @@ func (dst *NumericArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -282,10 +282,10 @@ func (dst *NumericArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to NumericArray") return 0, fmt.Errorf("cannot convert all values to NumericArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in NumericArray", err) return 0, fmt.Errorf("%v in NumericArray", err)
} }
index++ index++
@@ -401,7 +401,7 @@ func (src *NumericArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -416,7 +416,7 @@ func (src *NumericArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -424,7 +424,7 @@ func (src *NumericArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *NumericArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *NumericArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -440,7 +440,7 @@ func (src *NumericArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -458,14 +458,14 @@ func (src *NumericArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from NumericArray") return 0, fmt.Errorf("cannot assign all values from NumericArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from NumericArray") return 0, fmt.Errorf("cannot assign all values from NumericArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -624,7 +624,7 @@ func (src NumericArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("numeric"); ok { if dt, ok := ci.DataTypeForName("numeric"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "numeric") return nil, fmt.Errorf("unable to find oid for type name %v", "numeric")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -668,7 +668,7 @@ func (dst *NumericArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Numrange struct { type Numrange struct {
@@ -30,7 +30,7 @@ func (dst *Numrange) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Numrange", src) return fmt.Errorf("cannot convert %v to Numrange", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Numrange) Get() interface{} {
} }
func (src *Numrange) AssignTo(dst interface{}) error { func (src *Numrange) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Numrange) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Numrange) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Numrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Numrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Numrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Numrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Numrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Numrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Numrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Numrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Numrange) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+6 -6
View File
@@ -3,10 +3,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// OID (Object Identifier Type) is, according to // OID (Object Identifier Type) is, according to
@@ -20,7 +20,7 @@ type OID uint32
func (dst *OID) DecodeText(ci *ConnInfo, src []byte) error { func (dst *OID) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil { if src == nil {
return errors.Errorf("cannot decode nil into OID") return fmt.Errorf("cannot decode nil into OID")
} }
n, err := strconv.ParseUint(string(src), 10, 32) n, err := strconv.ParseUint(string(src), 10, 32)
@@ -34,11 +34,11 @@ func (dst *OID) DecodeText(ci *ConnInfo, src []byte) error {
func (dst *OID) DecodeBinary(ci *ConnInfo, src []byte) error { func (dst *OID) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil { if src == nil {
return errors.Errorf("cannot decode nil into OID") return fmt.Errorf("cannot decode nil into OID")
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length: %v", len(src)) return fmt.Errorf("invalid length: %v", len(src))
} }
n := binary.BigEndian.Uint32(src) n := binary.BigEndian.Uint32(src)
@@ -57,7 +57,7 @@ func (src OID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
// Scan implements the database/sql Scanner interface. // Scan implements the database/sql Scanner interface.
func (dst *OID) Scan(src interface{}) error { func (dst *OID) Scan(src interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan NULL into %T", src) return fmt.Errorf("cannot scan NULL into %T", src)
} }
switch src := src.(type) { switch src := src.(type) {
@@ -72,7 +72,7 @@ func (dst *OID) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+6 -7
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Path struct { type Path struct {
@@ -19,7 +18,7 @@ type Path struct {
} }
func (dst *Path) Set(src interface{}) error { func (dst *Path) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Path", src) return fmt.Errorf("cannot convert %v to Path", src)
} }
func (dst Path) Get() interface{} { func (dst Path) Get() interface{} {
@@ -34,7 +33,7 @@ func (dst Path) Get() interface{} {
} }
func (src *Path) AssignTo(dst interface{}) error { func (src *Path) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Path) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Path) DecodeText(ci *ConnInfo, src []byte) error {
@@ -44,7 +43,7 @@ func (dst *Path) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 7 { if len(src) < 7 {
return errors.Errorf("invalid length for Path: %v", len(src)) return fmt.Errorf("invalid length for Path: %v", len(src))
} }
closed := src[0] == '(' closed := src[0] == '('
@@ -87,7 +86,7 @@ func (dst *Path) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) < 5 { if len(src) < 5 {
return errors.Errorf("invalid length for Path: %v", len(src)) return fmt.Errorf("invalid length for Path: %v", len(src))
} }
closed := src[0] == 1 closed := src[0] == 1
@@ -96,7 +95,7 @@ func (dst *Path) DecodeBinary(ci *ConnInfo, src []byte) error {
rp := 5 rp := 5
if 5+pointCount*16 != len(src) { if 5+pointCount*16 != len(src) {
return errors.Errorf("invalid length for Path with %d points: %v", pointCount, len(src)) return fmt.Errorf("invalid length for Path with %d points: %v", pointCount, len(src))
} }
points := make([]Vec2, pointCount) points := make([]Vec2, pointCount)
@@ -187,7 +186,7 @@ func (dst *Path) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+14 -15
View File
@@ -3,13 +3,12 @@ package pgtype
import ( import (
"database/sql" "database/sql"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"math" "math"
"net" "net"
"reflect" "reflect"
"time" "time"
errors "golang.org/x/xerrors"
) )
// PostgreSQL oids for common types // PostgreSQL oids for common types
@@ -625,11 +624,11 @@ type scanPlanBinaryInt16 struct{}
func (scanPlanBinaryInt16) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanBinaryInt16) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if len(src) != 2 { if len(src) != 2 {
return errors.Errorf("invalid length for int2: %v", len(src)) return fmt.Errorf("invalid length for int2: %v", len(src))
} }
if p, ok := (dst).(*int16); ok { if p, ok := (dst).(*int16); ok {
@@ -645,11 +644,11 @@ type scanPlanBinaryInt32 struct{}
func (scanPlanBinaryInt32) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanBinaryInt32) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length for int4: %v", len(src)) return fmt.Errorf("invalid length for int4: %v", len(src))
} }
if p, ok := (dst).(*int32); ok { if p, ok := (dst).(*int32); ok {
@@ -665,11 +664,11 @@ type scanPlanBinaryInt64 struct{}
func (scanPlanBinaryInt64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanBinaryInt64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for int8: %v", len(src)) return fmt.Errorf("invalid length for int8: %v", len(src))
} }
if p, ok := (dst).(*int64); ok { if p, ok := (dst).(*int64); ok {
@@ -685,11 +684,11 @@ type scanPlanBinaryFloat32 struct{}
func (scanPlanBinaryFloat32) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanBinaryFloat32) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length for int4: %v", len(src)) return fmt.Errorf("invalid length for int4: %v", len(src))
} }
if p, ok := (dst).(*float32); ok { if p, ok := (dst).(*float32); ok {
@@ -706,11 +705,11 @@ type scanPlanBinaryFloat64 struct{}
func (scanPlanBinaryFloat64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanBinaryFloat64) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for int8: %v", len(src)) return fmt.Errorf("invalid length for int8: %v", len(src))
} }
if p, ok := (dst).(*float64); ok { if p, ok := (dst).(*float64); ok {
@@ -739,7 +738,7 @@ type scanPlanString struct{}
func (scanPlanString) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error { func (scanPlanString) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
if src == nil { if src == nil {
return errors.Errorf("cannot scan null into %T", dst) return fmt.Errorf("cannot scan null into %T", dst)
} }
if p, ok := (dst).(*string); ok { if p, ok := (dst).(*string); ok {
@@ -841,7 +840,7 @@ func scanUnknownType(oid uint32, formatCode int16, buf []byte, dest interface{})
switch dest := dest.(type) { switch dest := dest.(type) {
case *string: case *string:
if formatCode == BinaryFormatCode { if formatCode == BinaryFormatCode {
return errors.Errorf("unknown oid %d in binary format cannot be scanned into %T", oid, dest) return fmt.Errorf("unknown oid %d in binary format cannot be scanned into %T", oid, dest)
} }
*dest = string(buf) *dest = string(buf)
return nil return nil
@@ -852,7 +851,7 @@ func scanUnknownType(oid uint32, formatCode int16, buf []byte, dest interface{})
if nextDst, retry := GetAssignToDstType(dest); retry { if nextDst, retry := GetAssignToDstType(dest); retry {
return scanUnknownType(oid, formatCode, buf, nextDst) return scanUnknownType(oid, formatCode, buf, nextDst)
} }
return errors.Errorf("unknown oid %d cannot be scanned into %T", oid, dest) return fmt.Errorf("unknown oid %d cannot be scanned into %T", oid, dest)
} }
} }
+1 -1
View File
@@ -2,6 +2,7 @@ package pgtype_test
import ( import (
"bytes" "bytes"
"errors"
"net" "net"
"testing" "testing"
@@ -11,7 +12,6 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
errors "golang.org/x/xerrors"
) )
// Test for renamed types // Test for renamed types
+7 -7
View File
@@ -3,11 +3,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"math" "math"
"strconv" "strconv"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// pguint32 is the core type that is used to implement PostgreSQL types such as // pguint32 is the core type that is used to implement PostgreSQL types such as
@@ -24,16 +24,16 @@ func (dst *pguint32) Set(src interface{}) error {
switch value := src.(type) { switch value := src.(type) {
case int64: case int64:
if value < 0 { if value < 0 {
return errors.Errorf("%d is less than minimum value for pguint32", value) return fmt.Errorf("%d is less than minimum value for pguint32", value)
} }
if value > math.MaxUint32 { if value > math.MaxUint32 {
return errors.Errorf("%d is greater than maximum value for pguint32", value) return fmt.Errorf("%d is greater than maximum value for pguint32", value)
} }
*dst = pguint32{Uint: uint32(value), Status: Present} *dst = pguint32{Uint: uint32(value), Status: Present}
case uint32: case uint32:
*dst = pguint32{Uint: value, Status: Present} *dst = pguint32{Uint: value, Status: Present}
default: default:
return errors.Errorf("cannot convert %v to pguint32", value) return fmt.Errorf("cannot convert %v to pguint32", value)
} }
return nil return nil
@@ -58,7 +58,7 @@ func (src *pguint32) AssignTo(dst interface{}) error {
if src.Status == Present { if src.Status == Present {
*v = src.Uint *v = src.Uint
} else { } else {
return errors.Errorf("cannot assign %v into %T", src, dst) return fmt.Errorf("cannot assign %v into %T", src, dst)
} }
case **uint32: case **uint32:
if src.Status == Present { if src.Status == Present {
@@ -94,7 +94,7 @@ func (dst *pguint32) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 4 { if len(src) != 4 {
return errors.Errorf("invalid length: %v", len(src)) return fmt.Errorf("invalid length: %v", len(src))
} }
n := binary.BigEndian.Uint32(src) n := binary.BigEndian.Uint32(src)
@@ -146,7 +146,7 @@ func (dst *pguint32) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+8 -9
View File
@@ -10,7 +10,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Vec2 struct { type Vec2 struct {
@@ -28,7 +27,7 @@ func (dst *Point) Set(src interface{}) error {
dst.Status = Null dst.Status = Null
return nil return nil
} }
err := errors.Errorf("cannot convert %v to Point", src) err := fmt.Errorf("cannot convert %v to Point", src)
var p *Point var p *Point
switch value := src.(type) { switch value := src.(type) {
case string: case string:
@@ -51,14 +50,14 @@ func parsePoint(src []byte) (*Point, error) {
} }
if len(src) < 5 { if len(src) < 5 {
return nil, errors.Errorf("invalid length for point: %v", len(src)) return nil, fmt.Errorf("invalid length for point: %v", len(src))
} }
if src[0] == '"' && src[len(src)-1] == '"' { if src[0] == '"' && src[len(src)-1] == '"' {
src = src[1 : len(src)-1] src = src[1 : len(src)-1]
} }
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
if len(parts) < 2 { if len(parts) < 2 {
return nil, errors.Errorf("invalid format for point") return nil, fmt.Errorf("invalid format for point")
} }
x, err := strconv.ParseFloat(parts[0], 64) x, err := strconv.ParseFloat(parts[0], 64)
@@ -86,7 +85,7 @@ func (dst Point) Get() interface{} {
} }
func (src *Point) AssignTo(dst interface{}) error { func (src *Point) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Point) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Point) DecodeText(ci *ConnInfo, src []byte) error {
@@ -96,12 +95,12 @@ func (dst *Point) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 5 { if len(src) < 5 {
return errors.Errorf("invalid length for point: %v", len(src)) return fmt.Errorf("invalid length for point: %v", len(src))
} }
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
if len(parts) < 2 { if len(parts) < 2 {
return errors.Errorf("invalid format for point") return fmt.Errorf("invalid format for point")
} }
x, err := strconv.ParseFloat(parts[0], 64) x, err := strconv.ParseFloat(parts[0], 64)
@@ -125,7 +124,7 @@ func (dst *Point) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 16 { if len(src) != 16 {
return errors.Errorf("invalid length for point: %v", len(src)) return fmt.Errorf("invalid length for point: %v", len(src))
} }
x := binary.BigEndian.Uint64(src) x := binary.BigEndian.Uint64(src)
@@ -181,7 +180,7 @@ func (dst *Point) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+7 -8
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Polygon struct { type Polygon struct {
@@ -28,7 +27,7 @@ func (dst *Polygon) Set(src interface{}) error {
dst.Status = Null dst.Status = Null
return nil return nil
} }
err := errors.Errorf("cannot convert %v to Polygon", src) err := fmt.Errorf("cannot convert %v to Polygon", src)
var p *Polygon var p *Polygon
switch value := src.(type) { switch value := src.(type) {
case string: case string:
@@ -61,7 +60,7 @@ func float64ToPolygon(src []float64) (*Polygon, error) {
} }
if len(src)%2 != 0 { if len(src)%2 != 0 {
p.Status = Undefined p.Status = Undefined
return p, errors.Errorf("invalid length for polygon: %v", len(src)) return p, fmt.Errorf("invalid length for polygon: %v", len(src))
} }
p.Status = Present p.Status = Present
p.P = make([]Vec2, 0) p.P = make([]Vec2, 0)
@@ -83,7 +82,7 @@ func (dst Polygon) Get() interface{} {
} }
func (src *Polygon) AssignTo(dst interface{}) error { func (src *Polygon) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Polygon) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Polygon) DecodeText(ci *ConnInfo, src []byte) error {
@@ -93,7 +92,7 @@ func (dst *Polygon) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 7 { if len(src) < 7 {
return errors.Errorf("invalid length for Polygon: %v", len(src)) return fmt.Errorf("invalid length for Polygon: %v", len(src))
} }
points := make([]Vec2, 0) points := make([]Vec2, 0)
@@ -135,14 +134,14 @@ func (dst *Polygon) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) < 5 { if len(src) < 5 {
return errors.Errorf("invalid length for Polygon: %v", len(src)) return fmt.Errorf("invalid length for Polygon: %v", len(src))
} }
pointCount := int(binary.BigEndian.Uint32(src)) pointCount := int(binary.BigEndian.Uint32(src))
rp := 4 rp := 4
if 4+pointCount*16 != len(src) { if 4+pointCount*16 != len(src) {
return errors.Errorf("invalid length for Polygon with %d points: %v", pointCount, len(src)) return fmt.Errorf("invalid length for Polygon with %d points: %v", pointCount, len(src))
} }
points := make([]Vec2, pointCount) points := make([]Vec2, pointCount)
@@ -218,7 +217,7 @@ func (dst *Polygon) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -17
View File
@@ -1,10 +1,9 @@
package pgtype package pgtype
import ( import (
"fmt"
"math" "math"
"strconv" "strconv"
errors "golang.org/x/xerrors"
) )
// QChar is for PostgreSQL's special 8-bit-only "char" type more akin to the C // QChar is for PostgreSQL's special 8-bit-only "char" type more akin to the C
@@ -41,59 +40,59 @@ func (dst *QChar) Set(src interface{}) error {
*dst = QChar{Int: value, Status: Present} *dst = QChar{Int: value, Status: Present}
case uint8: case uint8:
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case int16: case int16:
if value < math.MinInt8 { if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case uint16: case uint16:
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case int32: case int32:
if value < math.MinInt8 { if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case uint32: case uint32:
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case int64: case int64:
if value < math.MinInt8 { if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case uint64: case uint64:
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case int: case int:
if value < math.MinInt8 { if value < math.MinInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case uint: case uint:
if value > math.MaxInt8 { if value > math.MaxInt8 {
return errors.Errorf("%d is greater than maximum value for QChar", value) return fmt.Errorf("%d is greater than maximum value for QChar", value)
} }
*dst = QChar{Int: int8(value), Status: Present} *dst = QChar{Int: int8(value), Status: Present}
case string: case string:
@@ -106,7 +105,7 @@ func (dst *QChar) Set(src interface{}) error {
if originalSrc, ok := underlyingNumberType(src); ok { if originalSrc, ok := underlyingNumberType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to QChar", value) return fmt.Errorf("cannot convert %v to QChar", value)
} }
return nil return nil
@@ -134,7 +133,7 @@ func (dst *QChar) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 1 { if len(src) != 1 {
return errors.Errorf(`invalid length for "char": %v`, len(src)) return fmt.Errorf(`invalid length for "char": %v`, len(src))
} }
*dst = QChar{Int: int8(src[0]), Status: Present} *dst = QChar{Int: int8(src[0]), Status: Present}
+19 -20
View File
@@ -3,8 +3,7 @@ package pgtype
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
errors "golang.org/x/xerrors"
) )
type BoundType byte type BoundType byte
@@ -41,7 +40,7 @@ func ParseUntypedTextRange(src string) (*UntypedTextRange, error) {
r, _, err := buf.ReadRune() r, _, err := buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid lower bound: %v", err) return nil, fmt.Errorf("invalid lower bound: %v", err)
} }
switch r { switch r {
case '(': case '(':
@@ -49,12 +48,12 @@ func ParseUntypedTextRange(src string) (*UntypedTextRange, error) {
case '[': case '[':
utr.LowerType = Inclusive utr.LowerType = Inclusive
default: default:
return nil, errors.Errorf("missing lower bound, instead got: %v", string(r)) return nil, fmt.Errorf("missing lower bound, instead got: %v", string(r))
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid lower value: %v", err) return nil, fmt.Errorf("invalid lower value: %v", err)
} }
buf.UnreadRune() buf.UnreadRune()
@@ -63,21 +62,21 @@ func ParseUntypedTextRange(src string) (*UntypedTextRange, error) {
} else { } else {
utr.Lower, err = rangeParseValue(buf) utr.Lower, err = rangeParseValue(buf)
if err != nil { if err != nil {
return nil, errors.Errorf("invalid lower value: %v", err) return nil, fmt.Errorf("invalid lower value: %v", err)
} }
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("missing range separator: %v", err) return nil, fmt.Errorf("missing range separator: %v", err)
} }
if r != ',' { if r != ',' {
return nil, errors.Errorf("missing range separator: %v", r) return nil, fmt.Errorf("missing range separator: %v", r)
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("invalid upper value: %v", err) return nil, fmt.Errorf("invalid upper value: %v", err)
} }
if r == ')' || r == ']' { if r == ')' || r == ']' {
@@ -86,12 +85,12 @@ func ParseUntypedTextRange(src string) (*UntypedTextRange, error) {
buf.UnreadRune() buf.UnreadRune()
utr.Upper, err = rangeParseValue(buf) utr.Upper, err = rangeParseValue(buf)
if err != nil { if err != nil {
return nil, errors.Errorf("invalid upper value: %v", err) return nil, fmt.Errorf("invalid upper value: %v", err)
} }
r, _, err = buf.ReadRune() r, _, err = buf.ReadRune()
if err != nil { if err != nil {
return nil, errors.Errorf("missing upper bound: %v", err) return nil, fmt.Errorf("missing upper bound: %v", err)
} }
switch r { switch r {
case ')': case ')':
@@ -99,14 +98,14 @@ func ParseUntypedTextRange(src string) (*UntypedTextRange, error) {
case ']': case ']':
utr.UpperType = Inclusive utr.UpperType = Inclusive
default: default:
return nil, errors.Errorf("missing upper bound, instead got: %v", string(r)) return nil, fmt.Errorf("missing upper bound, instead got: %v", string(r))
} }
} }
skipWhitespace(buf) skipWhitespace(buf)
if buf.Len() > 0 { if buf.Len() > 0 {
return nil, errors.Errorf("unexpected trailing data: %v", buf.String()) return nil, fmt.Errorf("unexpected trailing data: %v", buf.String())
} }
return utr, nil return utr, nil
@@ -202,7 +201,7 @@ func ParseUntypedBinaryRange(src []byte) (*UntypedBinaryRange, error) {
ubr := &UntypedBinaryRange{} ubr := &UntypedBinaryRange{}
if len(src) == 0 { if len(src) == 0 {
return nil, errors.Errorf("range too short: %v", len(src)) return nil, fmt.Errorf("range too short: %v", len(src))
} }
rangeType := src[0] rangeType := src[0]
@@ -210,7 +209,7 @@ func ParseUntypedBinaryRange(src []byte) (*UntypedBinaryRange, error) {
if rangeType&emptyMask > 0 { if rangeType&emptyMask > 0 {
if len(src[rp:]) > 0 { if len(src[rp:]) > 0 {
return nil, errors.Errorf("unexpected trailing bytes parsing empty range: %v", len(src[rp:])) return nil, fmt.Errorf("unexpected trailing bytes parsing empty range: %v", len(src[rp:]))
} }
ubr.LowerType = Empty ubr.LowerType = Empty
ubr.UpperType = Empty ubr.UpperType = Empty
@@ -235,13 +234,13 @@ func ParseUntypedBinaryRange(src []byte) (*UntypedBinaryRange, error) {
if ubr.LowerType == Unbounded && ubr.UpperType == Unbounded { if ubr.LowerType == Unbounded && ubr.UpperType == Unbounded {
if len(src[rp:]) > 0 { if len(src[rp:]) > 0 {
return nil, errors.Errorf("unexpected trailing bytes parsing unbounded range: %v", len(src[rp:])) return nil, fmt.Errorf("unexpected trailing bytes parsing unbounded range: %v", len(src[rp:]))
} }
return ubr, nil return ubr, nil
} }
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return nil, errors.Errorf("too few bytes for size: %v", src[rp:]) return nil, fmt.Errorf("too few bytes for size: %v", src[rp:])
} }
valueLen := int(binary.BigEndian.Uint32(src[rp:])) valueLen := int(binary.BigEndian.Uint32(src[rp:]))
rp += 4 rp += 4
@@ -254,14 +253,14 @@ func ParseUntypedBinaryRange(src []byte) (*UntypedBinaryRange, error) {
} else { } else {
ubr.Upper = val ubr.Upper = val
if len(src[rp:]) > 0 { if len(src[rp:]) > 0 {
return nil, errors.Errorf("unexpected trailing bytes parsing range: %v", len(src[rp:])) return nil, fmt.Errorf("unexpected trailing bytes parsing range: %v", len(src[rp:]))
} }
return ubr, nil return ubr, nil
} }
if ubr.UpperType != Unbounded { if ubr.UpperType != Unbounded {
if len(src[rp:]) < 4 { if len(src[rp:]) < 4 {
return nil, errors.Errorf("too few bytes for size: %v", src[rp:]) return nil, fmt.Errorf("too few bytes for size: %v", src[rp:])
} }
valueLen := int(binary.BigEndian.Uint32(src[rp:])) valueLen := int(binary.BigEndian.Uint32(src[rp:]))
rp += 4 rp += 4
@@ -270,7 +269,7 @@ func ParseUntypedBinaryRange(src []byte) (*UntypedBinaryRange, error) {
} }
if len(src[rp:]) > 0 { if len(src[rp:]) > 0 {
return nil, errors.Errorf("unexpected trailing bytes parsing range: %v", len(src[rp:])) return nil, fmt.Errorf("unexpected trailing bytes parsing range: %v", len(src[rp:]))
} }
return ubr, nil return ubr, nil
+6 -7
View File
@@ -1,9 +1,8 @@
package pgtype package pgtype
import ( import (
"fmt"
"reflect" "reflect"
errors "golang.org/x/xerrors"
) )
// Record is the generic PostgreSQL record type such as is created with the // Record is the generic PostgreSQL record type such as is created with the
@@ -33,7 +32,7 @@ func (dst *Record) Set(src interface{}) error {
case []Value: case []Value:
*dst = Record{Fields: value, Status: Present} *dst = Record{Fields: value, Status: Present}
default: default:
return errors.Errorf("cannot convert %v to Record", src) return fmt.Errorf("cannot convert %v to Record", src)
} }
return nil return nil
@@ -68,13 +67,13 @@ func (src *Record) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func prepareNewBinaryDecoder(ci *ConnInfo, fieldOID uint32, v *Value) (BinaryDecoder, error) { func prepareNewBinaryDecoder(ci *ConnInfo, fieldOID uint32, v *Value) (BinaryDecoder, error) {
@@ -83,11 +82,11 @@ func prepareNewBinaryDecoder(ci *ConnInfo, fieldOID uint32, v *Value) (BinaryDec
if dt, ok := ci.DataTypeForOID(fieldOID); ok { if dt, ok := ci.DataTypeForOID(fieldOID); ok {
binaryDecoder, _ = dt.Value.(BinaryDecoder) binaryDecoder, _ = dt.Value.(BinaryDecoder)
} else { } else {
return nil, errors.Errorf("unknown oid while decoding record: %v", fieldOID) return nil, fmt.Errorf("unknown oid while decoding record: %v", fieldOID)
} }
if binaryDecoder == nil { if binaryDecoder == nil {
return nil, errors.Errorf("no binary decoder registered for: %v", fieldOID) return nil, fmt.Errorf("no binary decoder registered for: %v", fieldOID)
} }
// Duplicate struct to scan into // Duplicate struct to scan into
+5 -6
View File
@@ -3,8 +3,7 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/json" "encoding/json"
"fmt"
errors "golang.org/x/xerrors"
) )
type Text struct { type Text struct {
@@ -44,7 +43,7 @@ func (dst *Text) Set(src interface{}) error {
if originalSrc, ok := underlyingStringType(src); ok { if originalSrc, ok := underlyingStringType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Text", value) return fmt.Errorf("cannot convert %v to Text", value)
} }
return nil return nil
@@ -76,13 +75,13 @@ func (src *Text) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (Text) PreferredResultFormat() int16 { func (Text) PreferredResultFormat() int16 {
@@ -138,7 +137,7 @@ func (dst *Text) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type TextArray struct { type TextArray struct {
@@ -96,7 +96,7 @@ func (dst *TextArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for TextArray", src) return fmt.Errorf("cannot find dimensions of %v for TextArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = TextArray{Status: Present} *dst = TextArray{Status: Present}
@@ -106,7 +106,7 @@ func (dst *TextArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to TextArray", src) return fmt.Errorf("cannot convert %v to TextArray", src)
} }
*dst = TextArray{ *dst = TextArray{
@@ -137,7 +137,7 @@ func (dst *TextArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to TextArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to TextArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *TextArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *TextArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to TextArray") return 0, fmt.Errorf("cannot convert all values to TextArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in TextArray", err) return 0, fmt.Errorf("%v in TextArray", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *TextArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *TextArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *TextArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *TextArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *TextArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *TextArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *TextArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from TextArray") return 0, fmt.Errorf("cannot assign all values from TextArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from TextArray") return 0, fmt.Errorf("cannot assign all values from TextArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src TextArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("text"); ok { if dt, ok := ci.DataTypeForName("text"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "text") return nil, fmt.Errorf("unable to find oid for type name %v", "text")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *TextArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+7 -8
View File
@@ -8,7 +8,6 @@ import (
"strings" "strings"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// TID is PostgreSQL's Tuple Identifier type. // TID is PostgreSQL's Tuple Identifier type.
@@ -29,7 +28,7 @@ type TID struct {
} }
func (dst *TID) Set(src interface{}) error { func (dst *TID) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to TID", src) return fmt.Errorf("cannot convert %v to TID", src)
} }
func (dst TID) Get() interface{} { func (dst TID) Get() interface{} {
@@ -53,11 +52,11 @@ func (src *TID) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
} }
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *TID) DecodeText(ci *ConnInfo, src []byte) error { func (dst *TID) DecodeText(ci *ConnInfo, src []byte) error {
@@ -67,12 +66,12 @@ func (dst *TID) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) < 5 { if len(src) < 5 {
return errors.Errorf("invalid length for tid: %v", len(src)) return fmt.Errorf("invalid length for tid: %v", len(src))
} }
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2) parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
if len(parts) < 2 { if len(parts) < 2 {
return errors.Errorf("invalid format for tid") return fmt.Errorf("invalid format for tid")
} }
blockNumber, err := strconv.ParseUint(parts[0], 10, 32) blockNumber, err := strconv.ParseUint(parts[0], 10, 32)
@@ -96,7 +95,7 @@ func (dst *TID) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 6 { if len(src) != 6 {
return errors.Errorf("invalid length for tid: %v", len(src)) return fmt.Errorf("invalid length for tid: %v", len(src))
} }
*dst = TID{ *dst = TID{
@@ -148,7 +147,7 @@ func (dst *TID) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+11 -12
View File
@@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
// Time represents the PostgreSQL time type. The PostgreSQL time is a time of day without time zone. // Time represents the PostgreSQL time type. The PostgreSQL time is a time of day without time zone.
@@ -52,7 +51,7 @@ func (dst *Time) Set(src interface{}) error {
if originalSrc, ok := underlyingTimeType(src); ok { if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Time", value) return fmt.Errorf("cannot convert %v to Time", value)
} }
return nil return nil
@@ -77,7 +76,7 @@ func (src *Time) AssignTo(dst interface{}) error {
// 24:00:00 is max allowed time in PostgreSQL, but time.Time will normalize that to 00:00:00 the next day. // 24:00:00 is max allowed time in PostgreSQL, but time.Time will normalize that to 00:00:00 the next day.
var maxRepresentableByTime int64 = 24*60*60*1000000 - 1 var maxRepresentableByTime int64 = 24*60*60*1000000 - 1
if src.Microseconds > maxRepresentableByTime { if src.Microseconds > maxRepresentableByTime {
return errors.Errorf("%d microseconds cannot be represented as time.Time", src.Microseconds) return fmt.Errorf("%d microseconds cannot be represented as time.Time", src.Microseconds)
} }
usec := src.Microseconds usec := src.Microseconds
@@ -94,13 +93,13 @@ func (src *Time) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
// DecodeText decodes from src into dst. // DecodeText decodes from src into dst.
@@ -113,24 +112,24 @@ func (dst *Time) DecodeText(ci *ConnInfo, src []byte) error {
s := string(src) s := string(src)
if len(s) < 8 { if len(s) < 8 {
return errors.Errorf("cannot decode %v into Time", s) return fmt.Errorf("cannot decode %v into Time", s)
} }
hours, err := strconv.ParseInt(s[0:2], 10, 64) hours, err := strconv.ParseInt(s[0:2], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot decode %v into Time", s) return fmt.Errorf("cannot decode %v into Time", s)
} }
usec := hours * microsecondsPerHour usec := hours * microsecondsPerHour
minutes, err := strconv.ParseInt(s[3:5], 10, 64) minutes, err := strconv.ParseInt(s[3:5], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot decode %v into Time", s) return fmt.Errorf("cannot decode %v into Time", s)
} }
usec += minutes * microsecondsPerMinute usec += minutes * microsecondsPerMinute
seconds, err := strconv.ParseInt(s[6:8], 10, 64) seconds, err := strconv.ParseInt(s[6:8], 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot decode %v into Time", s) return fmt.Errorf("cannot decode %v into Time", s)
} }
usec += seconds * microsecondsPerSecond usec += seconds * microsecondsPerSecond
@@ -138,7 +137,7 @@ func (dst *Time) DecodeText(ci *ConnInfo, src []byte) error {
fraction := s[9:] fraction := s[9:]
n, err := strconv.ParseInt(fraction, 10, 64) n, err := strconv.ParseInt(fraction, 10, 64)
if err != nil { if err != nil {
return errors.Errorf("cannot decode %v into Time", s) return fmt.Errorf("cannot decode %v into Time", s)
} }
for i := len(fraction); i < 6; i++ { for i := len(fraction); i < 6; i++ {
@@ -161,7 +160,7 @@ func (dst *Time) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for time: %v", len(src)) return fmt.Errorf("invalid length for time: %v", len(src))
} }
usec := int64(binary.BigEndian.Uint64(src)) usec := int64(binary.BigEndian.Uint64(src))
@@ -223,7 +222,7 @@ func (dst *Time) Scan(src interface{}) error {
return dst.Set(src) return dst.Set(src)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+9 -9
View File
@@ -3,10 +3,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
const pgTimestampFormat = "2006-01-02 15:04:05.999999999" const pgTimestampFormat = "2006-01-02 15:04:05.999999999"
@@ -52,7 +52,7 @@ func (dst *Timestamp) Set(src interface{}) error {
if originalSrc, ok := underlyingTimeType(src); ok { if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Timestamp", value) return fmt.Errorf("cannot convert %v to Timestamp", value)
} }
return nil return nil
@@ -78,7 +78,7 @@ func (src *Timestamp) AssignTo(dst interface{}) error {
switch v := dst.(type) { switch v := dst.(type) {
case *time.Time: case *time.Time:
if src.InfinityModifier != None { if src.InfinityModifier != None {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
*v = src.Time *v = src.Time
return nil return nil
@@ -86,13 +86,13 @@ func (src *Timestamp) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
// DecodeText decodes from src into dst. The decoded time is considered to // DecodeText decodes from src into dst. The decoded time is considered to
@@ -130,7 +130,7 @@ func (dst *Timestamp) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for timestamp: %v", len(src)) return fmt.Errorf("invalid length for timestamp: %v", len(src))
} }
microsecSinceY2K := int64(binary.BigEndian.Uint64(src)) microsecSinceY2K := int64(binary.BigEndian.Uint64(src))
@@ -159,7 +159,7 @@ func (src Timestamp) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, errUndefined return nil, errUndefined
} }
if src.Time.Location() != time.UTC { if src.Time.Location() != time.UTC {
return nil, errors.Errorf("cannot encode non-UTC time into timestamp") return nil, fmt.Errorf("cannot encode non-UTC time into timestamp")
} }
var s string var s string
@@ -186,7 +186,7 @@ func (src Timestamp) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, errUndefined return nil, errUndefined
} }
if src.Time.Location() != time.UTC { if src.Time.Location() != time.UTC {
return nil, errors.Errorf("cannot encode non-UTC time into timestamp") return nil, fmt.Errorf("cannot encode non-UTC time into timestamp")
} }
var microsecSinceY2K int64 var microsecSinceY2K int64
@@ -222,7 +222,7 @@ func (dst *Timestamp) Scan(src interface{}) error {
return nil return nil
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type TimestampArray struct { type TimestampArray struct {
@@ -97,7 +97,7 @@ func (dst *TimestampArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for TimestampArray", src) return fmt.Errorf("cannot find dimensions of %v for TimestampArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = TimestampArray{Status: Present} *dst = TimestampArray{Status: Present}
@@ -107,7 +107,7 @@ func (dst *TimestampArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to TimestampArray", src) return fmt.Errorf("cannot convert %v to TimestampArray", src)
} }
*dst = TimestampArray{ *dst = TimestampArray{
@@ -138,7 +138,7 @@ func (dst *TimestampArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to TimestampArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to TimestampArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -156,7 +156,7 @@ func (dst *TimestampArray) setRecursive(value reflect.Value, index, dimension in
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -169,10 +169,10 @@ func (dst *TimestampArray) setRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to TimestampArray") return 0, fmt.Errorf("cannot convert all values to TimestampArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in TimestampArray", err) return 0, fmt.Errorf("%v in TimestampArray", err)
} }
index++ index++
@@ -234,7 +234,7 @@ func (src *TimestampArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -249,7 +249,7 @@ func (src *TimestampArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -257,7 +257,7 @@ func (src *TimestampArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *TimestampArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *TimestampArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -273,7 +273,7 @@ func (src *TimestampArray) assignToRecursive(value reflect.Value, index, dimensi
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -291,14 +291,14 @@ func (src *TimestampArray) assignToRecursive(value reflect.Value, index, dimensi
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from TimestampArray") return 0, fmt.Errorf("cannot assign all values from TimestampArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from TimestampArray") return 0, fmt.Errorf("cannot assign all values from TimestampArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -457,7 +457,7 @@ func (src TimestampArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
if dt, ok := ci.DataTypeForName("timestamp"); ok { if dt, ok := ci.DataTypeForName("timestamp"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "timestamp") return nil, fmt.Errorf("unable to find oid for type name %v", "timestamp")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -501,7 +501,7 @@ func (dst *TimestampArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+7 -7
View File
@@ -4,10 +4,10 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
const pgTimestamptzHourFormat = "2006-01-02 15:04:05.999999999Z07" const pgTimestamptzHourFormat = "2006-01-02 15:04:05.999999999Z07"
@@ -54,7 +54,7 @@ func (dst *Timestamptz) Set(src interface{}) error {
if originalSrc, ok := underlyingTimeType(src); ok { if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to Timestamptz", value) return fmt.Errorf("cannot convert %v to Timestamptz", value)
} }
return nil return nil
@@ -80,7 +80,7 @@ func (src *Timestamptz) AssignTo(dst interface{}) error {
switch v := dst.(type) { switch v := dst.(type) {
case *time.Time: case *time.Time:
if src.InfinityModifier != None { if src.InfinityModifier != None {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
*v = src.Time *v = src.Time
return nil return nil
@@ -88,13 +88,13 @@ func (src *Timestamptz) AssignTo(dst interface{}) error {
if nextDst, retry := GetAssignToDstType(dst); retry { if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst) return src.AssignTo(nextDst)
} }
return errors.Errorf("unable to assign to %T", dst) return fmt.Errorf("unable to assign to %T", dst)
} }
case Null: case Null:
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (dst *Timestamptz) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Timestamptz) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (dst *Timestamptz) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 8 { if len(src) != 8 {
return errors.Errorf("invalid length for timestamptz: %v", len(src)) return fmt.Errorf("invalid length for timestamptz: %v", len(src))
} }
microsecSinceY2K := int64(binary.BigEndian.Uint64(src)) microsecSinceY2K := int64(binary.BigEndian.Uint64(src))
@@ -219,7 +219,7 @@ func (dst *Timestamptz) Scan(src interface{}) error {
return nil return nil
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,11 +5,11 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"time" "time"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type TimestamptzArray struct { type TimestamptzArray struct {
@@ -97,7 +97,7 @@ func (dst *TimestamptzArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for TimestamptzArray", src) return fmt.Errorf("cannot find dimensions of %v for TimestamptzArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = TimestamptzArray{Status: Present} *dst = TimestamptzArray{Status: Present}
@@ -107,7 +107,7 @@ func (dst *TimestamptzArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to TimestamptzArray", src) return fmt.Errorf("cannot convert %v to TimestamptzArray", src)
} }
*dst = TimestamptzArray{ *dst = TimestamptzArray{
@@ -138,7 +138,7 @@ func (dst *TimestamptzArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to TimestamptzArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to TimestamptzArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -156,7 +156,7 @@ func (dst *TimestamptzArray) setRecursive(value reflect.Value, index, dimension
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -169,10 +169,10 @@ func (dst *TimestamptzArray) setRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to TimestamptzArray") return 0, fmt.Errorf("cannot convert all values to TimestamptzArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in TimestamptzArray", err) return 0, fmt.Errorf("%v in TimestamptzArray", err)
} }
index++ index++
@@ -234,7 +234,7 @@ func (src *TimestamptzArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -249,7 +249,7 @@ func (src *TimestamptzArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -257,7 +257,7 @@ func (src *TimestamptzArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *TimestamptzArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *TimestamptzArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -273,7 +273,7 @@ func (src *TimestamptzArray) assignToRecursive(value reflect.Value, index, dimen
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -291,14 +291,14 @@ func (src *TimestamptzArray) assignToRecursive(value reflect.Value, index, dimen
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from TimestamptzArray") return 0, fmt.Errorf("cannot assign all values from TimestamptzArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from TimestamptzArray") return 0, fmt.Errorf("cannot assign all values from TimestamptzArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -457,7 +457,7 @@ func (src TimestamptzArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, erro
if dt, ok := ci.DataTypeForName("timestamptz"); ok { if dt, ok := ci.DataTypeForName("timestamptz"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "timestamptz") return nil, fmt.Errorf("unable to find oid for type name %v", "timestamptz")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -501,7 +501,7 @@ func (dst *TimestamptzArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Tsrange struct { type Tsrange struct {
@@ -30,7 +30,7 @@ func (dst *Tsrange) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Tsrange", src) return fmt.Errorf("cannot convert %v to Tsrange", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Tsrange) Get() interface{} {
} }
func (src *Tsrange) AssignTo(dst interface{}) error { func (src *Tsrange) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Tsrange) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Tsrange) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Tsrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Tsrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Tsrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Tsrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Tsrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Tsrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Tsrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Tsrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Tsrange) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type TsrangeArray struct { type TsrangeArray struct {
@@ -58,7 +58,7 @@ func (dst *TsrangeArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for TsrangeArray", src) return fmt.Errorf("cannot find dimensions of %v for TsrangeArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = TsrangeArray{Status: Present} *dst = TsrangeArray{Status: Present}
@@ -68,7 +68,7 @@ func (dst *TsrangeArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to TsrangeArray", src) return fmt.Errorf("cannot convert %v to TsrangeArray", src)
} }
*dst = TsrangeArray{ *dst = TsrangeArray{
@@ -99,7 +99,7 @@ func (dst *TsrangeArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to TsrangeArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to TsrangeArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -117,7 +117,7 @@ func (dst *TsrangeArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -130,10 +130,10 @@ func (dst *TsrangeArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to TsrangeArray") return 0, fmt.Errorf("cannot convert all values to TsrangeArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in TsrangeArray", err) return 0, fmt.Errorf("%v in TsrangeArray", err)
} }
index++ index++
@@ -186,7 +186,7 @@ func (src *TsrangeArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -201,7 +201,7 @@ func (src *TsrangeArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -209,7 +209,7 @@ func (src *TsrangeArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *TsrangeArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *TsrangeArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -225,7 +225,7 @@ func (src *TsrangeArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -243,14 +243,14 @@ func (src *TsrangeArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from TsrangeArray") return 0, fmt.Errorf("cannot assign all values from TsrangeArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from TsrangeArray") return 0, fmt.Errorf("cannot assign all values from TsrangeArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -409,7 +409,7 @@ func (src TsrangeArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("tsrange"); ok { if dt, ok := ci.DataTypeForName("tsrange"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "tsrange") return nil, fmt.Errorf("unable to find oid for type name %v", "tsrange")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -453,7 +453,7 @@ func (dst *TsrangeArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+12 -12
View File
@@ -2,9 +2,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Tstzrange struct { type Tstzrange struct {
@@ -30,7 +30,7 @@ func (dst *Tstzrange) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to Tstzrange", src) return fmt.Errorf("cannot convert %v to Tstzrange", src)
} }
return nil return nil
@@ -48,7 +48,7 @@ func (dst Tstzrange) Get() interface{} {
} }
func (src *Tstzrange) AssignTo(dst interface{}) error { func (src *Tstzrange) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Tstzrange) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Tstzrange) DecodeText(ci *ConnInfo, src []byte) error {
@@ -137,7 +137,7 @@ func (src Tstzrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -147,7 +147,7 @@ func (src Tstzrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -158,7 +158,7 @@ func (src Tstzrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -168,7 +168,7 @@ func (src Tstzrange) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -192,7 +192,7 @@ func (src Tstzrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -202,7 +202,7 @@ func (src Tstzrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -218,7 +218,7 @@ func (src Tstzrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -233,7 +233,7 @@ func (src Tstzrange) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -258,7 +258,7 @@ func (dst *Tstzrange) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type TstzrangeArray struct { type TstzrangeArray struct {
@@ -58,7 +58,7 @@ func (dst *TstzrangeArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for TstzrangeArray", src) return fmt.Errorf("cannot find dimensions of %v for TstzrangeArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = TstzrangeArray{Status: Present} *dst = TstzrangeArray{Status: Present}
@@ -68,7 +68,7 @@ func (dst *TstzrangeArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to TstzrangeArray", src) return fmt.Errorf("cannot convert %v to TstzrangeArray", src)
} }
*dst = TstzrangeArray{ *dst = TstzrangeArray{
@@ -99,7 +99,7 @@ func (dst *TstzrangeArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to TstzrangeArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to TstzrangeArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -117,7 +117,7 @@ func (dst *TstzrangeArray) setRecursive(value reflect.Value, index, dimension in
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -130,10 +130,10 @@ func (dst *TstzrangeArray) setRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to TstzrangeArray") return 0, fmt.Errorf("cannot convert all values to TstzrangeArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in TstzrangeArray", err) return 0, fmt.Errorf("%v in TstzrangeArray", err)
} }
index++ index++
@@ -186,7 +186,7 @@ func (src *TstzrangeArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -201,7 +201,7 @@ func (src *TstzrangeArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -209,7 +209,7 @@ func (src *TstzrangeArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *TstzrangeArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *TstzrangeArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -225,7 +225,7 @@ func (src *TstzrangeArray) assignToRecursive(value reflect.Value, index, dimensi
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -243,14 +243,14 @@ func (src *TstzrangeArray) assignToRecursive(value reflect.Value, index, dimensi
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from TstzrangeArray") return 0, fmt.Errorf("cannot assign all values from TstzrangeArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from TstzrangeArray") return 0, fmt.Errorf("cannot assign all values from TstzrangeArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -409,7 +409,7 @@ func (src TstzrangeArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error)
if dt, ok := ci.DataTypeForName("tstzrange"); ok { if dt, ok := ci.DataTypeForName("tstzrange"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "tstzrange") return nil, fmt.Errorf("unable to find oid for type name %v", "tstzrange")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -453,7 +453,7 @@ func (dst *TstzrangeArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+15 -15
View File
@@ -78,7 +78,7 @@ func (dst *<%= pgtype_array_type %>) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for <%= pgtype_array_type %>", src) return fmt.Errorf("cannot find dimensions of %v for <%= pgtype_array_type %>", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = <%= pgtype_array_type %>{Status: Present} *dst = <%= pgtype_array_type %>{Status: Present}
@@ -88,7 +88,7 @@ func (dst *<%= pgtype_array_type %>) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to <%= pgtype_array_type %>", src) return fmt.Errorf("cannot convert %v to <%= pgtype_array_type %>", src)
} }
*dst = <%= pgtype_array_type %> { *dst = <%= pgtype_array_type %> {
@@ -119,7 +119,7 @@ func (dst *<%= pgtype_array_type %>) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to <%= pgtype_array_type %>, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to <%= pgtype_array_type %>, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -137,7 +137,7 @@ func (dst *<%= pgtype_array_type %>) setRecursive(value reflect.Value, index, di
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -150,10 +150,10 @@ func (dst *<%= pgtype_array_type %>) setRecursive(value reflect.Value, index, di
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to <%= pgtype_array_type %>") return 0, fmt.Errorf("cannot convert all values to <%= pgtype_array_type %>")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in <%= pgtype_array_type %>", err) return 0, fmt.Errorf("%v in <%= pgtype_array_type %>", err)
} }
index++ index++
@@ -206,7 +206,7 @@ func (src *<%= pgtype_array_type %>) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -221,7 +221,7 @@ func (src *<%= pgtype_array_type %>) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -229,7 +229,7 @@ func (src *<%= pgtype_array_type %>) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *<%= pgtype_array_type %>) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *<%= pgtype_array_type %>) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -245,7 +245,7 @@ func (src *<%= pgtype_array_type %>) assignToRecursive(value reflect.Value, inde
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -263,14 +263,14 @@ func (src *<%= pgtype_array_type %>) assignToRecursive(value reflect.Value, inde
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr(){ if !value.CanAddr(){
return 0, errors.Errorf("cannot assign all values from <%= pgtype_array_type %>") return 0, fmt.Errorf("cannot assign all values from <%= pgtype_array_type %>")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from <%= pgtype_array_type %>") return 0, fmt.Errorf("cannot assign all values from <%= pgtype_array_type %>")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -432,7 +432,7 @@ func (src <%= pgtype_array_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byte
if dt, ok := ci.DataTypeForName("<%= element_type_name %>"); ok { if dt, ok := ci.DataTypeForName("<%= element_type_name %>"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "<%= element_type_name %>") return nil, fmt.Errorf("unable to find oid for type name %v", "<%= element_type_name %>")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -477,7 +477,7 @@ func (dst *<%= pgtype_array_type %>) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+11 -11
View File
@@ -32,7 +32,7 @@ func (dst *<%= range_type %>) Set(src interface{}) error {
case string: case string:
return dst.DecodeText(nil, []byte(value)) return dst.DecodeText(nil, []byte(value))
default: default:
return errors.Errorf("cannot convert %v to <%= range_type %>", src) return fmt.Errorf("cannot convert %v to <%= range_type %>", src)
} }
return nil return nil
@@ -50,7 +50,7 @@ func (dst <%= range_type %>) Get() interface{} {
} }
func (src *<%= range_type %>) AssignTo(dst interface{}) error { func (src *<%= range_type %>) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *<%= range_type %>) DecodeText(ci *ConnInfo, src []byte) error { func (dst *<%= range_type %>) DecodeText(ci *ConnInfo, src []byte) error {
@@ -139,7 +139,7 @@ func (src <%= range_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error
case Empty: case Empty:
return append(buf, "empty"...), nil return append(buf, "empty"...), nil
default: default:
return nil, errors.Errorf("unknown lower bound type %v", src.LowerType) return nil, fmt.Errorf("unknown lower bound type %v", src.LowerType)
} }
var err error var err error
@@ -149,7 +149,7 @@ func (src <%= range_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
} }
@@ -160,7 +160,7 @@ func (src <%= range_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error
if err != nil { if err != nil {
return nil, err return nil, err
} else if buf == nil { } else if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
} }
@@ -170,7 +170,7 @@ func (src <%= range_type %>) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error
case Inclusive: case Inclusive:
buf = append(buf, ']') buf = append(buf, ']')
default: default:
return nil, errors.Errorf("unknown upper bound type %v", src.UpperType) return nil, fmt.Errorf("unknown upper bound type %v", src.UpperType)
} }
return buf, nil return buf, nil
@@ -194,7 +194,7 @@ func (src <%= range_type %>) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, err
case Empty: case Empty:
return append(buf, emptyMask), nil return append(buf, emptyMask), nil
default: default:
return nil, errors.Errorf("unknown LowerType: %v", src.LowerType) return nil, fmt.Errorf("unknown LowerType: %v", src.LowerType)
} }
switch src.UpperType { switch src.UpperType {
@@ -204,7 +204,7 @@ func (src <%= range_type %>) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, err
rangeType |= upperUnboundedMask rangeType |= upperUnboundedMask
case Exclusive: case Exclusive:
default: default:
return nil, errors.Errorf("unknown UpperType: %v", src.UpperType) return nil, fmt.Errorf("unknown UpperType: %v", src.UpperType)
} }
buf = append(buf, rangeType) buf = append(buf, rangeType)
@@ -220,7 +220,7 @@ func (src <%= range_type %>) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, err
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Lower cannot be null unless LowerType is Unbounded") return nil, fmt.Errorf("Lower cannot be null unless LowerType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -235,7 +235,7 @@ func (src <%= range_type %>) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, err
return nil, err return nil, err
} }
if buf == nil { if buf == nil {
return nil, errors.Errorf("Upper cannot be null unless UpperType is Unbounded") return nil, fmt.Errorf("Upper cannot be null unless UpperType is Unbounded")
} }
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4)) pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
@@ -260,7 +260,7 @@ func (dst *<%= range_type %>) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+8 -10
View File
@@ -5,8 +5,6 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
errors "golang.org/x/xerrors"
) )
type UUID struct { type UUID struct {
@@ -33,7 +31,7 @@ func (dst *UUID) Set(src interface{}) error {
case []byte: case []byte:
if value != nil { if value != nil {
if len(value) != 16 { if len(value) != 16 {
return errors.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value)) return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
} }
*dst = UUID{Status: Present} *dst = UUID{Status: Present}
copy(dst.Bytes[:], value) copy(dst.Bytes[:], value)
@@ -56,7 +54,7 @@ func (dst *UUID) Set(src interface{}) error {
if originalSrc, ok := underlyingUUIDType(src); ok { if originalSrc, ok := underlyingUUIDType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to UUID", value) return fmt.Errorf("cannot convert %v to UUID", value)
} }
return nil return nil
@@ -96,7 +94,7 @@ func (src *UUID) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot assign %v into %T", src, dst) return fmt.Errorf("cannot assign %v into %T", src, dst)
} }
// parseUUID converts a string UUID in standard form to a byte array. // parseUUID converts a string UUID in standard form to a byte array.
@@ -108,7 +106,7 @@ func parseUUID(src string) (dst [16]byte, err error) {
// dashes already stripped, assume valid // dashes already stripped, assume valid
default: default:
// assume invalid. // assume invalid.
return dst, errors.Errorf("cannot parse UUID %v", src) return dst, fmt.Errorf("cannot parse UUID %v", src)
} }
buf, err := hex.DecodeString(src) buf, err := hex.DecodeString(src)
@@ -132,7 +130,7 @@ func (dst *UUID) DecodeText(ci *ConnInfo, src []byte) error {
} }
if len(src) != 36 { if len(src) != 36 {
return errors.Errorf("invalid length for UUID: %v", len(src)) return fmt.Errorf("invalid length for UUID: %v", len(src))
} }
buf, err := parseUUID(string(src)) buf, err := parseUUID(string(src))
@@ -151,7 +149,7 @@ func (dst *UUID) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) != 16 { if len(src) != 16 {
return errors.Errorf("invalid length for UUID: %v", len(src)) return fmt.Errorf("invalid length for UUID: %v", len(src))
} }
*dst = UUID{Status: Present} *dst = UUID{Status: Present}
@@ -197,7 +195,7 @@ func (dst *UUID) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
@@ -226,7 +224,7 @@ func (dst *UUID) UnmarshalJSON(src []byte) error {
return dst.Set(nil) return dst.Set(nil)
} }
if len(src) != 38 { if len(src) != 38 {
return errors.Errorf("invalid length for UUID: %v", len(src)) return fmt.Errorf("invalid length for UUID: %v", len(src))
} }
return dst.Set(string(src[1 : len(src)-1])) return dst.Set(string(src[1 : len(src)-1]))
} }
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type UUIDArray struct { type UUIDArray struct {
@@ -134,7 +134,7 @@ func (dst *UUIDArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for UUIDArray", src) return fmt.Errorf("cannot find dimensions of %v for UUIDArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = UUIDArray{Status: Present} *dst = UUIDArray{Status: Present}
@@ -144,7 +144,7 @@ func (dst *UUIDArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to UUIDArray", src) return fmt.Errorf("cannot convert %v to UUIDArray", src)
} }
*dst = UUIDArray{ *dst = UUIDArray{
@@ -175,7 +175,7 @@ func (dst *UUIDArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to UUIDArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to UUIDArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -193,7 +193,7 @@ func (dst *UUIDArray) setRecursive(value reflect.Value, index, dimension int) (i
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -206,10 +206,10 @@ func (dst *UUIDArray) setRecursive(value reflect.Value, index, dimension int) (i
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to UUIDArray") return 0, fmt.Errorf("cannot convert all values to UUIDArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in UUIDArray", err) return 0, fmt.Errorf("%v in UUIDArray", err)
} }
index++ index++
@@ -289,7 +289,7 @@ func (src *UUIDArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -304,7 +304,7 @@ func (src *UUIDArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -312,7 +312,7 @@ func (src *UUIDArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *UUIDArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *UUIDArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -328,7 +328,7 @@ func (src *UUIDArray) assignToRecursive(value reflect.Value, index, dimension in
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -346,14 +346,14 @@ func (src *UUIDArray) assignToRecursive(value reflect.Value, index, dimension in
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from UUIDArray") return 0, fmt.Errorf("cannot assign all values from UUIDArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from UUIDArray") return 0, fmt.Errorf("cannot assign all values from UUIDArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -512,7 +512,7 @@ func (src UUIDArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("uuid"); ok { if dt, ok := ci.DataTypeForName("uuid"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "uuid") return nil, fmt.Errorf("unable to find oid for type name %v", "uuid")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -556,7 +556,7 @@ func (dst *UUIDArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+5 -5
View File
@@ -3,9 +3,9 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type Varbit struct { type Varbit struct {
@@ -15,7 +15,7 @@ type Varbit struct {
} }
func (dst *Varbit) Set(src interface{}) error { func (dst *Varbit) Set(src interface{}) error {
return errors.Errorf("cannot convert %v to Varbit", src) return fmt.Errorf("cannot convert %v to Varbit", src)
} }
func (dst Varbit) Get() interface{} { func (dst Varbit) Get() interface{} {
@@ -30,7 +30,7 @@ func (dst Varbit) Get() interface{} {
} }
func (src *Varbit) AssignTo(dst interface{}) error { func (src *Varbit) AssignTo(dst interface{}) error {
return errors.Errorf("cannot assign %v to %T", src, dst) return fmt.Errorf("cannot assign %v to %T", src, dst)
} }
func (dst *Varbit) DecodeText(ci *ConnInfo, src []byte) error { func (dst *Varbit) DecodeText(ci *ConnInfo, src []byte) error {
@@ -65,7 +65,7 @@ func (dst *Varbit) DecodeBinary(ci *ConnInfo, src []byte) error {
} }
if len(src) < 4 { if len(src) < 4 {
return errors.Errorf("invalid length for varbit: %v", len(src)) return fmt.Errorf("invalid length for varbit: %v", len(src))
} }
bitLen := int32(binary.BigEndian.Uint32(src)) bitLen := int32(binary.BigEndian.Uint32(src))
@@ -124,7 +124,7 @@ func (dst *Varbit) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.
+16 -16
View File
@@ -5,10 +5,10 @@ package pgtype
import ( import (
"database/sql/driver" "database/sql/driver"
"encoding/binary" "encoding/binary"
"fmt"
"reflect" "reflect"
"github.com/jackc/pgio" "github.com/jackc/pgio"
errors "golang.org/x/xerrors"
) )
type VarcharArray struct { type VarcharArray struct {
@@ -96,7 +96,7 @@ func (dst *VarcharArray) Set(src interface{}) error {
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0) dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok { if !ok {
return errors.Errorf("cannot find dimensions of %v for VarcharArray", src) return fmt.Errorf("cannot find dimensions of %v for VarcharArray", src)
} }
if elementsLength == 0 { if elementsLength == 0 {
*dst = VarcharArray{Status: Present} *dst = VarcharArray{Status: Present}
@@ -106,7 +106,7 @@ func (dst *VarcharArray) Set(src interface{}) error {
if originalSrc, ok := underlyingSliceType(src); ok { if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc) return dst.Set(originalSrc)
} }
return errors.Errorf("cannot convert %v to VarcharArray", src) return fmt.Errorf("cannot convert %v to VarcharArray", src)
} }
*dst = VarcharArray{ *dst = VarcharArray{
@@ -137,7 +137,7 @@ func (dst *VarcharArray) Set(src interface{}) error {
} }
} }
if elementCount != len(dst.Elements) { if elementCount != len(dst.Elements) {
return errors.Errorf("cannot convert %v to VarcharArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount) return fmt.Errorf("cannot convert %v to VarcharArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
} }
} }
@@ -155,7 +155,7 @@ func (dst *VarcharArray) setRecursive(value reflect.Value, index, dimension int)
valueLen := value.Len() valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length { if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, errors.Errorf("multidimensional arrays must have array expressions with matching dimensions") return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
} }
for i := 0; i < valueLen; i++ { for i := 0; i < valueLen; i++ {
var err error var err error
@@ -168,10 +168,10 @@ func (dst *VarcharArray) setRecursive(value reflect.Value, index, dimension int)
return index, nil return index, nil
} }
if !value.CanInterface() { if !value.CanInterface() {
return 0, errors.Errorf("cannot convert all values to VarcharArray") return 0, fmt.Errorf("cannot convert all values to VarcharArray")
} }
if err := dst.Elements[index].Set(value.Interface()); err != nil { if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, errors.Errorf("%v in VarcharArray", err) return 0, fmt.Errorf("%v in VarcharArray", err)
} }
index++ index++
@@ -233,7 +233,7 @@ func (src *VarcharArray) AssignTo(dst interface{}) error {
switch value.Kind() { switch value.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
default: default:
return errors.Errorf("cannot assign %T to %T", src, dst) return fmt.Errorf("cannot assign %T to %T", src, dst)
} }
if len(src.Elements) == 0 { if len(src.Elements) == 0 {
@@ -248,7 +248,7 @@ func (src *VarcharArray) AssignTo(dst interface{}) error {
return err return err
} }
if elementCount != len(src.Elements) { if elementCount != len(src.Elements) {
return errors.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount) return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
} }
return nil return nil
@@ -256,7 +256,7 @@ func (src *VarcharArray) AssignTo(dst interface{}) error {
return NullAssignTo(dst) return NullAssignTo(dst)
} }
return errors.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
func (src *VarcharArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) { func (src *VarcharArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
@@ -272,7 +272,7 @@ func (src *VarcharArray) assignToRecursive(value reflect.Value, index, dimension
if reflect.Array == kind { if reflect.Array == kind {
typ := value.Type() typ := value.Type()
if typ.Len() != length { if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len()) return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
} }
value.Set(reflect.New(typ).Elem()) value.Set(reflect.New(typ).Elem())
} else { } else {
@@ -290,14 +290,14 @@ func (src *VarcharArray) assignToRecursive(value reflect.Value, index, dimension
return index, nil return index, nil
} }
if len(src.Dimensions) != dimension { if len(src.Dimensions) != dimension {
return 0, errors.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension) return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
} }
if !value.CanAddr() { if !value.CanAddr() {
return 0, errors.Errorf("cannot assign all values from VarcharArray") return 0, fmt.Errorf("cannot assign all values from VarcharArray")
} }
addr := value.Addr() addr := value.Addr()
if !addr.CanInterface() { if !addr.CanInterface() {
return 0, errors.Errorf("cannot assign all values from VarcharArray") return 0, fmt.Errorf("cannot assign all values from VarcharArray")
} }
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil { if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err return 0, err
@@ -456,7 +456,7 @@ func (src VarcharArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if dt, ok := ci.DataTypeForName("varchar"); ok { if dt, ok := ci.DataTypeForName("varchar"); ok {
arrayHeader.ElementOID = int32(dt.OID) arrayHeader.ElementOID = int32(dt.OID)
} else { } else {
return nil, errors.Errorf("unable to find oid for type name %v", "varchar") return nil, fmt.Errorf("unable to find oid for type name %v", "varchar")
} }
for i := range src.Elements { for i := range src.Elements {
@@ -500,7 +500,7 @@ func (dst *VarcharArray) Scan(src interface{}) error {
return dst.DecodeText(nil, srcCopy) return dst.DecodeText(nil, srcCopy)
} }
return errors.Errorf("cannot scan %T", src) return fmt.Errorf("cannot scan %T", src)
} }
// Value implements the database/sql/driver Valuer interface. // Value implements the database/sql/driver Valuer interface.