-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 json parser operator files (#32110)
Contributes to #32058
- Loading branch information
1 parent
18eee5c
commit 5385fb0
Showing
3 changed files
with
40 additions
and
30 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,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package json // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
|
||
"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 JSON. | ||
type Parser struct { | ||
helper.ParserOperator | ||
json jsoniter.API | ||
} | ||
|
||
// Process will parse an entry for JSON. | ||
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 JSON. | ||
func (p *Parser) parse(value any) (any, error) { | ||
var parsedValue map[string]any | ||
switch m := value.(type) { | ||
case string: | ||
err := p.json.UnmarshalFromString(m, &parsedValue) | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
return nil, fmt.Errorf("type %T cannot be parsed as JSON", value) | ||
} | ||
return parsedValue, nil | ||
} |
File renamed without changes.