mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add new type in utils MapSubsystemIDs
This commit is contained in:
committed by
Dan Christian Bogos
parent
a246124cc9
commit
763e030f03
21
utils/map.go
21
utils/map.go
@@ -247,3 +247,24 @@ func MapStringToInt64(in map[string]string) (out map[string]int64, err error) {
|
||||
}
|
||||
return mapout, nil
|
||||
}
|
||||
|
||||
func MapSubsystemIDsFromSlice(s []string) (MapSubsystemIDs, error) {
|
||||
result := make(MapSubsystemIDs, len(s))
|
||||
for _, v := range s {
|
||||
subsystemWithIDs := strings.Split(v, InInFieldSep)
|
||||
result[subsystemWithIDs[0]] = []string{}
|
||||
if len(subsystemWithIDs) == 2 {
|
||||
result[subsystemWithIDs[0]] = strings.Split(subsystemWithIDs[1], INFIELD_SEP)
|
||||
} else if len(subsystemWithIDs) > 2 {
|
||||
return nil, ErrUnsupportedFormat
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type MapSubsystemIDs map[string][]string
|
||||
|
||||
func (msIDs MapSubsystemIDs) HasKey(key string) (has bool) {
|
||||
_, has = msIDs[key]
|
||||
return
|
||||
}
|
||||
|
||||
@@ -114,3 +114,28 @@ func TestMapHasKey(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMapSubsystemIDsFromSlice(t *testing.T) {
|
||||
sls := []string{"*event", "*thresholds:ID1;ID2;ID3", "*attributes", "*stats:ID"}
|
||||
eMp := MapSubsystemIDs{
|
||||
"*event": []string{},
|
||||
"*thresholds": []string{"ID1", "ID2", "ID3"},
|
||||
"*attributes": []string{},
|
||||
"*stats": []string{"ID"},
|
||||
}
|
||||
if mp, err := MapSubsystemIDsFromSlice(sls); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(mp, eMp) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eMp, mp)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMapSubsystemIDsFromSliceWithErr(t *testing.T) {
|
||||
sls := []string{"*event", "*thresholds:ID1;ID2;ID3:error:", "*attributes", "*stats:ID"}
|
||||
|
||||
if _, err := MapSubsystemIDsFromSlice(sls); err != ErrUnsupportedFormat {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user