no more reflection

This commit is contained in:
Radu Ioan Fericean
2012-10-01 13:07:07 +03:00
parent 7dd14f9038
commit 9349c27f76
6 changed files with 43 additions and 50 deletions

View File

@@ -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
}

View File

@@ -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 {