maputil.MapTo在定义的struct中有指针时报panic: reflect: call of reflect.Value.Interface on zero Value
ekoycut opened this issue · 2 comments
`
func Test_ToMap(t *testing.T) {
type M struct {
Name string json:"name"
}
type S struct {
ID int json:"id"
M *M json:"m"
}
s := &S{
ID: 1,
}
maps, err := structs.ToMap(s)
assert.Nil(t, err)
t.Log(maps)
}
`
`
== RUN Test_ToMap
--- FAIL: Test_ToMap (0.00s)
panic: reflect: call of reflect.Value.Interface on zero Value [recovered]
panic: reflect: call of reflect.Value.Interface on zero Value
goroutine 14 [running]:
testing.tRunner.func1.2({0x18faf80, 0xc0001c6288})
/usr/local/go1.21.5/go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
/usr/local/go1.21.5/go/src/testing/testing.go:1548 +0x397
panic({0x18faf80?, 0xc0001c6288?})
/usr/local/go1.21.5/go/src/runtime/panic.go:914 +0x21f
reflect.valueInterface({0x0?, 0x0?, 0x0?}, 0x0?)
/usr/local/go1.21.5/go/src/reflect/value.go:1500 +0xfb
reflect.Value.Interface(...)
/usr/local/go1.21.5/go/src/reflect/value.go:1495
github.com/duke-git/lancet/v2/pointer.ExtractPointer({0x17f6c00, 0x0})
/root/go/pkg/mod/github.com/duke-git/lancet/v2@v2.2.8/pointer/pointer.go:50 +0xe7
github.com/duke-git/lancet/v2/structs.(*Field).mapValue(0xc0007b4600, {0x17f6c00?, 0x0?})
/root/go/pkg/mod/github.com/duke-git/lancet/v2@v2.2.8/structs/field.go:73 +0x3d
github.com/duke-git/lancet/v2/structs.(*Struct).ToMap(0xc000b02190)
/root/go/pkg/mod/github.com/duke-git/lancet/v2@v2.2.8/structs/struct.go:76 +0x1b5
github.com/duke-git/lancet/v2/structs.ToMap({0x17f6c40?, 0xc00079e670?})
/root/go/pkg/mod/github.com/duke-git/lancet/v2@v2.2.8/structs/struct.go:116 +0x2a
git.woa.com/svip/vdesk/im_logic/logics.Test_ToMap(0xc000007a00)
/data/develop/svip/vdesk/im_logic/logics/message_logic_test.go:72 +0x3b
testing.tRunner(0xc000007a00, 0x1d894c8)
/usr/local/go1.21.5/go/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 1
/usr/local/go1.21.5/go/src/testing/testing.go:1648 +0x3ad
FAIL git.woa.com/svip/vdesk/im_logic/logics 0.038s
`
需要对struct中有指针的情况做处理
v2.2.9已修复