forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Cleanup keyvalue parser operator files (open-tele…
…metry#32112) Contributes to open-telemetry#32058
- Loading branch information
1 parent
5385fb0
commit 5c7e342
Showing
3 changed files
with
48 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package keyvalue // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/keyvalue" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"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/helper" | ||
) | ||
|
||
// Parser is an operator that parses key value pairs. | ||
type Parser struct { | ||
helper.ParserOperator | ||
delimiter string | ||
pairDelimiter string | ||
} | ||
|
||
// Process will parse an entry for key value pairs. | ||
func (p *Parser) Process(ctx context.Context, entry *entry.Entry) error { | ||
return p.ParserOperator.ProcessWith(ctx, entry, p.parse) | ||
} | ||
|
||
// parse will parse a value as key values. | ||
func (p *Parser) parse(value any) (any, error) { | ||
switch m := value.(type) { | ||
case string: | ||
return p.parser(m, p.delimiter, p.pairDelimiter) | ||
default: | ||
return nil, fmt.Errorf("type %T cannot be parsed as key value pairs", value) | ||
} | ||
} | ||
|
||
func (p *Parser) parser(input string, delimiter string, pairDelimiter string) (map[string]any, error) { | ||
if input == "" { | ||
return nil, fmt.Errorf("parse from field %s is empty", p.ParseFrom.String()) | ||
} | ||
|
||
pairs, err := parseutils.SplitString(input, pairDelimiter) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse pairs from input: %w", err) | ||
} | ||
|
||
return parseutils.ParseKeyValuePairs(pairs, delimiter) | ||
} |
File renamed without changes.