Files
cgrates/utils/rpc_params.go
2016-05-26 15:32:55 +03:00

50 lines
1.1 KiB
Go

package utils
import "reflect"
var rpcParamsMap map[string]*RpcParams
type RpcParams struct {
Object interface{}
InParam interface{}
OutParam interface{}
}
func init() {
rpcParamsMap = make(map[string]*RpcParams)
}
func RegisterRpcParams(name string, obj interface{}) {
objType := reflect.TypeOf(obj)
if name == "" {
val := reflect.ValueOf(obj)
name = objType.Name()
if val.Kind() == reflect.Ptr {
name = objType.Elem().Name()
}
}
for i := 0; i < objType.NumMethod(); i++ {
method := objType.Method(i)
methodType := method.Type
if methodType.NumIn() == 3 { // if it has three parameters (one is self and two are rpc params)
out := methodType.In(2)
if out.Kind() == reflect.Ptr {
out = out.Elem()
}
rpcParamsMap[name+"."+method.Name] = &RpcParams{
Object: obj,
InParam: reflect.New(methodType.In(1)).Interface(),
OutParam: reflect.New(out).Interface(),
}
}
}
}
func GetRpcParams(method string) (*RpcParams, error) {
x, found := rpcParamsMap[method]
if !found {
return nil, ErrNotFound
}
return x, nil
}