Skip to content

Commit 2ecf32d

Browse files
committed
update parsing to reflect changes made to stanza keyvalue parser
1 parent 967289c commit 2ecf32d

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

pkg/ottl/ottlfuncs/func_parse_key_value.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func parseKeyValue[K any](target ottl.StringGetter[K], d ottl.Optional[string],
6565
if len(pair) != 2 {
6666
return nil, fmt.Errorf("cannot split '%s' into 2 items, got %d", p, len(pair))
6767
}
68-
key := strings.TrimSpace(strings.Trim(pair[0], "\"'"))
69-
value := strings.TrimSpace(strings.Trim(pair[1], "\"'"))
68+
key := strings.TrimSpace(pair[0])
69+
value := strings.TrimSpace(pair[1])
7070
parsed[key] = value
7171
}
7272

@@ -84,36 +84,34 @@ func splitPairs(input, pairDelimiter string) ([]string, error) {
8484
delimiterLength := len(pairDelimiter)
8585
quoteChar := "" // "" means we are not in quotes
8686

87-
i := 0
88-
for i < len(input) {
89-
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == pairDelimiter {
90-
if currentPair == "" {
91-
i++
87+
for i := 0; i < len(input); i++ {
88+
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == pairDelimiter { // delimiter
89+
if currentPair == "" { // leading || trailing delimiter; ignore
9290
continue
9391
}
9492
result = append(result, currentPair)
9593
currentPair = ""
96-
i += delimiterLength
94+
i += delimiterLength - 1
95+
continue
96+
}
97+
98+
if quoteChar == "" && (input[i] == '"' || input[i] == '\'') { // start of quote
99+
quoteChar = string(input[i])
97100
continue
98-
} else if input[i] == '"' || input[i] == '\'' {
99-
if quoteChar != "" {
100-
if quoteChar == string(input[i]) {
101-
quoteChar = ""
102-
}
103-
} else {
104-
quoteChar = string(input[i])
105-
}
106101
}
102+
if string(input[i]) == quoteChar { // end of quote
103+
quoteChar = ""
104+
continue
105+
}
106+
107107
currentPair += string(input[i])
108-
i++
109108
}
110109

111-
if quoteChar != "" {
110+
if quoteChar != "" { // check for closed quotes
112111
return nil, fmt.Errorf("never reached end of a quoted value")
113112
}
114-
115-
if currentPair != "" {
116-
result = append(result, currentPair)
113+
if currentPair != "" { // avoid adding empty value bc of a trailing delimiter
114+
return append(result, currentPair), nil
117115
}
118116

119117
return result, nil

pkg/ottl/ottlfuncs/func_parse_key_value_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,17 @@ hello!!world `, nil
202202
},
203203
},
204204
{
205-
name: "double quotes",
205+
name: " embedded double quotes end single quoted value",
206206
target: ottl.StandardStringGetter[any]{
207207
Getter: func(ctx context.Context, tCtx any) (any, error) {
208-
return `a=b c='this is a "cool" value'`, nil
208+
return `a=b c='this is a "co ol"'`, nil
209209
},
210210
},
211211
delimiter: ottl.Optional[string]{},
212212
pair_delimiter: ottl.Optional[string]{},
213213
expected: map[string]any{
214214
"a": "b",
215-
"c": "this is a \"cool\" value",
215+
"c": "this is a \"co ol\"",
216216
},
217217
},
218218
}

0 commit comments

Comments
 (0)