From 763e030f03429a67bcb6288a3b1e6845e423f7a9 Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 2 Jul 2019 15:59:43 +0300 Subject: [PATCH] Add new type in utils MapSubsystemIDs --- utils/map.go | 21 +++++++++++++++++++++ utils/map_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/utils/map.go b/utils/map.go index 448eb474e..b1c2b1f7f 100644 --- a/utils/map.go +++ b/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 +} diff --git a/utils/map_test.go b/utils/map_test.go index e65c36186..6a93d4b14 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -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) + } + +}