Skip to content

Commit

Permalink
[pkg/stanza] Update KeyValue parser to use parseutils pkg (open-telem…
Browse files Browse the repository at this point in the history
…etry#31291)

**Description:** <Describe what has changed.>

Updates the KeyValue parser to use the parseutils pkg and subsequent
functions

**Link to tracking Issue:** N/A

Follows up on this
[comment](open-telemetry#31035 (comment))
about merging functionality between Stanza and OTTL key value parsing

**Testing:** 

Unit tests still pass, had to update one because of different wording in
err message

**Documentation:** N/A
  • Loading branch information
dpaasman00 authored and XinRanZhAWS committed Mar 13, 2024
1 parent da87572 commit 949bdd1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 62 deletions.
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

0 comments on commit 949bdd1

Please sign in to comment.