-
Notifications
You must be signed in to change notification settings - Fork 581
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
config: Parse model from JSON #4806
base: main
Are you sure you want to change the base?
Changes from 3 commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,9 @@ package config | |||||
|
||||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"io" | ||||||
"strings" | ||||||
"testing" | ||||||
|
||||||
"github.com/stretchr/testify/assert" | ||||||
|
@@ -51,3 +54,43 @@ func TestNewSDK(t *testing.T) { | |||||
require.Equal(t, tt.wantShutdownErr, sdk.Shutdown(context.Background())) | ||||||
} | ||||||
} | ||||||
|
||||||
func TestParseJSON(t *testing.T) { | ||||||
tests := []struct { | ||||||
name string | ||||||
input string | ||||||
wantErr error | ||||||
wantType interface{} | ||||||
}{ | ||||||
{ | ||||||
name: "valid JSON", | ||||||
input: `{"file_format": "json", "disabled": false}`, | ||||||
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. This would return the same result
Suggested change
Reference: https://dev.to/taqkarim/you-might-not-be-using-json-decoder-correctly-in-golang-12mb Maybe it would be safer to change the API to accept Or maybe we should even not add such function, but simply add an an example to docs instead? E.g. func ExampleParseJSON() {
input := `{"file_format": "0.1", "disabled": true}`
var cfg config.OpenTelemetryConfiguration
if err := json.Unmarshal(([]byte)(input), &cfg); err != nil {
panic(err)
}
fmt.Printf("Disabled: %v", cfg.Disabled)
// Output: Disabled: true
} I am leaning towards adding a runnable example in docs instead of adding a function. 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. A runnable example in the docs sounds just fine to me. I can also rewrite this to accept 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 leaning towards a runnable a example to reduce the API surface. When I created #4412 I did NOT want CC @codeboten |
||||||
wantErr: nil, | ||||||
wantType: &OpenTelemetryConfiguration{}, | ||||||
}, | ||||||
{ | ||||||
name: "invalid JSON", | ||||||
input: `{"file_format": "json", "disabled":`, | ||||||
wantErr: io.ErrUnexpectedEOF, | ||||||
wantType: nil, | ||||||
}, | ||||||
{ | ||||||
name: "missing required field", | ||||||
input: `{"foo": "bar"}`, | ||||||
wantErr: fmt.Errorf("field file_format in OpenTelemetryConfiguration: required"), | ||||||
wantType: nil, | ||||||
}, | ||||||
} | ||||||
|
||||||
for _, tt := range tests { | ||||||
t.Run(tt.name, func(t *testing.T) { | ||||||
r := strings.NewReader(tt.input) | ||||||
got, err := ParseJSON(r) | ||||||
if err != nil { | ||||||
require.Equal(t, tt.wantErr.Error(), err.Error()) | ||||||
} else { | ||||||
assert.IsType(t, tt.wantType, got) | ||||||
} | ||||||
}) | ||||||
} | ||||||
} |
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.
Can you make some more advanced test? I am not sure if it works "out of the box". We may be missing JSON tags. See:
opentelemetry-go-contrib/Makefile
Line 322 in ba19074
@codeboten Am I correct?
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.
@pellared / @codeboten, I wasn't super clear on what the expected input would be, so perhaps I've overlooked something. I'd thought the
OpenTelemetryConfiguration
was specifically designed to build the parameters asmapstructures
so the user could choose to define their config using either yaml or json. I (possibly naively) was building this to accept any valid json that had the required property from the configuration input used in the Makefile:opentelemetry-go-contrib/Makefile
Line 324 in ba19074
Perhaps we can clarify what are the expected valid inputs for this (and #4373), and what we'd expect as output on valid/invalid cases? I feel this would help with implementation and writing more comprehensive tests. lmkwyt!
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.
I was thinking of removing this line:
opentelemetry-go-contrib/Makefile
Line 322 in ba19074
As then the
OpenTelemetryConfiguration
type will havemapstructure
,json
andyaml
tags. See:https://github.com/omissis/go-jsonschema/blob/6fcc83b8eebe874eeebc20474775280877cc62fe/main.go#L167-L168
Then we could simply provide examples how one can parse input to
OpenTelemetryConfiguration
using different libraries.The parsing method do NOT need to do any validation e.g. check for required fields. The validation has to be done by
NewSDK
because the user could even createOpenTelemetryConfiguration
"by hand" without parsing any JSON or YAML.