mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Separated rpcParams into it's own structure for optimisation on send purposes, methods cleanup
This commit is contained in:
@@ -8,42 +8,43 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type CmdGetBalance struct {
|
||||
|
||||
type PrmsGetBalance struct {
|
||||
User string
|
||||
BalanceType string
|
||||
Direction string
|
||||
rpcMethod string
|
||||
rpcParams interface{}
|
||||
rpcResult string
|
||||
}
|
||||
|
||||
type CmdGetBalance struct {
|
||||
rpcMethod string
|
||||
rpcParams PrmsGetBalance
|
||||
rpcResult string
|
||||
idxArgsToRpcPrms map[int]string
|
||||
}
|
||||
|
||||
// name should be exec's name
|
||||
func (self *CmdGetBalance) usage(name string) string {
|
||||
func (self *CmdGetBalance) Usage(name string) string {
|
||||
return fmt.Sprintf("usage: %s get_balance <user> <baltype> [<direction>]", name)
|
||||
}
|
||||
|
||||
// set param defaults
|
||||
func (self *CmdGetBalance) defaults() error {
|
||||
self.idxArgsToRpcPrms = map[int]string{2: "User", 3: "BalanceType", 4: "Direction"}
|
||||
self.rpcMethod = "Responder.GetBalance"
|
||||
self.rpcParams = self
|
||||
self.BalanceType = "MONETARY"
|
||||
self.Direction = "OUT"
|
||||
self.rpcParams.BalanceType = "MONETARY"
|
||||
self.rpcParams.Direction = "OUT"
|
||||
return nil
|
||||
}
|
||||
|
||||
func( self *CmdGetBalance) idxArgsToFields() map[int]string {
|
||||
return map[int]string{2: "User", 3: "BalanceType", 4: "Direction"}
|
||||
}
|
||||
|
||||
// Parses command line args and builds CmdBalance value
|
||||
func (self *CmdGetBalance) FromArgs(args []string) error {
|
||||
if len(os.Args) < 3 {
|
||||
return fmt.Errorf(self.usage(filepath.Base(args[0])))
|
||||
return fmt.Errorf(self.Usage(filepath.Base(args[0])))
|
||||
}
|
||||
// Args look OK, set defaults before going further
|
||||
self.defaults()
|
||||
// Dynamically set field values
|
||||
CmdFieldsFromArgs( self, args )
|
||||
// Dynamically set rpc params
|
||||
CmdRpcPrmsFromArgs( self.rpcParams, args, self.idxArgsToRpcPrms )
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -11,24 +11,23 @@ import (
|
||||
// Console Command interface
|
||||
type Commander interface {
|
||||
FromArgs(args []string) error // Load data from os arguments or flag.Args()
|
||||
usage(string) string // usage message
|
||||
defaults() error // set default field values
|
||||
idxArgsToFields() map[int]string // field's index position in command arguments
|
||||
Usage(string) string // usage message
|
||||
RpcMethod() string // Method which should be called remotely
|
||||
RpcParams() interface{} // Parameters to send out on rpc
|
||||
RpcResult() interface{} // Only requirement is to have a String method to print on console
|
||||
defaults() error // set default field values
|
||||
}
|
||||
|
||||
|
||||
// Set command fields based on indexes defined in default()
|
||||
func CmdFieldsFromArgs( cmd Commander, args []string ) {
|
||||
func CmdRpcPrmsFromArgs( rpcPrms interface{}, args []string, idxArgsToRpcPrms map[int]string ) {
|
||||
for idx := range args {
|
||||
fldName, hasIdx := cmd.idxArgsToFields()[idx]
|
||||
fldName, hasIdx := idxArgsToRpcPrms[idx]
|
||||
if !hasIdx {
|
||||
continue
|
||||
}
|
||||
// field defined to be set by os.Args index
|
||||
if fld := reflect.ValueOf(cmd).Elem().FieldByName(fldName); fld.Kind() == reflect.String {
|
||||
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
|
||||
@@ -56,5 +55,3 @@ func GetCommandValue( args []string ) ( Commander, error ) {
|
||||
}
|
||||
return cmdVal, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ type CmdStatus struct {
|
||||
rpcResult string
|
||||
}
|
||||
|
||||
func (self *CmdStatus) usage(name string) string {
|
||||
func (self *CmdStatus) Usage(name string) string {
|
||||
return fmt.Sprintf("usage: %s status", name)
|
||||
}
|
||||
|
||||
@@ -19,10 +19,6 @@ func (self *CmdStatus) defaults() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func( self *CmdStatus) idxArgsToFields() map[int]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *CmdStatus) FromArgs(args []string) error {
|
||||
self.defaults()
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user