[gogf/gf]reflect: call of reflect.Value.NumField on ptr Value出现这个错误是什么意思啊?

2024-06-25 762 views
4

我向服务器提交的json数据为: [     {         "id":"65047",         "type":"001001",         "level":"003001",         "content":"gggg",         "answer":"B",         "analysis":"。",         "score":null,         "userAnswer":null,         "blanksNumber":null,         "signed":null,         "checkResult":null,         "userScore":null,         "tag":"008001",         "chapterName":null,         "inCollection":0,         "choices":[             {                 "id":"a10faef4722a427fb60cc17745622760",                 "sort":1,                 "code":"A",                 "content":"ff"             },             {                 "id":"4d1793240ab44a27b6d44dab6712c31a",                 "sort":2,                 "code":"B",                 "content":"dd"             },             {                 "id":"0ebf7e3b5d264ddfbe20acd30e7e0692",                 "sort":3,                 "code":"C",                 "content":"ss"             },             {                 "id":"d58ede4ad572462481f2742c3d974de3",                 "sort":4,                 "code":"D",                 "content":"rr"             }         ],         "keywords":null,         "questionclass":"ss",         "typeContentMd5":"d57a9fbae757a684cf420f7495c0e267"     } ] 服务器的代码为: request:=new(jj.Request) if err:=r.Parse(&request);err!nil{ if v,ok:=err.(*gvalid.Error); ok{ base.Fail(r,v.FirstString()) } } 但不知道反射为什么会出现这个错误呢? reflect: call of reflect.Value.NumField on ptr Value

回答

0

我也遇到了。当用户提交的数据格式与定义的struct结构不一样时,就会报错,通过err并拿不到错误,而是直接把程序给终止进行了,应该时bug。比如struct{a struct{b int}},当提交{"a": 1},直接就崩掉了

3

@writethesky 你的QQ号是多少

6

@joyoes 能否提供可复现问题的最小完整代码?

7

@writethesky 你的QQ号是多少

335678880

3

@writethesky 你的QQ号是多少

正常情况 image

刻意胡乱传参

image

直接崩掉了

这个api层代码(控制器) image

可以看到 63 行err并没有得到错误,err的值是nil,否则应该返回的是一个json,内容会提示参数错误。但是实际上直接就抛出错误了,下面把各个结构体的定义发一下

image image image

2

@writethesky 能给个最小化的完整可运行代码么?或者把截图的代码粘贴出来也行

8

@gqcn 我的复现问题的最小完整代码如下: 1 2 3 4 5

7

@writethesky 能给个最小化的完整可运行代码么?或者把截图的代码粘贴出来也行

api层代码(控制器)


type listRequst struct {
    qualitygoods.ListsInput
}

// Lists Lists
func (c *Scheduler) Lists(r *ghttp.Request) {
    var params *listRequst
    if err := r.GetStruct(&params); err != nil {
                // todo 这块引入了别的代码,测试时,可以改成直接http响应“参数错误”
        response.FailWithMessage(r, logicerror.ParamsError, err.Error())
        return
    }

         // todo 这块引入了别的代码,测试时,可以改成直接http响应“请求成功”
    response.SuccessWithData(r, list)
}

service层代码

// ListsInput ListsInput
type ListsInput struct {
    Page  int `v:"min:1#page不能小于1"`
    Limit int `v:"between:10,100#limit必须是10-100之间的整数"`
    quality_goods.Search
}

model层代码


// Search Search
type Search struct {
    GoodsName    string
    GoodsSource  types.SourceType
    FreeShipping types.FreeShippingType
    MarketPrice  struct {
        Min       int
        Max       int
        IsInclude bool
    }
    GuidePrice struct {
        Min       int
        Max       int
        IsInclude bool
    }
    AgreementPrice struct {
        Min       int
        Max       int
        IsInclude bool
    }
    NormalProfitMargin struct {
        Min       int
        Max       int
        IsInclude bool
    }
    ActivityProfitMargin struct {
        Min       int
        Max       int
        IsInclude bool
    }
    Sales struct {
        Cycle    types.CycleType
        Quantity struct {
            Min       int
            Max       int
            IsInclude bool
        }
    }
    SalesReturn struct {
        Cycle    types.CycleType
        Quantity struct {
            Min       int
            Max       int
            IsInclude bool
        }
    }
    ChooseGoods struct {
        Cycle    types.CycleType
        Quantity struct {
            Min       int
            Max       int
            IsInclude bool
        }
    }
}

因为该问题只涉及到结构体绑定解析,顾service、model层,不需要实际逻辑代码,只提供结构体定义即可。

postman测试参数(post请求、body传参、json类型)(正确)

{
    "page": 1,
    "limit": 10,
    "search": {
        "guidePrice": {
                      "min": 100,
                      "max": 1000
                 }
    }
}

postman测试参数(post请求、body传参、json类型)(错误,报错)

{
    "page": 1,
    "limit": 10,
    "search": {
        "guidePrice": 111
    }
}
3

@writethesky @joyoes 感谢两位的反馈,这里涉及到两件事。第一个是和 https://github.com/gogf/gf/issues/645 相关,我已回复。第二个是本身提交的数据格式和定义的struct结构严格无法匹配的,之前是panic报错,目前已改进为返回error。大家可以使用最新的master代码试试看还有无问题,如果问题继续存在请开新的issue提问,并提交可运行代码,截图的不要,以方便大家都节省时间。当前这个issue我便关闭掉了。