Skip to content

Commit e890c30

Browse files
✨ [collection] ParseListWithCleanupKeepBlankLines as alternative to ParseListWithCleanup to keep blank lines (#82)
Add option to run `ParseListWithCleanupKeepBlankLines` to run `ParseListWithCleanup` but keep empty lines. This is necessary for the VHT Service. Backwards compatible with old versions.
1 parent 467b6cd commit e890c30

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

changes/202205121612.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add option to run parseListWithCleanup to keep empty lines

utils/collection/parseLists.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,50 @@
44
*/
55
package collection
66

7-
import "strings"
7+
import (
8+
"strings"
9+
"unicode"
10+
)
811

9-
// ParseListWithCleanup splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
10-
// for example,
11-
// ParseListWithCleanup("a, b , c", ",") returns []{"a","b","c"}
12-
func ParseListWithCleanup(input string, sep string) (newS []string) {
12+
func lineIsOnlyWhitespace(line string) bool {
13+
for _, c := range line {
14+
if !unicode.IsSpace(c) {
15+
return false
16+
}
17+
}
18+
return true
19+
}
20+
21+
func parseListWithCleanup(input string, sep string, keepBlankLines bool) (newS []string) {
1322
if len(input) == 0 {
1423
newS = []string{} // initialisation of empty arrays in function returns []string(nil) instead of []string{}
1524
return
1625
}
1726
split := strings.Split(input, sep)
1827
for _, s := range split {
1928
tempString := strings.TrimSpace(s)
20-
if tempString != "" {
29+
if tempString != "" || (keepBlankLines && lineIsOnlyWhitespace(s)) {
2130
newS = append(newS, tempString)
2231
}
2332
}
2433
return
2534
}
2635

36+
// ParseListWithCleanup splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
37+
// for example,
38+
// ParseListWithCleanup("a, b , c", ",") returns []{"a","b","c"}
39+
func ParseListWithCleanup(input string, sep string) (newS []string) {
40+
return parseListWithCleanup(input, sep, false)
41+
}
42+
43+
// ParseListWithCleanupKeepBlankLines splits a string into a list like strings.Split but also removes any whitespace surrounding the different items
44+
// unless the entire item is whitespace in which case it is converted to an empty string. For example,
45+
// ParseListWithCleanupKeepBlankLines("a, b , c", ",") returns []{"a","b","c"}
46+
// ParseListWithCleanupKeepBlankLines("a, b , , c", ",") returns []{"a","b", "", "c"}
47+
func ParseListWithCleanupKeepBlankLines(input string, sep string) (newS []string) {
48+
return parseListWithCleanup(input, sep, true)
49+
}
50+
2751
// ParseCommaSeparatedList returns the list of string separated by a comma
2852
func ParseCommaSeparatedList(input string) []string {
2953
return ParseListWithCleanup(input, ",")

utils/collection/parseLists_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ func TestParseCommaSeparatedListWithSpacesBetweenWords(t *testing.T) {
5050
finalList := ParseCommaSeparatedList(stringList)
5151
require.Equal(t, stringArray, finalList)
5252
}
53+
54+
func TestParseCommaSeparatedListWithSpacesBetweenWordsKeepBlanks(t *testing.T) {
55+
stringList := ""
56+
stringArray := []string{}
57+
// we don't need cryptographically secure random numbers for generating a number of elements in a list
58+
lengthOfList := rand.Intn(10) + 8 //nolint:gosec
59+
for i := 0; i < lengthOfList; i++ {
60+
word := faker.Sentence()
61+
stringList += word
62+
stringArray = append(stringArray, word)
63+
numSpacesToAdd := rand.Intn(5) //nolint:gosec
64+
for j := 0; j < numSpacesToAdd; j++ {
65+
stringList += " "
66+
}
67+
stringList += ","
68+
if i%3 == 2 {
69+
numSpacesToAdd := rand.Intn(5) //nolint:gosec
70+
for j := 0; j < numSpacesToAdd; j++ {
71+
stringList += " "
72+
}
73+
stringArray = append(stringArray, "")
74+
stringList += ","
75+
}
76+
}
77+
stringArray = append(stringArray, "") // account for final ,
78+
79+
finalList1 := ParseCommaSeparatedList(stringList)
80+
require.NotEqual(t, stringArray, finalList1)
81+
82+
finalList2 := ParseListWithCleanupKeepBlankLines(stringList, ",")
83+
require.Equal(t, stringArray, finalList2)
84+
}

0 commit comments

Comments
 (0)