-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add parse_ints
config in json parser to support parsing int or float properly
#33699
Changes from 5 commits
06b8e50
e3b3b43
cb7ce2d
5ad9c68
87ed62a
a260977
299ec68
0a2882e
18a8d0f
66df47e
8ab600d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/stanza | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add `use_number` config in json parser to support decode into int or float properly | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33696] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package json // import "github.com/open-telemetry/opentelemetry-collector-contri | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
|
@@ -16,7 +17,8 @@ import ( | |
// Parser is an operator that parses JSON. | ||
type Parser struct { | ||
helper.ParserOperator | ||
json jsoniter.API | ||
json jsoniter.API | ||
useNumber bool | ||
} | ||
|
||
// Process will parse an entry for JSON. | ||
|
@@ -36,5 +38,47 @@ func (p *Parser) parse(value any) (any, error) { | |
default: | ||
return nil, fmt.Errorf("type %T cannot be parsed as JSON", value) | ||
} | ||
|
||
if p.useNumber { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing the conversion explicitly here makes me wonder what is the actual reason for defining the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure either. Looking at the encoding/json it looks like not as these many options as json iterator, which makes more sense to me to only expose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what's the point of enabling the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comment, please also check this test that when UseNumber is false, regardless the data is int or float, they will be parsed as |
||
p.convertNumbers(parsedValue) | ||
} | ||
return parsedValue, nil | ||
} | ||
|
||
func (p *Parser) convertNumbers(parsedValue map[string]any) { | ||
for k, v := range parsedValue { | ||
switch t := v.(type) { | ||
case json.Number: | ||
parsedValue[k] = p.convertNumber(t) | ||
case map[string]any: | ||
p.convertNumbers(t) | ||
case []any: | ||
p.convertNumbersArray(t) | ||
} | ||
} | ||
} | ||
|
||
func (p *Parser) convertNumbersArray(arr []any) { | ||
for i, v := range arr { | ||
switch t := v.(type) { | ||
case json.Number: | ||
arr[i] = p.convertNumber(t) | ||
case map[string]any: | ||
p.convertNumbers(t) | ||
case []any: | ||
p.convertNumbersArray(t) | ||
} | ||
} | ||
} | ||
|
||
func (p *Parser) convertNumber(value json.Number) any { | ||
i64, err := value.Int64() | ||
if err == nil { | ||
return i64 | ||
} | ||
f64, err := value.Float64() | ||
if err == nil { | ||
return f64 | ||
} | ||
return value.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, updated.