Skip to content

Commit

Permalink
Remove empty strings from slice before parsing agent config (#3387)
Browse files Browse the repository at this point in the history
Fixes: #3385
  • Loading branch information
xoxys committed Feb 14, 2024
1 parent 5e0ec97 commit 6abeff0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/agent/core/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func stringSliceAddToMap(sl []string, m map[string]string) error {
if m == nil {
m = make(map[string]string)
}
for _, v := range sl {
for _, v := range utils.StringSliceDeleteEmpty(sl) {
parts := strings.SplitN(v, "=", 2)
switch len(parts) {
case 2:
Expand Down
11 changes: 11 additions & 0 deletions shared/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ func SliceToBoolMap(s []string) map[string]bool {
}
return v
}

// StringSliceDeleteEmpty removes empty strings from a string slice.
func StringSliceDeleteEmpty(s []string) []string {
r := make([]string, 0)
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
21 changes: 21 additions & 0 deletions shared/utils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,24 @@ func TestSliceToBoolMap(t *testing.T) {
assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{}))
assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{""}))
}

func TestStringSliceDeleteEmpty(t *testing.T) {
tests := []struct {
in []string
out []string
}{{
in: []string{"", "ab", "ab"},
out: []string{"ab", "ab"},
}, {
in: []string{"", "ab", ""},
out: []string{"ab"},
}, {
in: []string{""},
out: []string{},
}}

for _, tc := range tests {
exp := StringSliceDeleteEmpty(tc.in)
assert.EqualValues(t, tc.out, exp, "got '%#v', expects %#v", exp, tc.out)
}
}

0 comments on commit 6abeff0

Please sign in to comment.