Updated *ec unmarshaling on ees

This commit is contained in:
porosnicuadrian
2022-03-04 16:13:20 +02:00
committed by Dan Christian Bogos
parent bcdf9214c1
commit 0dee207d6a
2 changed files with 29 additions and 14 deletions

View File

@@ -376,7 +376,7 @@ func testCsvExportComposedEvent(t *testing.T) {
utils.Cost: 1.016374,
"ExtraFields": map[string]string{"extra1": "val_extra1",
"extra2": "val_extra2", "extra3": "val_extra3"},
utils.CostDetails: cd,
utils.CostDetails: utils.ToJSON(cd),
},
},
}

View File

@@ -32,35 +32,50 @@ type CGREventWithEeIDs struct {
*utils.CGREvent
}
func (cgr *CGREventWithEeIDs) UnmarshalJSON(data []byte) error {
func (cgr *CGREventWithEeIDs) UnmarshalJSON(data []byte) (err error) {
// firstly, we will unamrshall the entire data into raw bytes
ids := make(map[string]json.RawMessage)
if err := json.Unmarshal(data, &ids); err != nil {
return err
if err = json.Unmarshal(data, &ids); err != nil {
return
}
// populate eeids in case of it's existance
eeIDs := make([]string, len(ids[utils.EeIDs]))
if err := json.Unmarshal(ids[utils.EeIDs], &eeIDs); err != nil {
return err
if err = json.Unmarshal(ids[utils.EeIDs], &eeIDs); err != nil {
return
}
cgr.EeIDs = eeIDs
// populate the entire CGRevent struct in case of it's existance
var cgrEv *utils.CGREvent
if err := json.Unmarshal(data, &cgrEv); err != nil {
return err
if err = json.Unmarshal(data, &cgrEv); err != nil {
return
}
cgr.CGREvent = cgrEv
// check if we have CostDetails and modify it's type (by default it was map[string]interface{} by unrmarshaling, now it will be EventCost)
if ecEv, has := cgrEv.Event[utils.CostDetails]; has {
ec := new(EventCost)
bts, err := json.Marshal(ecEv)
if err != nil {
return err
// ee
var bts []byte
switch ecEv.(type) {
case string:
btsToStr, err := json.Marshal(ecEv)
if err != nil {
return err
}
var toString string
if err = json.Unmarshal(btsToStr, &toString); err != nil {
return err
}
bts = []byte(toString)
default:
bts, err = json.Marshal(ecEv)
if err != nil {
return err
}
}
if err := json.Unmarshal(bts, &ec); err != nil {
ec := new(EventCost)
if err = json.Unmarshal(bts, &ec); err != nil {
return err
}
cgr.Event[utils.CostDetails] = ec
}
return nil
return
}