From 6695db8df74c958113738f28ceaa4f17cf670e48 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Tue, 6 Jan 2026 09:43:45 +0100 Subject: [PATCH] updated GetActions api to return negative destinations --- apier/v1/apier.go | 2 +- utils/map.go | 15 +++++++++++++ utils/map_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index cecdd36c1..6373b202d 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -713,7 +713,7 @@ func (apierSv1 *APIerSv1) GetActions(ctx *context.Context, actsId *string, reply if bf != nil { act.BalanceType = bf.GetType() act.Units = strconv.FormatFloat(bf.GetValue(), 'f', -1, 64) - act.DestinationIds = bf.GetDestinationIDs().String() + act.DestinationIds = bf.GetDestinationIDs().AsPrefixedString() act.RatingSubject = bf.GetRatingSubject() act.SharedGroups = bf.GetSharedGroups().String() act.BalanceWeight = strconv.FormatFloat(bf.GetWeight(), 'f', -1, 64) diff --git a/utils/map.go b/utils/map.go index 5ba8d5c77..44cb8bf47 100644 --- a/utils/map.go +++ b/utils/map.go @@ -104,6 +104,21 @@ func StringMapFromSlice(s []string) StringMap { return result } +func (sm StringMap) AsPrefixedString() string { + if len(sm) == 0 { + return "" + } + res := make([]string, 0, len(sm)) + for key, enabled := range sm { + if !enabled { + res = append(res, NegativePrefix+key) + } else { + res = append(res, key) + } + } + return strings.Join(res, InfieldSep) +} + func (sm StringMap) Copy(o StringMap) { for k, v := range o { sm[k] = v diff --git a/utils/map_test.go b/utils/map_test.go index 8dee6c82a..e000cd9cb 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -19,7 +19,9 @@ package utils import ( "reflect" + "slices" "sort" + "strings" "testing" ) @@ -519,6 +521,58 @@ func TestStringMapFromSlice(t *testing.T) { } +func TestStringMapAsPrefixedString(t *testing.T) { + tests := []struct { + name string + input StringMap + expStr string + }{ + { + name: "NormalDest", + input: StringMap{ + "DEST_1001": true, + "DEST_1002": true, + }, + expStr: "DEST_1001;DEST_1002", + }, + { + name: "ExcludedDest", + input: StringMap{ + "DEST_1003": false, + "DEST_1004": false, + }, + expStr: "!DEST_1003;!DEST_1004", + }, + { + name: "MixedDest", + input: StringMap{ + "DEST_1001": true, + "DEST_1004": false, + }, + expStr: "DEST_1001;!DEST_1004", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.input.AsPrefixedString() + + gotDest := strings.Split(got, InfieldSep) + expDest := strings.Split(tt.expStr, InfieldSep) + + slices.Sort(gotDest) + slices.Sort(expDest) + + for i := range gotDest { + if gotDest[i] != expDest[i] { + t.Errorf("Mismatch at index %d! Expected: %s, Received: %s", i, expDest[i], gotDest[i]) + } + } + + }) + } +} + func TestStringMapNewStringMap(t *testing.T) { s := []string{"foo", "", "!baz", "bar"} exp := map[string]bool{