mirror of https://github.com/jackc/pgx.git
Merge branch 'ncw/uuid-convs' of https://github.com/NWilson/pgx into NWilson-ncw/uuid-convs
commit
05597c2155
|
@ -149,7 +149,7 @@ func underlyingTimeType(val interface{}) (interface{}, bool) {
|
||||||
switch refVal.Kind() {
|
switch refVal.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if refVal.IsNil() {
|
if refVal.IsNil() {
|
||||||
return time.Time{}, false
|
return nil, false
|
||||||
}
|
}
|
||||||
convVal := refVal.Elem().Interface()
|
convVal := refVal.Elem().Interface()
|
||||||
return convVal, true
|
return convVal, true
|
||||||
|
@ -160,8 +160,29 @@ func underlyingTimeType(val interface{}) (interface{}, bool) {
|
||||||
return refVal.Convert(timeType).Interface(), true
|
return refVal.Convert(timeType).Interface(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// underlyingUUIDType gets the underlying type that can be converted to [16]byte
|
||||||
|
func underlyingUUIDType(val interface{}) (interface{}, bool) {
|
||||||
|
refVal := reflect.ValueOf(val)
|
||||||
|
|
||||||
|
switch refVal.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
if refVal.IsNil() {
|
||||||
return time.Time{}, false
|
return time.Time{}, false
|
||||||
}
|
}
|
||||||
|
convVal := refVal.Elem().Interface()
|
||||||
|
return convVal, true
|
||||||
|
}
|
||||||
|
|
||||||
|
uuidType := reflect.TypeOf([16]byte{})
|
||||||
|
if refVal.Type().ConvertibleTo(uuidType) {
|
||||||
|
return refVal.Convert(uuidType).Interface(), true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
// underlyingSliceType gets the underlying slice type
|
// underlyingSliceType gets the underlying slice type
|
||||||
func underlyingSliceType(val interface{}) (interface{}, bool) {
|
func underlyingSliceType(val interface{}) (interface{}, bool) {
|
||||||
|
@ -401,6 +422,14 @@ func GetAssignToDstType(dst interface{}) (interface{}, bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dstVal.Kind() == reflect.Array {
|
||||||
|
if baseElemType, ok := kindTypes[dstVal.Type().Elem().Kind()]; ok {
|
||||||
|
baseArrayType := reflect.PtrTo(reflect.ArrayOf(dstVal.Len(), baseElemType))
|
||||||
|
nextDst := dstPtr.Convert(baseArrayType)
|
||||||
|
return nextDst.Interface(), dstPtr.Type() != nextDst.Type()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (dst *UUID) Set(src interface{}) error {
|
||||||
}
|
}
|
||||||
*dst = UUID{Bytes: uuid, Status: Present}
|
*dst = UUID{Bytes: uuid, Status: Present}
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingPtrType(src); ok {
|
if originalSrc, ok := underlyingUUIDType(src); ok {
|
||||||
return dst.Set(originalSrc)
|
return dst.Set(originalSrc)
|
||||||
}
|
}
|
||||||
return errors.Errorf("cannot convert %v to UUID", value)
|
return errors.Errorf("cannot convert %v to UUID", value)
|
||||||
|
|
|
@ -15,6 +15,8 @@ func TestUUIDTranscode(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SomeUUIDType [16]byte
|
||||||
|
|
||||||
func TestUUIDSet(t *testing.T) {
|
func TestUUIDSet(t *testing.T) {
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
|
@ -32,6 +34,10 @@ func TestUUIDSet(t *testing.T) {
|
||||||
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||||
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
source: SomeUUIDType{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||||
|
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
source: ([]byte)(nil),
|
source: ([]byte)(nil),
|
||||||
result: pgtype.UUID{Status: pgtype.Null},
|
result: pgtype.UUID{Status: pgtype.Null},
|
||||||
|
@ -86,6 +92,21 @@ func TestUUIDAssignTo(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
|
var dst SomeUUIDType
|
||||||
|
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||||
|
|
||||||
|
err := src.AssignTo(&dst)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dst != expected {
|
||||||
|
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst string
|
var dst string
|
||||||
|
|
Loading…
Reference in New Issue