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

@@ -21,18 +21,17 @@ package main
import (
"flag"
"fmt"
"github.com/cgrates/cgrates/console"
"github.com/cgrates/cgrates/timespans"
"log"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"reflect"
"github.com/cgrates/cgrates/timespans"
"github.com/cgrates/cgrates/console"
)
var (
version = flag.Bool("version", false, "Prints the application version.")
server = flag.String("server", "127.0.0.1:2000", "server address host:port")
version = flag.Bool("version", false, "Prints the application version.")
server = flag.String("server", "127.0.0.1:2000", "server address host:port")
rpc_encoding = flag.String("rpc_encoding", "gob", "RPC encoding used <gob|json>")
)
@@ -62,8 +61,7 @@ func main() {
}
res := cmd.RpcResult()
if rpcErr := client.Call(cmd.RpcMethod(), cmd.RpcParams(), res); rpcErr != nil {
log.Fatal(rpcErr)
}
fmt.Println(reflect.ValueOf(res).Elem().Interface())
fmt.Println("Result:", res)
}

View File

@@ -106,7 +106,6 @@ func main() {
log.Fatal(err, "\n\t", v.message)
}
}
//sep = []rune(*separator)[0]
sep = ','
csvr := timespans.NewFileCSVReader()
@@ -169,6 +168,8 @@ func main() {
log.Fatalf("Could not open database connection: %v", err)
}
// writing to database
csvr.WriteToDatabase(getter, *flush, true)
// write maps to database
if err := csvr.WriteToDatabase(getter, *flush, true); err != nil {
log.Fatal("Could not write to database: ", err)
}
}

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 {

View File

@@ -22,4 +22,10 @@ import (
_ "github.com/cgrates/cgrates/cmd/cgr-console"
_ "github.com/cgrates/cgrates/cmd/cgr-loader"
_ "github.com/cgrates/cgrates/cmd/cgr-rater"
"github.com/cgrates/cgrates/timespans"
)
func main(){
cd := timespans.CallDesc{}
}

View File

@@ -184,12 +184,15 @@ func (rs *Responder) GetBalance(arg console.ArgsGetBalance, reply *console.Reply
ubKey := arg.Direction + ":" + arg.Tenant + ":" + arg.User
userBalance, err := storageGetter.GetUserBalance(ubKey)
if err != nil {
fmt.Println("here")
return err
}
if balance, balExists := userBalance.BalanceMap[arg.BalanceId]; !balExists {
// No match, balanceId not found
fmt.Printf("%+v : %s\n", userBalance, arg.BalanceId)
return fmt.Errorf("-BALANCE_NOT_FOUND")
} else {
fmt.Println("bal: ", balance)
reply.Tenant = arg.Tenant
reply.User = arg.User
reply.Direction = arg.Direction