-
Notifications
You must be signed in to change notification settings - Fork 675
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 unmarshal for ExporterType
#3565
base: master
Are you sure you want to change the base?
Add unmarshal for ExporterType
#3565
Conversation
trace/exporter_type.go
Outdated
if err != nil && !errors.Is(err, errUnknownExporterType) { | ||
return err | ||
} |
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.
(optional) I think we could remove the special case error check and just return an error on unknown
trace/exporter_type.go
Outdated
@@ -33,6 +33,16 @@ func (t ExporterType) MarshalJSON() ([]byte, error) { | |||
return []byte(`"` + t.String() + `"`), nil | |||
} | |||
|
|||
func (t *ExporterType) UnmarshalJSON(b []byte) error { | |||
exporterTypeStr := strings.Trim(string(b), `"`) |
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.
Should we be erroring if the quotes are formatted unexpectedly? For instance:
grpc
"grpc
""grpc
""grpc"""
would all be unmarshalled without issue, but non of them are formatted as expected.
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.
Done
@@ -33,6 +33,16 @@ func (t ExporterType) MarshalJSON() ([]byte, error) { | |||
return []byte(`"` + t.String() + `"`), nil | |||
} | |||
|
|||
func (t *ExporterType) UnmarshalJSON(b []byte) error { |
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.
By convention, "null" == string(b)
should be a noop.
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.
Done
@@ -33,6 +33,16 @@ func (t ExporterType) MarshalJSON() ([]byte, error) { | |||
return []byte(`"` + t.String() + `"`), nil | |||
} | |||
|
|||
func (t *ExporterType) UnmarshalJSON(b []byte) error { |
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.
Should we add some test vectors for marshal and unmarshal?
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.
Done
Why this should be merged
Currently, if you try calling
json.Marshal()
andjson.Unmarshal()
on a field of typeExporterType
, this will fail as, when unmarshaling, it will try to convert a string (e.g."grpc"
,"http"
,"unknown"
) into a byte.How this works
This PR fixes the above by adding an
UnmarshalJSON()
, which handles conversions from the strings mentioned above to their respectiveExporterType
value.How this was tested
Need to be documented in RELEASES.md?