mirror of https://github.com/jackc/pgx.git
Begin introduction of Convert
parent
e5707023ca
commit
0f93bfc2de
|
@ -11,72 +11,74 @@ import (
|
||||||
|
|
||||||
type Int2 int16
|
type Int2 int16
|
||||||
|
|
||||||
func ConvertToInt2(src interface{}) (Int2, error) {
|
func (i *Int2) Convert(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int2:
|
case Int2:
|
||||||
return value, nil
|
*i = value
|
||||||
case int8:
|
case int8:
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case uint8:
|
case uint8:
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case int16:
|
case int16:
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case uint16:
|
case uint16:
|
||||||
if value > math.MaxInt16 {
|
if value > math.MaxInt16 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case int32:
|
case int32:
|
||||||
if value < math.MinInt16 {
|
if value < math.MinInt16 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case uint32:
|
case uint32:
|
||||||
if value > math.MaxInt16 {
|
if value > math.MaxInt16 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case int64:
|
case int64:
|
||||||
if value < math.MinInt16 {
|
if value < math.MinInt16 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case uint64:
|
case uint64:
|
||||||
if value > math.MaxInt16 {
|
if value > math.MaxInt16 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case int:
|
case int:
|
||||||
if value < math.MinInt16 {
|
if value < math.MinInt16 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case uint:
|
case uint:
|
||||||
if value > math.MaxInt16 {
|
if value > math.MaxInt16 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int2", value)
|
return fmt.Errorf("%d is greater than maximum value for Int2", value)
|
||||||
}
|
}
|
||||||
return Int2(value), nil
|
*i = Int2(value)
|
||||||
case string:
|
case string:
|
||||||
num, err := strconv.ParseInt(value, 10, 16)
|
num, err := strconv.ParseInt(value, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
return Int2(num), nil
|
*i = Int2(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return ConvertToInt2(originalSrc)
|
return i.Convert(originalSrc)
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("cannot convert %v to Int2", value)
|
return fmt.Errorf("cannot convert %v to Int2", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Int2) DecodeText(r io.Reader) error {
|
func (i *Int2) DecodeText(r io.Reader) error {
|
||||||
|
|
|
@ -85,3 +85,35 @@ func TestInt2Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInt2Convert(t *testing.T) {
|
||||||
|
type _int8 int8
|
||||||
|
|
||||||
|
successfulTests := []struct {
|
||||||
|
source interface{}
|
||||||
|
result pgtype.Int2
|
||||||
|
}{
|
||||||
|
{source: int8(1), result: pgtype.Int2(1)},
|
||||||
|
{source: int16(1), result: pgtype.Int2(1)},
|
||||||
|
{source: int32(1), result: pgtype.Int2(1)},
|
||||||
|
{source: int64(1), result: pgtype.Int2(1)},
|
||||||
|
{source: int8(-1), result: pgtype.Int2(-1)},
|
||||||
|
{source: int16(-1), result: pgtype.Int2(-1)},
|
||||||
|
{source: int32(-1), result: pgtype.Int2(-1)},
|
||||||
|
{source: int64(-1), result: pgtype.Int2(-1)},
|
||||||
|
{source: uint8(1), result: pgtype.Int2(1)},
|
||||||
|
{source: uint16(1), result: pgtype.Int2(1)},
|
||||||
|
{source: uint32(1), result: pgtype.Int2(1)},
|
||||||
|
{source: uint64(1), result: pgtype.Int2(1)},
|
||||||
|
{source: "1", result: pgtype.Int2(1)},
|
||||||
|
{source: _int8(1), result: pgtype.Int2(1)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range successfulTests {
|
||||||
|
var r pgtype.Int2
|
||||||
|
err := r.Convert(tt.source)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,63 +11,65 @@ import (
|
||||||
|
|
||||||
type Int4 int32
|
type Int4 int32
|
||||||
|
|
||||||
func ConvertToInt4(src interface{}) (Int4, error) {
|
func (i *Int4) Convert(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int4:
|
case Int4:
|
||||||
return value, nil
|
*i = value
|
||||||
case int8:
|
case int8:
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case uint8:
|
case uint8:
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case int16:
|
case int16:
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case uint16:
|
case uint16:
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case int32:
|
case int32:
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case uint32:
|
case uint32:
|
||||||
if value > math.MaxInt32 {
|
if value > math.MaxInt32 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int4", value)
|
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||||
}
|
}
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case int64:
|
case int64:
|
||||||
if value < math.MinInt32 {
|
if value < math.MinInt32 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int4", value)
|
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||||
}
|
}
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case uint64:
|
case uint64:
|
||||||
if value > math.MaxInt32 {
|
if value > math.MaxInt32 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int4", value)
|
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||||
}
|
}
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case int:
|
case int:
|
||||||
if value < math.MinInt32 {
|
if value < math.MinInt32 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int4", value)
|
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||||
}
|
}
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case uint:
|
case uint:
|
||||||
if value > math.MaxInt32 {
|
if value > math.MaxInt32 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int4", value)
|
return fmt.Errorf("%d is greater than maximum value for Int4", value)
|
||||||
}
|
}
|
||||||
return Int4(value), nil
|
*i = Int4(value)
|
||||||
case string:
|
case string:
|
||||||
num, err := strconv.ParseInt(value, 10, 32)
|
num, err := strconv.ParseInt(value, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
return Int4(num), nil
|
*i = Int4(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return ConvertToInt4(originalSrc)
|
return i.Convert(originalSrc)
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("cannot convert %v to Int8", value)
|
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Int4) DecodeText(r io.Reader) error {
|
func (i *Int4) DecodeText(r io.Reader) error {
|
||||||
|
|
|
@ -85,3 +85,35 @@ func TestInt4Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInt4Convert(t *testing.T) {
|
||||||
|
type _int8 int8
|
||||||
|
|
||||||
|
successfulTests := []struct {
|
||||||
|
source interface{}
|
||||||
|
result pgtype.Int4
|
||||||
|
}{
|
||||||
|
{source: int8(1), result: pgtype.Int4(1)},
|
||||||
|
{source: int16(1), result: pgtype.Int4(1)},
|
||||||
|
{source: int32(1), result: pgtype.Int4(1)},
|
||||||
|
{source: int64(1), result: pgtype.Int4(1)},
|
||||||
|
{source: int8(-1), result: pgtype.Int4(-1)},
|
||||||
|
{source: int16(-1), result: pgtype.Int4(-1)},
|
||||||
|
{source: int32(-1), result: pgtype.Int4(-1)},
|
||||||
|
{source: int64(-1), result: pgtype.Int4(-1)},
|
||||||
|
{source: uint8(1), result: pgtype.Int4(1)},
|
||||||
|
{source: uint16(1), result: pgtype.Int4(1)},
|
||||||
|
{source: uint32(1), result: pgtype.Int4(1)},
|
||||||
|
{source: uint64(1), result: pgtype.Int4(1)},
|
||||||
|
{source: "1", result: pgtype.Int4(1)},
|
||||||
|
{source: _int8(1), result: pgtype.Int4(1)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range successfulTests {
|
||||||
|
var r pgtype.Int4
|
||||||
|
err := r.Convert(tt.source)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,54 +11,56 @@ import (
|
||||||
|
|
||||||
type Int8 int64
|
type Int8 int64
|
||||||
|
|
||||||
func ConvertToInt8(src interface{}) (Int8, error) {
|
func (i *Int8) Convert(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int8:
|
case Int8:
|
||||||
return value, nil
|
*i = value
|
||||||
case int8:
|
case int8:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case uint8:
|
case uint8:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case int16:
|
case int16:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case uint16:
|
case uint16:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case int32:
|
case int32:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case uint32:
|
case uint32:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case int64:
|
case int64:
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case uint64:
|
case uint64:
|
||||||
if value > math.MaxInt64 {
|
if value > math.MaxInt64 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int8", value)
|
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||||
}
|
}
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case int:
|
case int:
|
||||||
if int64(value) < math.MinInt64 {
|
if int64(value) < math.MinInt64 {
|
||||||
return 0, fmt.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 0, fmt.Errorf("%d is greater than maximum value for Int8", value)
|
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||||
}
|
}
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case uint:
|
case uint:
|
||||||
if uint64(value) > math.MaxInt64 {
|
if uint64(value) > math.MaxInt64 {
|
||||||
return 0, fmt.Errorf("%d is greater than maximum value for Int8", value)
|
return fmt.Errorf("%d is greater than maximum value for Int8", value)
|
||||||
}
|
}
|
||||||
return Int8(value), nil
|
*i = Int8(value)
|
||||||
case string:
|
case string:
|
||||||
num, err := strconv.ParseInt(value, 10, 64)
|
num, err := strconv.ParseInt(value, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return err
|
||||||
}
|
}
|
||||||
return Int8(num), nil
|
*i = Int8(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return ConvertToInt8(originalSrc)
|
return i.Convert(originalSrc)
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("cannot convert %v to Int8", value)
|
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Int8) DecodeText(r io.Reader) error {
|
func (i *Int8) DecodeText(r io.Reader) error {
|
||||||
|
|
|
@ -85,3 +85,35 @@ func TestInt8Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInt8Convert(t *testing.T) {
|
||||||
|
type _int8 int8
|
||||||
|
|
||||||
|
successfulTests := []struct {
|
||||||
|
source interface{}
|
||||||
|
result pgtype.Int8
|
||||||
|
}{
|
||||||
|
{source: int8(1), result: pgtype.Int8(1)},
|
||||||
|
{source: int16(1), result: pgtype.Int8(1)},
|
||||||
|
{source: int32(1), result: pgtype.Int8(1)},
|
||||||
|
{source: int64(1), result: pgtype.Int8(1)},
|
||||||
|
{source: int8(-1), result: pgtype.Int8(-1)},
|
||||||
|
{source: int16(-1), result: pgtype.Int8(-1)},
|
||||||
|
{source: int32(-1), result: pgtype.Int8(-1)},
|
||||||
|
{source: int64(-1), result: pgtype.Int8(-1)},
|
||||||
|
{source: uint8(1), result: pgtype.Int8(1)},
|
||||||
|
{source: uint16(1), result: pgtype.Int8(1)},
|
||||||
|
{source: uint32(1), result: pgtype.Int8(1)},
|
||||||
|
{source: uint64(1), result: pgtype.Int8(1)},
|
||||||
|
{source: "1", result: pgtype.Int8(1)},
|
||||||
|
{source: _int8(1), result: pgtype.Int8(1)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range successfulTests {
|
||||||
|
var r pgtype.Int8
|
||||||
|
err := r.Convert(tt.source)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1062,19 +1062,22 @@ func Encode(wbuf *WriteBuf, oid OID, arg interface{}) error {
|
||||||
|
|
||||||
switch oid {
|
switch oid {
|
||||||
case Int2OID:
|
case Int2OID:
|
||||||
i2, err := pgtype.ConvertToInt2(arg)
|
var i2 pgtype.Int2
|
||||||
|
err := i2.Convert(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return i2.EncodeBinary(wbuf)
|
return i2.EncodeBinary(wbuf)
|
||||||
case Int4OID:
|
case Int4OID:
|
||||||
i4, err := pgtype.ConvertToInt4(arg)
|
var i4 pgtype.Int4
|
||||||
|
err := i4.Convert(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return i4.EncodeBinary(wbuf)
|
return i4.EncodeBinary(wbuf)
|
||||||
case Int8OID:
|
case Int8OID:
|
||||||
i8, err := pgtype.ConvertToInt8(arg)
|
var i8 pgtype.Int8
|
||||||
|
err := i8.Convert(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue