mirror of https://github.com/jackc/pgx.git
Refactor to interface convert
parent
0977e29341
commit
5bca076182
20
convert.go
20
convert.go
|
@ -389,6 +389,11 @@ func NullAssignTo(dst interface{}) error {
|
||||||
|
|
||||||
var kindTypes map[reflect.Kind]reflect.Type
|
var kindTypes map[reflect.Kind]reflect.Type
|
||||||
|
|
||||||
|
func toInterface(dst reflect.Value, t reflect.Type) (interface{}, bool) {
|
||||||
|
nextDst := dst.Convert(t)
|
||||||
|
return nextDst.Interface(), dst.Type() != nextDst.Type()
|
||||||
|
}
|
||||||
|
|
||||||
// GetAssignToDstType attempts to convert dst to something AssignTo can assign
|
// GetAssignToDstType attempts to convert dst to something AssignTo can assign
|
||||||
// to. If dst is a pointer to pointer it allocates a value and returns the
|
// to. If dst is a pointer to pointer it allocates a value and returns the
|
||||||
// dereferences pointer. If dst is a named type such as *Foo where Foo is type
|
// dereferences pointer. If dst is a named type such as *Foo where Foo is type
|
||||||
|
@ -414,23 +419,18 @@ func GetAssignToDstType(dst interface{}) (interface{}, bool) {
|
||||||
|
|
||||||
// if dst is pointer to a base type that has been renamed
|
// if dst is pointer to a base type that has been renamed
|
||||||
if baseValType, ok := kindTypes[dstVal.Kind()]; ok {
|
if baseValType, ok := kindTypes[dstVal.Kind()]; ok {
|
||||||
nextDst := dstPtr.Convert(reflect.PtrTo(baseValType))
|
return toInterface(dstPtr, reflect.PtrTo(baseValType))
|
||||||
return nextDst.Interface(), dstPtr.Type() != nextDst.Type()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if dstVal.Kind() == reflect.Slice {
|
if dstVal.Kind() == reflect.Slice {
|
||||||
if baseElemType, ok := kindTypes[dstVal.Type().Elem().Kind()]; ok {
|
if baseElemType, ok := kindTypes[dstVal.Type().Elem().Kind()]; ok {
|
||||||
baseSliceType := reflect.PtrTo(reflect.SliceOf(baseElemType))
|
return toInterface(dstPtr, reflect.PtrTo(reflect.SliceOf(baseElemType)))
|
||||||
nextDst := dstPtr.Convert(baseSliceType)
|
|
||||||
return nextDst.Interface(), dstPtr.Type() != nextDst.Type()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dstVal.Kind() == reflect.Array {
|
if dstVal.Kind() == reflect.Array {
|
||||||
if baseElemType, ok := kindTypes[dstVal.Type().Elem().Kind()]; ok {
|
if baseElemType, ok := kindTypes[dstVal.Type().Elem().Kind()]; ok {
|
||||||
baseArrayType := reflect.PtrTo(reflect.ArrayOf(dstVal.Len(), baseElemType))
|
return toInterface(dstPtr, reflect.PtrTo(reflect.ArrayOf(dstVal.Len(), baseElemType)))
|
||||||
nextDst := dstPtr.Convert(baseArrayType)
|
|
||||||
return nextDst.Interface(), dstPtr.Type() != nextDst.Type()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,9 +440,7 @@ func GetAssignToDstType(dst interface{}) (interface{}, bool) {
|
||||||
nested := dstVal.Type().Field(0).Type
|
nested := dstVal.Type().Field(0).Type
|
||||||
if nested.Kind() == reflect.Array {
|
if nested.Kind() == reflect.Array {
|
||||||
if baseElemType, ok := kindTypes[nested.Elem().Kind()]; ok {
|
if baseElemType, ok := kindTypes[nested.Elem().Kind()]; ok {
|
||||||
baseArrayType := reflect.PtrTo(reflect.ArrayOf(nested.Len(), baseElemType))
|
return toInterface(dstPtr, reflect.PtrTo(reflect.ArrayOf(nested.Len(), baseElemType)))
|
||||||
nextDst := dstPtr.Convert(baseArrayType)
|
|
||||||
return nextDst.Interface(), dstPtr.Type() != nextDst.Type()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, ok := kindTypes[nested.Kind()]; ok && dstPtr.CanInterface() {
|
if _, ok := kindTypes[nested.Kind()]; ok && dstPtr.CanInterface() {
|
||||||
|
|
Loading…
Reference in New Issue