简体   繁体   English

执行:反射:使用很少的输入参数进行调用

[英]Go: reflect : Call with too few input arguments

I've stucked with problems of using reflect library. 我一直坚持使用反射库的问题。 I descided to use it because of many recomendations, but i'm just learning go and some parts are not really easy.. 我建议使用它,因为有很多建议,但是我只是在学习go,而某些部分并不是很容易。

I've got this part of code : 我有这部分代码:

 func countDataByName(sourceName string, statData interface{}, filters Filter, chartName string) []ChartElement {
            ...
    //step 1 - filter
    filteredData := reflect.ValueOf(statData).MethodByName("FilterData").Call([]reflect.Value{})

    //step 2 - cluster
    //  clusterData := reflect.ValueOf(filteredData).MethodByName("clusterData").Call([]reflect.Value{})

    //step 3 - count
    //  countedData := reflect.ValueOf(clusterData).MethodByName(chartName).Call([]reflect.Value{})

    fmt.Println("Never prints to anywhere", filteredData)

            ...
     return filterData
 }

If I execute the method like this, I get error : reflect: Call with too few input arguments . 如果执行这样的方法,则会出现错误: reflect: Call with too few input arguments But if I change reflect.ValueOf(statData) on reflect.ValueOf(&statData) than error is reflect: call of reflect.Value.Call on zero Value 但是,如果我更改了reflect.ValueOf(&statData) reflect.ValueOf(statData)上的reflect.ValueOf(&statData) ,那么错误就是reflect: call of reflect.Value.Call on zero Value

statData comes with one of 2 types, and fore this types I have structs and methods, like this : statData带有2种类型之一,在这种类型之前,我有结构和方法,例如:

type NoaggModel struct {
    Date             string
    Hour             int
    Id_user          int
    Id_line          int
    Id_region        int
    Id_tree_devision int
    N_inb            int
    N_inb_d          int
    T_ring           int
    T_inb            int
    T_inb_d          int
    T_hold           int
    T_acw            int
    T_acw_d          int
    T_wait           int
}

func (ng *NoaggModel) FilterData( data NoaggModel) {
    fmt.Println("FilterData")
    fmt.Println("data : ", data)
}

this Println also not works. 此Println也不起作用。 Code panics above , and method was not triggered. 上面的代码恐慌,未触发方法。 Where is my mistake here? 我的错在哪里?

Upd 1: 更新1:

Found that if I remove param data in functioin that I want to call, than it calls nicely. 发现,如果我删除了要调用的函数中的参数data ,那么调用起来就好了。 But! 但! I have statData as 1 row, of structs, so type is NoaggModel . 我将statData作为结构的1行,因此类型为NoaggModel And in the method FilterData I get this 1 row as ng . 在方法FilterData我将此第1行作为ng But I need to change it to the []NoaggModel . 但是我需要将其更改为[]NoaggModel How to call reflect in this case and how to pass parameter to the filter function ? 在这种情况下如何调用reflect以及如何将参数传递给过滤器函数?

Upd 2: I modified few parts : 更新2:我修改了一些部分:

func (ng *NoaggModel) FilterData(filter interface{}, data NoaggModel) {
    fmt.Println("data : ",ng)
}

In here, how to pass correct type to filter , if it is set up in revel controller, and method is in model. 在这里,如何将正确的类型传递给filter ,如果它是在发布控制器中设置的,并且方法在模型中。 Or should I set the type in each model and call it in controller? 还是应该在每个模型中设置类型并在控制器中调用它?

And in controller I wrote : 在控制器中,我写道:

//step 1 - filter
    in := make([]reflect.Value, 2)

    in[0] = reflect.ValueOf(filters)
    in[1] = reflect.ValueOf(statData)

    filteredData := reflect.ValueOf(statData).MethodByName("FilterData").Call(in)

StatData is a row of type NoaggModel, but I get the error : StatDataStatData类型的行,但出现错误:

 reflect: Call using *models.NoaggModel as type models.NoaggModel 

The type was set also by reflect in code above, like this : 该类型也是通过在上面的代码中reflect来设置的,如下所示:

    ...
    var sourceTypes = map[string]reflect.Type{
        "noagg": reflect.TypeOf(models.NoaggModel{}),
        "oracle": reflect.TypeOf(models.OracleModel{}),
    }
    deserializedData = reflect.New(sourceTypes[sourceName]).Interface()
    ...
    // deserialised becomes statData

Reflection is not easy. 反思并不容易。 And should be avoided if possible. 并且应尽可能避免。

I admit that I did recommend using reflect to dynamically create instances of types based on a map, which is really useful when you don't know which types you might have to handle. 我承认我确实建议使用reflect来基于地图动态创建类型的实例,当您不知道可能需要处理哪些类型时,这真的很有用。 But in your case you should consider using interfaces. 但是在您的情况下,您应该考虑使用接口。

While I don't really know what you want to achieve, I would suggest starting by creating an interface that all your Models need to implement (modify it to fit your needs): 虽然我真的不知道要实现什么,但我建议从创建一个所有模型都需要实现的接口开始(进行修改以适应您的需求):

type Model interface {
    FilterData(interface{})
}

NoaggModel and OracleModel would then implement the above interface by defining similar methods like this: 然后, NoaggModelOracleModel将通过定义类似的方法来实现上述接口:

func (ng *NoaggModel) FilterData(filter interface{}) {
    fmt.Printf("data: %#v, filter: %#v\n", ng, filter)
}

Then, change deserializedData (and statData ) to be of the interface type Model instead of interface{} . 然后,切换deserializedData (和statData )是接口类型的Model ,而不是interface{} And since you only have two types, you can avoid using reflect by having a switch instead: 并且由于只有两种类型,因此可以通过使用开关来避免使用反射:

...
var deserializedData Model

switch sourceName {
case "noagg":
    deserializedData = new(models.NoaggModel)
case "oracle":
    deserializedData = new(models.OracleModel)
}
...
// Marshal the values into deserializedData which now holds an instance of the desired type
...
deserializedData.FilterData("Replace this string with your filter")

And it is done without having to import reflect ! 无需导入reflect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM