Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] Update KeyValue parser to use parseutils pkg #31291

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 3 additions & 61 deletions pkg/stanza/operator/parser/keyvalue/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"context"
"errors"
"fmt"
"strings"

"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/parseutils"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
Expand Down Expand Up @@ -98,67 +97,10 @@ func (kv *Parser) parser(input string, delimiter string, pairDelimiter string) (
return nil, fmt.Errorf("parse from field %s is empty", kv.ParseFrom.String())
}

pairs, err := splitPairs(input, pairDelimiter)
pairs, err := parseutils.SplitString(input, pairDelimiter)
if err != nil {
return nil, fmt.Errorf("failed to parse pairs from input: %w", err)
}

parsed := make(map[string]any)

for _, raw := range pairs {
m := strings.SplitN(raw, delimiter, 2)
if len(m) != 2 {
e := fmt.Errorf("expected '%s' to split by '%s' into two items, got %d", raw, delimiter, len(m))
err = multierr.Append(err, e)
continue
}

key := strings.TrimSpace(m[0])
value := strings.TrimSpace(m[1])

parsed[key] = value
}

return parsed, err
}

// splitPairs will split the input on the pairDelimiter and return the resulting slice.
// `strings.Split` is not used because it does not respect quotes and will split if the delimiter appears in a quoted value
func splitPairs(input, pairDelimiter string) ([]string, error) {
var result []string
currentPair := ""
delimiterLength := len(pairDelimiter)
quoteChar := "" // "" means we are not in quotes

for i := 0; i < len(input); i++ {
if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == pairDelimiter { // delimiter
if currentPair == "" { // leading || trailing delimiter; ignore
continue
}
result = append(result, currentPair)
currentPair = ""
i += delimiterLength - 1
continue
}

if quoteChar == "" && (input[i] == '"' || input[i] == '\'') { // start of quote
quoteChar = string(input[i])
continue
}
if string(input[i]) == quoteChar { // end of quote
quoteChar = ""
continue
}

currentPair += string(input[i])
}

if quoteChar != "" { // check for closed quotes
return nil, fmt.Errorf("never reached end of a quoted value")
}
if currentPair != "" { // avoid adding empty value bc of a trailing delimiter
return append(result, currentPair), nil
}

return result, nil
return parseutils.ParseKeyValuePairs(pairs, delimiter)
}
2 changes: 1 addition & 1 deletion pkg/stanza/operator/parser/keyvalue/keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestParserStringFailure(t *testing.T) {
parser := newTestParser(t)
_, err := parser.parse("invalid")
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("expected '%s' to split by '%s' into two items, got", "invalid", parser.delimiter))
require.Contains(t, err.Error(), fmt.Sprintf("cannot split %q into 2 items, got 1 item(s)", "invalid"))
}

func TestParserInvalidType(t *testing.T) {
Expand Down
Loading