mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 14:19:54 +05:00
no more reflection
This commit is contained in:
@@ -4,58 +4,59 @@ package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Data being sent to the rpc responder
|
||||
type ArgsGetBalance struct {
|
||||
Tenant string
|
||||
User string
|
||||
Direction string
|
||||
BalanceId string
|
||||
Tenant string
|
||||
User string
|
||||
Direction string
|
||||
BalanceId string
|
||||
}
|
||||
|
||||
|
||||
// Received back from query
|
||||
type ReplyGetBalance struct {
|
||||
Tenant string
|
||||
User string
|
||||
Direction string
|
||||
BalanceId string
|
||||
Balance float64
|
||||
Tenant string
|
||||
User string
|
||||
Direction string
|
||||
BalanceId string
|
||||
Balance float64
|
||||
}
|
||||
|
||||
// Commander implementation
|
||||
type CmdGetBalance struct {
|
||||
rpcMethod string
|
||||
rpcParams ArgsGetBalance
|
||||
rpcResult ReplyGetBalance
|
||||
rpcParams *ArgsGetBalance
|
||||
rpcResult *ReplyGetBalance
|
||||
idxArgsToRpcPrms map[int]string
|
||||
}
|
||||
|
||||
// name should be exec's name
|
||||
func (self *CmdGetBalance) Usage(name string) string {
|
||||
return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] get_balance <tenant> <user> [<balanceid> [<direction>]]", name)
|
||||
return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] get_balance <tenant> <user> [<balanceid> [<direction>]]")
|
||||
}
|
||||
|
||||
// set param defaults
|
||||
func (self *CmdGetBalance) defaults() error {
|
||||
self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5:"Direction" }
|
||||
self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5: "Direction"}
|
||||
self.rpcMethod = "Responder.GetBalance"
|
||||
self.rpcParams.BalanceId = "MONETARY"
|
||||
self.rpcParams.Direction = "OUT"
|
||||
self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARY", Direction: "OUT"}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parses command line args and builds CmdBalance value
|
||||
func (self *CmdGetBalance) FromArgs(args []string) error {
|
||||
if len(args) < 4 {
|
||||
return fmt.Errorf(self.Usage(filepath.Base(args[0])))
|
||||
return fmt.Errorf(self.Usage(""))
|
||||
}
|
||||
// Args look OK, set defaults before going further
|
||||
// Args look OK, set defaults before going further
|
||||
self.defaults()
|
||||
// Dynamically set rpc params
|
||||
CmdRpcPrmsFromArgs(&self.rpcParams, args, self.idxArgsToRpcPrms)
|
||||
self.rpcParams = &ArgsGetBalance{
|
||||
Tenant: args[2],
|
||||
User: args[3],
|
||||
BalanceId: args[4],
|
||||
Direction: args[5],
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,9 +65,10 @@ func (self *CmdGetBalance) RpcMethod() string {
|
||||
}
|
||||
|
||||
func (self *CmdGetBalance) RpcParams() interface{} {
|
||||
return &self.rpcParams
|
||||
return self.rpcParams
|
||||
}
|
||||
|
||||
func (self *CmdGetBalance) RpcResult() interface{} {
|
||||
return &self.rpcResult
|
||||
self.rpcResult = &ReplyGetBalance{}
|
||||
return self.rpcResult
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package console
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Console Command interface
|
||||
@@ -16,22 +15,6 @@ type Commander interface {
|
||||
defaults() error // set defaults wherever necessary
|
||||
}
|
||||
|
||||
// Set command fields based on indexes defined in default()
|
||||
func CmdRpcPrmsFromArgs(rpcPrms interface{}, args []string, idxArgsToRpcPrms map[int]string) {
|
||||
for idx := range args {
|
||||
fldName, hasIdx := idxArgsToRpcPrms[idx]
|
||||
if !hasIdx {
|
||||
continue
|
||||
}
|
||||
// field defined to be set by os.Args index
|
||||
if fld := reflect.ValueOf(rpcPrms).Elem().FieldByName(fldName); fld.Kind() == reflect.String {
|
||||
fld.SetString(args[idx])
|
||||
} else if fld.Kind() == reflect.Int {
|
||||
fld.SetInt(1) // Placeholder for future usage of data types other than strings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process args and return right command Value or error
|
||||
func GetCommandValue(args []string) (Commander, error) {
|
||||
if len(args) < 2 {
|
||||
|
||||
Reference in New Issue
Block a user