[fix] struct 中存在slice字段会报错误
This commit is contained in:
parent
bf3032103e
commit
28d9dcbd4d
11
scan.go
11
scan.go
@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -128,7 +129,7 @@ func sStructSlice(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
|
|
||||||
// json 字符串 反序列化
|
// json 字符串 反序列化
|
||||||
for i, rv := range jFields {
|
for i, rv := range jFields {
|
||||||
v := rv.Interface()
|
v := rv.Addr().Interface()
|
||||||
data := *scanArgs[i].(*[]uint8)
|
data := *scanArgs[i].(*[]uint8)
|
||||||
|
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
@ -138,8 +139,6 @@ func sStructSlice(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
if err := json.Unmarshal(data, &v); err != nil {
|
if err := json.Unmarshal(data, &v); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rv.Set(reflect.ValueOf(v))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if itemIsPtr {
|
if itemIsPtr {
|
||||||
@ -186,6 +185,8 @@ func sStruct(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
case reflect.Slice, reflect.Struct, reflect.Map:
|
case reflect.Slice, reflect.Struct, reflect.Map:
|
||||||
scanArgs[i] = &[]uint8{}
|
scanArgs[i] = &[]uint8{}
|
||||||
jFields[i] = v.Field(f)
|
jFields[i] = v.Field(f)
|
||||||
|
|
||||||
|
fmt.Printf("123 %+#v \n", v.Field(f))
|
||||||
default:
|
default:
|
||||||
scanArgs[i] = v.Field(f).Addr().Interface()
|
scanArgs[i] = v.Field(f).Addr().Interface()
|
||||||
}
|
}
|
||||||
@ -200,7 +201,7 @@ func sStruct(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
|
|
||||||
// json 字符串 反序列化
|
// json 字符串 反序列化
|
||||||
for i, rv := range jFields {
|
for i, rv := range jFields {
|
||||||
v := rv.Interface()
|
v := rv.Addr().Interface()
|
||||||
data := *scanArgs[i].(*[]uint8)
|
data := *scanArgs[i].(*[]uint8)
|
||||||
|
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
@ -210,8 +211,6 @@ func sStruct(rows *sql.Rows, dest any) (result RowScan) {
|
|||||||
if err := json.Unmarshal(data, &v); err != nil {
|
if err := json.Unmarshal(data, &v); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rv.Set(reflect.ValueOf(v))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if realDest.Kind() == reflect.Ptr {
|
if realDest.Kind() == reflect.Ptr {
|
||||||
|
@ -67,9 +67,10 @@ func TestScanJsonFieldStruct(t *testing.T) {
|
|||||||
Id int `db:"id"`
|
Id int `db:"id"`
|
||||||
Code string `db:"code"`
|
Code string `db:"code"`
|
||||||
Map map[string]any `db:"map"`
|
Map map[string]any `db:"map"`
|
||||||
|
Arr []int64 `db:"arr"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
myConn.Select(`select 0 as id, 'test' as code, '{"a":1}' as map`, []any{}, &dest)
|
myConn.Select(`select 0 as id, 'test' as code, '{"a":1}' as map, '[3, 4]' as arr`, []any{}, &dest)
|
||||||
|
|
||||||
t.Log(dest)
|
t.Log(dest)
|
||||||
}
|
}
|
||||||
@ -92,9 +93,10 @@ func TestScanJsonFieldStructSlice(t *testing.T) {
|
|||||||
Id int `db:"id"`
|
Id int `db:"id"`
|
||||||
Code string `db:"code"`
|
Code string `db:"code"`
|
||||||
Map map[string]any `db:"map"`
|
Map map[string]any `db:"map"`
|
||||||
|
Arr []any `db:"arr"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
myConn.Select(`select * from ((select 0 as id, 'test' as code, '{"a":1}' as map) union (select 1 as id, 'test1' as code, '{"a":2}' as map)) as t`, []any{}, &dest)
|
myConn.Select(`select * from ((select 0 as id, 'test' as code, '{"a":1}' as map, '[1, 2]' as arr) union (select 1 as id, 'test1' as code, '{"a":2}' as map, '[1, 2]' as arr)) as t`, []any{}, &dest)
|
||||||
|
|
||||||
t.Log(dest)
|
t.Log(dest)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user