vendredi 4 janvier 2019

Golang error control on parameters when calling a function with reflection

I'm trying to call a function using reflection based on it's name.

I first recover the function with

f := reflect.ValueOf(s).MethodByName(name)

I do error control on the function name by checking if "f" is valid with

if !f.IsValid() {
    return errors.New("There is no function with that name")
}

And finally I perform the call with

f.Call(inputs)

My challenge is that inputs depends on the user, and sometimes they can put too many parameters, too little or invalid types.

IE:

2019/01/04 16:47:54 http: panic serving [::1]:53662: reflect: Call using string as type int

How can I control that the inputs are valid before performing the call? Maybe there is a way to recover the expected inputs in my method and check them against the provided ones?





Aucun commentaire:

Enregistrer un commentaire