[pkg/stanza] Fix Stanza Key Value Parser custom pair delimiter bug #31048
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
Fixes a bug that would occur where quotes were not respected while pair parsing with a custom pair delimiter. This occurred because in the case of a custom pair delimiter
strings.Split()
was used which does not respect quotes. This bug was only an issue with custom pair delimiters because the default white space delimiter usedstrings.FieldsFunc
with a function that did respect quotes.This fix adds the function
splitPairs(input, pairDelimiter string) ([]string, error)
which will split the given input on the given pairDelimiter.Alternative solutions like
strings.FieldsFunc
andcsv.NewReader
were considered but not used because they require the delimiter be a rune. This would have worked in many cases except for when the delimiter is more than 1 unique character long. Since the delimiter shouldn't be limited to a single rune, those solutions were rejected.A consequence of this new function respecting quotes is it will fail and return an error if any quoted value is ever malformed. For example, given the input
k1=v1 k2='v2"
the function will return an error because the single quote used fork2
's value is never closed. It will also fail on the inputk1='v1 k2=v2 k3=v3
for the same reason that the quote is never closed. I believe this is the behavior we want so that we don't return incorrect pairs for further parsing.Link to tracking Issue:
Closes #31034
Testing:
Added in additional unit tests to verify correct behaviors.
Documentation:
I don't believe documentation is required as the user shouldn't see any changes to intended behavior.