使用反射来调用具有零参数的函数会导致“使用零值参数调用”恐慌 [英] Using reflection to call a function with a nil parameter results in a "Call using zero Value argument" panic

查看:657
本文介绍了使用反射来调用具有零参数的函数会导致“使用零值参数调用”恐慌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试一个接受类型为error的参数的函数。



然而,当我尝试使用 reflect.Call nil 值(它可以被传递给一个接受某种类型为error的函数),它似乎导致一个恐慌,并带有以下消息:

  reflect:使用零值调用值参数

我发现了以下帖子,但是我没有将它集成到我的函数中。





相关Go游乐场: http://play.g olang.org/p/cYQMQ6amPH



在上面的操场中,我希望调用 InvocationCausedPanic(PanicOnErr ,nil)返回 false ,但是,上述来自反映的恐慌导致误报。



是否可以对 InvocationCausedPanic invoke 函数进行任何修改以使其工作(同时保留其测试不能接受 nil 作为参数的其他函数的能力 - 例如接受字符串的函数)?

也许问题出在如何调用函数?



我已经提供了像 InvocationCausedPanic(PanicOnErr,new(error) ) InvocationCausedPanic(PanicOnErr,error(nil))无济于事。 感谢您的任何建议。

解决方案

如果参数值为零,则使用函数参数类型的零值。

  if paramValue == nil {
reflectParams [paramIndex] = reflect.New(expectedType).Elem()
} else {
reflectParams [paramIndex] = reflect.ValueOf(paramValue)
}

操场



如果计算反射值然后检查可分配性,则可以简化代码。通过这种改变,不需要兼容函数。

 对于paramIndex ,paramValue:= range params {
如果paramValue == nil {
reflectParams [paramIndex] = reflect.New(expectedType).Elem()
} else {
reflectParams [paramIndex ] = reflect.ValueOf(paramValue)
}
expectedType:= funcType.In(paramIndex)
actualType:= reflectParams [paramIndex] .Type()
if!actualType.AssignableTo (expectedType){
errStr:= fmt.Sprintf(InvocationCausedPanic使用不匹配的参数类型调用[parameter#%v:expected%v; got%v]。,paramIndex,expectedType,actualType)
恐慌(errStr)
}
}


I'm trying to test a function that accepts a parameter of the type "error". The function should panic under certain situations and I'm trying to test scenarios.

However, when I try to use reflect.Call on a nil value (which can be passed into a function that accepts something of type "error"), it seems to result in a panic with the following message:

reflect: Call using zero Value argument

I have found the following posts but I am failing to integrate it into my function.

Relevant Go Playground: http://play.golang.org/p/cYQMQ6amPH

In the playground above, I would like the call to InvocationCausedPanic(PanicOnErr, nil) to return false, however, the above panic from reflect is causing a false positive.

Are there any modifications I can make to the InvocationCausedPanic or the invoke functions to make this work (while retaining its ability to test other functions that cannot accept nil as an argument - a function that accepts a string for instance)?

Perhaps the issue is in how the function is called?

I've tred things like InvocationCausedPanic(PanicOnErr, new(error)) or InvocationCausedPanic(PanicOnErr, error(nil)) to no avail.

Thanks for any advice.

解决方案

If the parameter value is nil, then use a zero value of the function parameter type.

    if paramValue == nil {
        reflectedParams[paramIndex] = reflect.New(expectedType).Elem()
    } else {
        reflectedParams[paramIndex] = reflect.ValueOf(paramValue)
    }

playground

You can simplify the code if you compute the reflect value and then check assignability. With this change, the compatible function is not required.

for paramIndex, paramValue := range params {
    if paramValue == nil {
        reflectedParams[paramIndex] = reflect.New(expectedType).Elem()
    } else {
        reflectedParams[paramIndex] = reflect.ValueOf(paramValue)
    }
    expectedType := funcType.In(paramIndex)
    actualType := reflectedParams[paramIndex].Type()
    if !actualType.AssignableTo(expectedType) {
        errStr := fmt.Sprintf("InvocationCausedPanic called with a mismatched parameter type [parameter #%v: expected %v; got %v].", paramIndex, expectedType,actualType)
        panic(errStr)
    }
}

这篇关于使用反射来调用具有零参数的函数会导致“使用零值参数调用”恐慌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆