Begin introduction of Convert

pgxtype-experiment2
Jack Christensen 2017-02-24 14:48:34 -06:00
parent e5707023ca
commit 0f93bfc2de
7 changed files with 177 additions and 72 deletions

View File

@ -11,72 +11,74 @@ import (
type Int2 int16
func ConvertToInt2(src interface{}) (Int2, error) {
func (i *Int2) Convert(src interface{}) error {
switch value := src.(type) {
case Int2:
return value, nil
*i = value
case int8:
return Int2(value), nil
*i = Int2(value)
case uint8:
return Int2(value), nil
*i = Int2(value)
case int16:
return Int2(value), nil
*i = Int2(value)
case uint16:
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:
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 {
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:
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:
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 {
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:
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:
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 {
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:
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:
num, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return 0, err
return err
}
return Int2(num), nil
*i = Int2(num)
default:
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 {

View File

@ -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)
}
}
}

View File

@ -11,63 +11,65 @@ import (
type Int4 int32
func ConvertToInt4(src interface{}) (Int4, error) {
func (i *Int4) Convert(src interface{}) error {
switch value := src.(type) {
case Int4:
return value, nil
*i = value
case int8:
return Int4(value), nil
*i = Int4(value)
case uint8:
return Int4(value), nil
*i = Int4(value)
case int16:
return Int4(value), nil
*i = Int4(value)
case uint16:
return Int4(value), nil
*i = Int4(value)
case int32:
return Int4(value), nil
*i = Int4(value)
case uint32:
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:
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 {
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:
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:
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 {
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:
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:
num, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return 0, err
return err
}
return Int4(num), nil
*i = Int4(num)
default:
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 {

View File

@ -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)
}
}
}

View File

@ -11,54 +11,56 @@ import (
type Int8 int64
func ConvertToInt8(src interface{}) (Int8, error) {
func (i *Int8) Convert(src interface{}) error {
switch value := src.(type) {
case Int8:
return value, nil
*i = value
case int8:
return Int8(value), nil
*i = Int8(value)
case uint8:
return Int8(value), nil
*i = Int8(value)
case int16:
return Int8(value), nil
*i = Int8(value)
case uint16:
return Int8(value), nil
*i = Int8(value)
case int32:
return Int8(value), nil
*i = Int8(value)
case uint32:
return Int8(value), nil
*i = Int8(value)
case int64:
return Int8(value), nil
*i = Int8(value)
case uint64:
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:
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 {
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:
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:
num, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, err
return err
}
return Int8(num), nil
*i = Int8(num)
default:
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 {

View File

@ -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)
}
}
}

View File

@ -1062,19 +1062,22 @@ func Encode(wbuf *WriteBuf, oid OID, arg interface{}) error {
switch oid {
case Int2OID:
i2, err := pgtype.ConvertToInt2(arg)
var i2 pgtype.Int2
err := i2.Convert(arg)
if err != nil {
return err
}
return i2.EncodeBinary(wbuf)
case Int4OID:
i4, err := pgtype.ConvertToInt4(arg)
var i4 pgtype.Int4
err := i4.Convert(arg)
if err != nil {
return err
}
return i4.EncodeBinary(wbuf)
case Int8OID:
i8, err := pgtype.ConvertToInt8(arg)
var i8 pgtype.Int8
err := i8.Convert(arg)
if err != nil {
return err
}