处理 string 和 int, converting NULL to string is unsupported
This commit is contained in:
+8
-2
@@ -70,9 +70,15 @@ func (s *scanner) ScanStruct(i interface{}) error {
|
|||||||
scans := make([]interface{}, 0, len(s.columns))
|
scans := make([]interface{}, 0, len(s.columns))
|
||||||
for _, col := range s.columns {
|
for _, col := range s.columns {
|
||||||
data, ok := s.columnMap[col]
|
data, ok := s.columnMap[col]
|
||||||
switch {
|
|
||||||
case !ok:
|
if !ok {
|
||||||
return unableToFindFieldError(col)
|
return unableToFindFieldError(col)
|
||||||
|
}
|
||||||
|
// 处理 converting NULL to string is unsupported
|
||||||
|
// 前面将 string 和 int 类型转为了 *string 和 *int
|
||||||
|
switch data.GoType.Kind() {
|
||||||
|
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
scans = append(scans, reflect.New(reflect.PointerTo(data.GoType)).Interface())
|
||||||
default:
|
default:
|
||||||
scans = append(scans, reflect.New(data.GoType).Interface())
|
scans = append(scans, reflect.New(data.GoType).Interface())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,8 +167,15 @@ func SafeSetFieldByIndex(v reflect.Value, fieldIndex []int, src interface{}) (re
|
|||||||
return v
|
return v
|
||||||
case 1:
|
case 1:
|
||||||
f := v.FieldByIndex(fieldIndex)
|
f := v.FieldByIndex(fieldIndex)
|
||||||
srcVal := reflect.ValueOf(src)
|
srcVal := reflect.ValueOf(src).Elem()
|
||||||
f.Set(reflect.Indirect(srcVal))
|
// 处理 converting NULL to string is unsupported
|
||||||
|
// 前面将 string 和 int 类型转为了 *string 和 *int
|
||||||
|
// 这里做还原
|
||||||
|
if f.Type().ConvertibleTo(srcVal.Type().Elem()) {
|
||||||
|
f.Set(srcVal.Elem())
|
||||||
|
} else {
|
||||||
|
f.Set(srcVal)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
f := v.Field(fieldIndex[0])
|
f := v.Field(fieldIndex[0])
|
||||||
switch f.Kind() {
|
switch f.Kind() {
|
||||||
|
|||||||
Reference in New Issue
Block a user