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
+8 -8
View File
@@ -2,8 +2,8 @@ package uuid
import (
"database/sql/driver"
errors "golang.org/x/xerrors"
"errors"
"fmt"
"github.com/gofrs/uuid"
"github.com/jackc/pgtype"
@@ -37,7 +37,7 @@ func (dst *UUID) Set(src interface{}) error {
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
case []byte:
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}
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.
pgUUID := &pgtype.UUID{}
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}
@@ -92,13 +92,13 @@ func (src *UUID) AssignTo(dst interface{}) error {
if nextDst, retry := pgtype.GetAssignToDstType(v); retry {
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:
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 {
@@ -123,7 +123,7 @@ func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
}
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}
@@ -167,7 +167,7 @@ func (dst *UUID) Scan(src interface{}) error {
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.
+27 -27
View File
@@ -2,10 +2,10 @@ package numeric
import (
"database/sql/driver"
"errors"
"fmt"
"strconv"
errors "golang.org/x/xerrors"
"github.com/jackc/pgtype"
"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.
num := &pgtype.Numeric{}
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)
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))
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}
}
@@ -121,99 +121,99 @@ func (src *Numeric) AssignTo(dst interface{}) error {
*v = f
case *int:
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)
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)
case *int8:
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)
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)
case *int16:
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)
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)
case *int32:
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)
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)
case *int64:
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)
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)
case *uint:
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)
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)
case *uint8:
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)
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)
case *uint16:
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)
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)
case *uint32:
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)
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)
case *uint64:
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)
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)
default:
if nextDst, retry := pgtype.GetAssignToDstType(dst); retry {
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:
return pgtype.NullAssignTo(dst)
@@ -300,7 +300,7 @@ func (dst *Numeric) Scan(src interface{}) error {
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.