Skip to content

Commit 102c3b6

Browse files
authored
fix(util/gconv): fix incompatable converting to nil pointer target from older version implement (#4224)
fixed: #4218
1 parent 5e677a1 commit 102c3b6

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

util/gconv/gconv_z_unit_issue_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,41 @@ func Test_Issue3903(t *testing.T) {
806806
})
807807
}
808808

809+
// https://github.com/gogf/gf/issues/4218
810+
func Test_Issue4218(t *testing.T) {
811+
gtest.C(t, func(t *gtest.T) {
812+
type SysMenuVo struct {
813+
MenuId int64 `json:"menuId" orm:"menu_id"`
814+
MenuName string `json:"menuName" orm:"menu_name"`
815+
Children []*SysMenuVo `json:"children" orm:"children"`
816+
ParentId int64 `json:"parentId" orm:"parent_id"`
817+
}
818+
menus := []*SysMenuVo{
819+
{
820+
MenuId: 1,
821+
MenuName: "系统管理",
822+
ParentId: 0,
823+
},
824+
{
825+
MenuId: 2,
826+
MenuName: "字典查询",
827+
ParentId: 1,
828+
},
829+
}
830+
var parent *SysMenuVo
831+
err := gconv.Scan(menus[0], &parent)
832+
t.AssertNil(err)
833+
t.Assert(parent.MenuId, 1)
834+
t.Assert(parent.ParentId, 0)
835+
836+
parent.Children = append(parent.Children, menus[1])
837+
838+
t.Assert(len(menus[0].Children), 1)
839+
t.Assert(menus[0].Children[0].MenuId, 2)
840+
t.Assert(menus[0].Children[0].ParentId, 1)
841+
})
842+
}
843+
809844
// https://github.com/gogf/gf/issues/4542
810845
func Test_Issue4542(t *testing.T) {
811846
// Test case 1: Nested map conversion - map[string]any to map[string]map[string]float64

util/gconv/internal/converter/converter_scan.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ func (c *Converter) Scan(srcValue any, dstPointer any, option ...ScanOption) (er
9696
}
9797

9898
// Get the element type and kind of dstPointer
99-
var (
100-
dstPointerReflectValueElem = dstPointerReflectValue.Elem()
101-
dstPointerReflectValueElemKind = dstPointerReflectValueElem.Kind()
102-
)
99+
var dstPointerReflectValueElem = dstPointerReflectValue.Elem()
100+
// Check if srcValue and dstPointer are the same type, in which case direct assignment can be performed
101+
if ok := c.doConvertWithTypeCheck(srcValueReflectValue, dstPointerReflectValueElem); ok {
102+
return nil
103+
}
104+
103105
// Handle multiple level pointers
106+
var dstPointerReflectValueElemKind = dstPointerReflectValueElem.Kind()
104107
if dstPointerReflectValueElemKind == reflect.Pointer {
105108
if dstPointerReflectValueElem.IsNil() {
106109
// Create a new value for the pointer dereference
@@ -114,11 +117,6 @@ func (c *Converter) Scan(srcValue any, dstPointer any, option ...ScanOption) (er
114117
return c.Scan(srcValueReflectValue, dstPointerReflectValueElem, option...)
115118
}
116119

117-
// Check if srcValue and dstPointer are the same type, in which case direct assignment can be performed
118-
if ok := c.doConvertWithTypeCheck(srcValueReflectValue, dstPointerReflectValueElem); ok {
119-
return nil
120-
}
121-
122120
scanOption := c.getScanOption(option...)
123121
// Handle different destination types
124122
switch dstPointerReflectValueElemKind {

0 commit comments

Comments
 (0)