Files
cgrates/utils/rpc_params.go
2023-06-05 10:55:32 +02:00

74 lines
1.8 KiB
Go

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import (
"reflect"
"sync"
)
var (
rpcParamsMap = make(map[string]*RpcParams)
rpcParamsLock sync.RWMutex
)
type RpcParams struct {
Object any
InParam any
OutParam any
}
func RegisterRpcParams(name string, obj any) {
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()
}
rpcParamsLock.Lock()
rpcParamsMap[name+"."+method.Name] = &RpcParams{
Object: obj,
InParam: reflect.New(methodType.In(1)).Interface(),
OutParam: reflect.New(out).Interface(),
}
rpcParamsLock.Unlock()
}
}
}
func GetRpcParams(method string) (*RpcParams, error) {
rpcParamsLock.Lock()
x, found := rpcParamsMap[method]
rpcParamsLock.Unlock()
if !found {
return nil, ErrNotFound
}
return x, nil
}