Move "char" to pgtype

non-blocking
Jack Christensen 2017-03-07 19:39:57 -06:00
parent 94612427ed
commit eea6e5a64c
3 changed files with 299 additions and 7 deletions

View File

@ -74,12 +74,15 @@ func (f forceBinaryEncoder) EncodeBinary(w io.Writer) error {
func forceEncoder(e interface{}, formatCode int16) interface{} {
switch formatCode {
case pgx.TextFormatCode:
return forceTextEncoder{e: e.(pgtype.TextEncoder)}
if e, ok := e.(pgtype.TextEncoder); ok {
return forceTextEncoder{e: e}
}
case pgx.BinaryFormatCode:
return forceBinaryEncoder{e: e.(pgtype.BinaryEncoder)}
default:
panic("bad encoder")
if e, ok := e.(pgtype.BinaryEncoder); ok {
return forceBinaryEncoder{e: e.(pgtype.BinaryEncoder)}
}
}
return nil
}
func testSuccessfulTranscode(t testing.TB, pgTypeName string, values []interface{}) {
@ -105,9 +108,14 @@ func testSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []int
{name: "BinaryFormat", formatCode: pgx.BinaryFormatCode},
}
for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode
for i, v := range values {
for i, v := range values {
for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(v, fc.formatCode)
if vEncoder == nil {
t.Logf("%v does not implement %v", fc.name)
continue
}
// Derefence value if it is a pointer
derefV := v
refVal := reflect.ValueOf(v)

144
qchar.go Normal file
View File

@ -0,0 +1,144 @@
package pgtype
import (
"fmt"
"io"
"math"
"strconv"
"github.com/jackc/pgx/pgio"
)
// QChar is for PostgreSQL's special 8-bit-only "char" type more akin to the C
// language's char type, or Go's byte type. (Note that the name in PostgreSQL
// itself is "char", in double-quotes, and not char.) It gets used a lot in
// PostgreSQL's system tables to hold a single ASCII character value (eg
// pg_class.relkind). It is named Qchar for quoted char to disambiguate from SQL
// standard type char.
//
// Not all possible values of QChar are representable in the text format.
// Therefore, QChar does not implement TextEncoder and TextDecoder.
type QChar struct {
Int int8
Status Status
}
func (dst *QChar) ConvertFrom(src interface{}) error {
switch value := src.(type) {
case QChar:
*dst = value
case int8:
*dst = QChar{Int: value, Status: Present}
case uint8:
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int16:
if value < math.MinInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint16:
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int32:
if value < math.MinInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint32:
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int64:
if value < math.MinInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint64:
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case int:
if value < math.MinInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case uint:
if value > math.MaxInt8 {
return fmt.Errorf("%d is greater than maximum value for QChar", value)
}
*dst = QChar{Int: int8(value), Status: Present}
case string:
num, err := strconv.ParseInt(value, 10, 8)
if err != nil {
return err
}
*dst = QChar{Int: int8(num), Status: Present}
default:
if originalSrc, ok := underlyingNumberType(src); ok {
return dst.ConvertFrom(originalSrc)
}
return fmt.Errorf("cannot convert %v to QChar", value)
}
return nil
}
func (src *QChar) AssignTo(dst interface{}) error {
return int64AssignTo(int64(src.Int), src.Status, dst)
}
func (dst *QChar) DecodeBinary(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {
return err
}
if size == -1 {
*dst = QChar{Status: Null}
return nil
}
if size != 1 {
return fmt.Errorf(`invalid length for "char": %v`, size)
}
byt, err := pgio.ReadByte(r)
if err != nil {
return err
}
*dst = QChar{Int: int8(byt), Status: Present}
return nil
}
func (src QChar) EncodeBinary(w io.Writer) error {
if done, err := encodeNotPresent(w, src.Status); done {
return err
}
_, err := pgio.WriteInt32(w, 1)
if err != nil {
return nil
}
return pgio.WriteByte(w, byte(src.Int))
}

140
qchar_test.go Normal file
View File

@ -0,0 +1,140 @@
package pgtype_test
import (
"math"
"reflect"
"testing"
"github.com/jackc/pgx/pgtype"
)
func TestQCharTranscode(t *testing.T) {
testSuccessfulTranscode(t, `"char"`, []interface{}{
pgtype.QChar{Int: math.MinInt8, Status: pgtype.Present},
pgtype.QChar{Int: -1, Status: pgtype.Present},
pgtype.QChar{Int: 0, Status: pgtype.Present},
pgtype.QChar{Int: 1, Status: pgtype.Present},
pgtype.QChar{Int: math.MaxInt8, Status: pgtype.Present},
pgtype.QChar{Int: 0, Status: pgtype.Null},
})
}
func TestQCharConvertFrom(t *testing.T) {
successfulTests := []struct {
source interface{}
result pgtype.QChar
}{
{source: int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: int16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: int32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: int64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: int8(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}},
{source: int16(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}},
{source: int32(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}},
{source: int64(-1), result: pgtype.QChar{Int: -1, Status: pgtype.Present}},
{source: uint8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: uint16(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: uint32(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: uint64(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: "1", result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
{source: _int8(1), result: pgtype.QChar{Int: 1, Status: pgtype.Present}},
}
for i, tt := range successfulTests {
var r pgtype.QChar
err := r.ConvertFrom(tt.source)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}
func TestQCharAssignTo(t *testing.T) {
var i8 int8
var i16 int16
var i32 int32
var i64 int64
var i int
var ui8 uint8
var ui16 uint16
var ui32 uint32
var ui64 uint64
var ui uint
var pi8 *int8
var _i8 _int8
var _pi8 *_int8
simpleTests := []struct {
src pgtype.QChar
dst interface{}
expected interface{}
}{
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)},
{src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))},
{src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))},
}
for i, tt := range simpleTests {
err := tt.src.AssignTo(tt.dst)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
}
}
pointerAllocTests := []struct {
src pgtype.QChar
dst interface{}
expected interface{}
}{
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)},
{src: pgtype.QChar{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)},
}
for i, tt := range pointerAllocTests {
err := tt.src.AssignTo(tt.dst)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
}
}
errorTests := []struct {
src pgtype.QChar
dst interface{}
}{
{src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui8},
{src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui16},
{src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui32},
{src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui64},
{src: pgtype.QChar{Int: -1, Status: pgtype.Present}, dst: &ui},
{src: pgtype.QChar{Int: 0, Status: pgtype.Null}, dst: &i16},
}
for i, tt := range errorTests {
err := tt.src.AssignTo(tt.dst)
if err == nil {
t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
}
}
}