This is a followup question to Function and argument unmarshalling in go?.
Given the following interface variable.
var args interface{}
Assuming as an example it contained these bytes:
[[74 111 104 110] [32 97 110 100 32 74 97 110 101]]
i.e. two strings "John", " and Jane"
And a function value obtained by MethodByName
f := reflect.ValueOf(s.functions).MethodByName("Hello")
if f.IsValid() {
val := reflect.ValueOf(args)
// Do some kind of conversion...
result := f.Call(val) // This won't compile. Wrong type. How do I massage args of type interface{} into what's expected by the call.
}
I don't particularly care if it fails. I will capture the failure of the call with recover.
Here is a bit more detail:
var req struct {
Ver int
MsgID int
FuncName string
Args interface{}
}
dec := codec.NewDecoder(frame, s.h)
err = dec.Decode(&req)
if err != nil {
s.log.Println(err)
break
}
fmt.Println("New Msg:")
fmt.Printf(" ver : %d
", req.Ver)
fmt.Printf(" id : %d
", req.MsgID)
fmt.Printf(" func : %s
", req.FuncName)
f := reflect.ValueOf(s.functions).MethodByName(req.FuncName)
if f.IsValid() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
fmt.Println("Function is: ", req.FuncName)
var callArgs []reflect.Value
args := reflect.ValueOf(req.Args)
t := f.Type()
for i := 0; i < t.NumIn(); i++ {
t := t.In(i)
v := reflect.New(t).Elem()
if i < args.Len() {
// Convert arg to type of v and set.
arg := args.Index(i)
switch t.Kind() {
case reflect.String:
v.SetString(string(reflect.Value.Bytes(arg)))
case reflect.Slice:
if t.Elem() == reflect.TypeOf(byte(0)) {
v.SetBytes(reflect.Value.Bytes(arg))
} else {
panic("not supported")
}
case reflect.Int:
//i, err := strconv.ParseInt(string(arg), 10, 0)
// if err != nil {
// panic("bad int")
// }
// v.SetInt(i)
default:
panic("not supported")
}
}
// Collect arguments for the call below.
callArgs = append(callArgs, v)
}
result := f.Call(callArgs)
fmt.Println(result)
val := reflect.ValueOf(req.Args)
a := []reflect.Value{val}
r := f.Call(a)
fmt.Println("Returned", r[0], r[1])
}
Outputs:
New Msg: ver : 2 id : 1 func : Hello Function is: Hello Recovered in f reflect: call of reflect.Value.Bytes on interface Value
Note: This is an RPC API. I have a function name (see link at top of question) and some arguments that are passed in as an array. The arguments in my example here are strings but could anything that you could pass to a function. It's whatever the function requires. It's generic.
e.g.
Hello(name ...string) string
or
Add(n ...int) int
or
DoSomething(a string, b int, c bool)
etc
i.e. I'm unmarshalling the arguments and I don't know what they are.
Except that I do know they will be passed into a slice and
thrown into a variable Args which has the type interface{}
I hope this makes sense now