diff --git a/engine/action.go b/engine/action.go index 62ca439dd..3aedbf153 100644 --- a/engine/action.go +++ b/engine/action.go @@ -74,6 +74,7 @@ const ( CDRLOG = "*cdrlog" SET_DDESTINATIONS = "*set_ddestinations" TRANSFER_MONETARY_DEFAULT = "*transfer_monetary_default" + CGR_RPC = "*cgr_rpc" ) func (a *Action) Clone() *Action { @@ -140,6 +141,8 @@ func getActionFunc(typ string) (actionTypeFunc, bool) { return setBalanceAction, true case TRANSFER_MONETARY_DEFAULT: return transferMonetaryDefaultAction, true + case CGR_RPC: + return cgrRPCAction, true } return nil, false } @@ -647,6 +650,22 @@ func transferMonetaryDefaultAction(acc *Account, sq *StatsQueueTriggered, a *Act return nil } +type RPCRequest struct { + Server string + Transport string + Attempts int + Async bool + Arg map[string]interface{} +} + +func cgrRPCAction(account *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error { + rpcRequest := RPCRequest{} + if err := json.Unmarshal([]byte(a.ExtraParameters), &rpcRequest); err != nil { + return err + } + return nil +} + // Structure to store actions according to weight type Actions []*Action diff --git a/engine/actions_test.go b/engine/actions_test.go index bead47620..9b3dc8f22 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -2102,6 +2102,8 @@ func TestActionCdrlogBalanceValue(t *testing.T) { ID: "cgrates.org:bv", BalanceMap: map[string]Balances{ utils.MONETARY: Balances{&Balance{ + ID: "*default", + Uuid: "25a02c82-f09f-4c6e-bacf-8ed4b076475a", Value: 10, }}, }, @@ -2114,16 +2116,29 @@ func TestActionCdrlogBalanceValue(t *testing.T) { Timing: &RateInterval{}, actions: []*Action{ &Action{ + Id: "RECUR_FOR_V3HSILLMILLD1G", ActionType: TOPUP, - Balance: &BalanceFilter{Value: utils.Float64Pointer(1.1), Type: utils.StringPointer(utils.MONETARY)}, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Uuid: utils.StringPointer("25a02c82-f09f-4c6e-bacf-8ed4b076475a"), + Value: utils.Float64Pointer(1.1), + Type: utils.StringPointer(utils.MONETARY), + }, }, &Action{ + Id: "RECUR_FOR_V3HSILLMILLD5G", ActionType: DEBIT, - Balance: &BalanceFilter{Value: utils.Float64Pointer(2.1), Type: utils.StringPointer(utils.MONETARY)}, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Uuid: utils.StringPointer("25a02c82-f09f-4c6e-bacf-8ed4b076475a"), + Value: utils.Float64Pointer(2.1), + Type: utils.StringPointer(utils.MONETARY), + }, }, &Action{ + Id: "c", ActionType: CDRLOG, - ExtraParameters: `{"BalanceValue":"BalanceValue"}`, + ExtraParameters: `{"BalanceID":"BalanceID","BalanceUUID":"BalanceUUID","ActionID":"ActionID","BalanceValue":"BalanceValue"}`, }, }, } @@ -2140,7 +2155,7 @@ func TestActionCdrlogBalanceValue(t *testing.T) { if len(cdrs) != 2 || cdrs[0].ExtraFields["BalanceValue"] != "11.1" || cdrs[1].ExtraFields["BalanceValue"] != "9" { - t.Errorf("Wrong cdrlogs: %+v", cdrs[1]) + t.Errorf("Wrong cdrlogs: %", utils.ToIJSON(cdrs)) } } diff --git a/utils/struct.go b/utils/struct.go index 0b16caeef..3da80ec84 100644 --- a/utils/struct.go +++ b/utils/struct.go @@ -18,6 +18,7 @@ along with this program. If not, see package utils import ( + "errors" "reflect" "strconv" "strings" @@ -171,6 +172,28 @@ func FromMapStringString(m map[string]string, in interface{}) { return } +func FromMapStringInterface(m map[string]interface{}, in interface{}) error { + v := reflect.ValueOf(in) + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + for fieldName, fieldValue := range m { + field := v.FieldByName(fieldName) + if field.IsValid() { + if !field.IsValid() || !field.CanSet() { + continue + } + structFieldType := field.Type() + val := reflect.ValueOf(fieldValue) + if structFieldType != val.Type() { + return errors.New("Provided value type didn't match obj field type") + } + field.Set(val) + } + } + return nil +} + // Update struct with map fields, returns not matching map keys, s is a struct to be updated func UpdateStructWithStrMap(s interface{}, m map[string]string) []string { notMatched := []string{} diff --git a/utils/struct_test.go b/utils/struct_test.go index 50371ea36..74450a7cb 100644 --- a/utils/struct_test.go +++ b/utils/struct_test.go @@ -84,3 +84,32 @@ func TestStructExtraFields(t *testing.T) { t.Errorf("expected: %v got: %v", ts.ExtraFields, efMap) } } + +func TestStructFromMapStringInterface(t *testing.T) { + ts := &struct { + Name string + Class *string + List []string + Elements struct { + Type string + Value float64 + } + }{} + s := "test2" + m := map[string]interface{}{ + "Name": "test1", + "Class": &s, + "List": []string{"test3", "test4"}, + "Elements": struct { + Type string + Value float64 + }{ + Type: "test5", + Value: 9.8, + }, + } + if err := FromMapStringInterface(m, ts); err != nil { + t.Logf("ts: %+v", ToJSON(ts)) + t.Error("Error converting map to struct: ", err) + } +}