mirror of https://github.com/jackc/pgx.git
parent
9e5d81d8f5
commit
5b861d0a5f
|
@ -50,10 +50,7 @@ func (src *Date) AssignTo(dst interface{}) error {
|
||||||
// if dst is a pointer to pointer, strip the pointer and try again
|
// if dst is a pointer to pointer, strip the pointer and try again
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if src.Status == Null {
|
if src.Status == Null {
|
||||||
if !el.IsNil() {
|
|
||||||
// if the destination pointer is not nil, nil it out
|
|
||||||
el.Set(reflect.Zero(el.Type()))
|
el.Set(reflect.Zero(el.Type()))
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if el.IsNil() {
|
if el.IsNil() {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pgtype_test
|
package pgtype_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ func TestDateConvertFrom(t *testing.T) {
|
||||||
source interface{}
|
source interface{}
|
||||||
result pgtype.Date
|
result pgtype.Date
|
||||||
}{
|
}{
|
||||||
|
{source: pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, result: pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
||||||
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
||||||
{source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
{source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
||||||
{source: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
{source: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
|
||||||
|
@ -49,3 +51,47 @@ func TestDateConvertFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDateAssignTo(t *testing.T) {
|
||||||
|
var tim time.Time
|
||||||
|
var ptim *time.Time
|
||||||
|
|
||||||
|
simpleTests := []struct {
|
||||||
|
src pgtype.Date
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
|
||||||
|
{src: pgtype.Date{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(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.Date
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Date{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pgtype_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/pgtype"
|
"github.com/jackc/pgx/pgtype"
|
||||||
|
@ -19,8 +20,6 @@ func TestInt4Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInt4ConvertFrom(t *testing.T) {
|
func TestInt4ConvertFrom(t *testing.T) {
|
||||||
type _int8 int8
|
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
result pgtype.Int4
|
result pgtype.Int4
|
||||||
|
@ -53,3 +52,70 @@ func TestInt4ConvertFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInt4AssignTo(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.Int4
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)},
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)},
|
||||||
|
{src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))},
|
||||||
|
{src: pgtype.Int4{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.Int4
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)},
|
||||||
|
{src: pgtype.Int4{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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pgtype_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/pgtype"
|
"github.com/jackc/pgx/pgtype"
|
||||||
|
@ -19,8 +20,6 @@ func TestInt8Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInt8ConvertFrom(t *testing.T) {
|
func TestInt8ConvertFrom(t *testing.T) {
|
||||||
type _int8 int8
|
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
result pgtype.Int8
|
result pgtype.Int8
|
||||||
|
@ -53,3 +52,70 @@ func TestInt8ConvertFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInt8AssignTo(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.Int8
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)},
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)},
|
||||||
|
{src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))},
|
||||||
|
{src: pgtype.Int8{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.Int8
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)},
|
||||||
|
{src: pgtype.Int8{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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pgtype_test
|
package pgtype_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -37,6 +38,7 @@ func TestTimestamptzConvertFrom(t *testing.T) {
|
||||||
source interface{}
|
source interface{}
|
||||||
result pgtype.Timestamptz
|
result pgtype.Timestamptz
|
||||||
}{
|
}{
|
||||||
|
{source: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, result: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
|
||||||
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
|
{source: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
|
||||||
{source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
|
{source: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}},
|
||||||
{source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), Status: pgtype.Present}},
|
{source: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), result: pgtype.Timestamptz{Time: time.Date(1999, 12, 31, 12, 59, 59, 0, time.Local), Status: pgtype.Present}},
|
||||||
|
@ -58,3 +60,47 @@ func TestTimestamptzConvertFrom(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTimestamptzAssignTo(t *testing.T) {
|
||||||
|
var tim time.Time
|
||||||
|
var ptim *time.Time
|
||||||
|
|
||||||
|
simpleTests := []struct {
|
||||||
|
src pgtype.Timestamptz
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &tim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
|
||||||
|
{src: pgtype.Timestamptz{Time: time.Time{}, Status: pgtype.Null}, dst: &ptim, expected: ((*time.Time)(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.Timestamptz
|
||||||
|
dst interface{}
|
||||||
|
expected interface{}
|
||||||
|
}{
|
||||||
|
{src: pgtype.Timestamptz{Time: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, dst: &ptim, expected: time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local)},
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue