Skip to content

修复gdb.Result或gdb.Record这两个类型多层结构体转换的bug #4747

@JB-fy

Description

@JB-fy

Go version

1.25

GoFrame version

2.10.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

1、顶层gdb.Result或gdb.Record使用list.Structs(&res.List)或info.Struct(&res.Info)转换时,内嵌的gdb.Result或gdb.Record转结构体时会失败,必须先转成json后方可成功,即使这样,内嵌的gdb.Result或gdb.Record内有字段是浮点数字符串时,该字段无法转换成float类型,都变成0值
2、顶层改用gconv.Structs(list.List(), &res.List)或gconv.Struct(info.Map(), &res.Info)做转换可解决上面BUG,可list.List()或info.Map()内如果还有gdb.Result或gdb.Record字段,其内嵌gdb.Result或gdb.Record时,依然重复上面BUG

type OrderBuyTrackInfo struct {
BuyTrackId *uint json:"buy_track_id,omitempty" dc:"采购物流ID"
BuyTrackName *string json:"buy_track_name,omitempty" dc:"采购物流名称"
BuyTrackWeight *float64 json:"buy_track_weight,omitempty" dc:"重量。单位:千克"
}
buyTrackList := gdb.Result{{
buy_track_id: gvar.New(1),
buy_track_name: gvar.New(1111),
buy_track_weight: gvar.New(2.5),
}}
type OrderTrackInfo struct {
TrackId *uint json:"track_id,omitempty" dc:"物流ID"
TrackName *string json:"track_name,omitempty" dc:"物流名称"
TrackWeight *float64 json:"track_weight,omitempty" dc:"重量。单位:千克"
BuyTrackList []OrderBuyTrackInfo json:"buy_track_list,omitempty" dc:"采购物流列表"
}
trackList := gdb.Result{{
track_id: gvar.New(2),
track_name: gvar.New(2222),
track_weight: gvar.New(3.5),
buy_track_list: gvar.New(buyTrackList), //上层依然使用gvar.New(trackList),这里改成gvar.New(gjson.MustEncode(buyTrackList)),这里正常,但track_weight数据丢失
}}
type OrderItemInfo struct {
OrderItemId *uint json:"order_item_id,omitempty" dc:"订单商品ID"
TrackList []OrderTrackInfo json:"track_list,omitempty" dc:"物流信息列表"
}
itemInfo := gdb.Record{
order_item_id: gvar.New(1),
track_list: gvar.New(trackList), //改成gvar.New(gjson.MustEncode(trackList))后,itemInfo.Struct(&itemInfoS)正常,但track_weight数据丢失
}
var itemInfoS OrderItemInfo
itemInfo.Struct(&itemInfoS)
gutil.Dump(itemInfoS)

gconv.Struct(itemInfo.Map(), &itemInfoS)
itemInfo.Struct(&itemInfoS)
gutil.Dump(itemInfoS)

itemList := gdb.Result{itemInfo}
var itemListS []OrderItemInfo
itemList.Structs(&itemListS)
gutil.Dump(itemListS)

gconv.Struct(itemList.List(), &itemListS)
itemInfo.Struct(&itemListS)
gutil.Dump(itemListS)

What did you see happen?

Image

What did you expect to see?

修复gdb.Result或gdb.Record这两个类型多层结构体转换的bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIt is confirmed a bug, but don't worry, we'll handle it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions