Skip to content

Commit 6de875a

Browse files
committed
respect embedded commas when parsing slices
1 parent 69829ef commit 6de875a

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

cmd/telemetrygen/internal/common/config.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,34 @@ func parseValue(val string) (any, error) {
5252
return val[1 : len(val)-1], nil
5353
}
5454

55-
// splitItems splits the content into a list of items separated by commas
55+
// splitItems splits the pre-trimmed content into a list of items separated by commas, respecting quotes
5656
func splitItems(content string) []string {
5757
var items []string
58-
for item := range strings.SplitSeq(content, ",") {
59-
if i := strings.TrimSpace(item); i != "" {
60-
items = append(items, i)
58+
var current strings.Builder
59+
inQuotes := false
60+
61+
for _, char := range content {
62+
switch char {
63+
case '"':
64+
inQuotes = !inQuotes
65+
current.WriteRune(char)
66+
case ',':
67+
if !inQuotes {
68+
if item := strings.TrimSpace(current.String()); item != "" {
69+
items = append(items, item)
70+
}
71+
current.Reset()
72+
} else {
73+
current.WriteRune(char)
74+
}
75+
default:
76+
current.WriteRune(char)
6177
}
6278
}
79+
// Add the last item
80+
if item := strings.TrimSpace(current.String()); item != "" {
81+
items = append(items, item)
82+
}
6383
return items
6484
}
6585

cmd/telemetrygen/internal/common/config_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func TestKeyValueSet(t *testing.T) {
9696
flag: "key=[true,]",
9797
expected: KeyValue(map[string]any{"key": []bool{true}}),
9898
},
99+
{
100+
flag: "key=[\"value1\",\"value-with,comma\"]",
101+
expected: KeyValue(map[string]any{"key": []string{"value1", "value-with,comma"}}),
102+
},
103+
{
104+
flag: "key=[2,3,,1]",
105+
expected: KeyValue(map[string]any{"key": []int{2, 3, 1}}),
106+
},
99107
{
100108
flag: "key=12.34",
101109
err: errDoubleQuotesOTLPAttributes,

0 commit comments

Comments
 (0)