Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RodrigoVillar
Copy link

Why this should be merged

Currently, if you try calling json.Marshal() and json.Unmarshal() on a field of type ExporterType, 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 respective ExporterType value.

How this was tested

Need to be documented in RELEASES.md?

Comment on lines 39 to 41
if err != nil && !errors.Is(err, errUnknownExporterType) {
return err
}
Copy link
Collaborator

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

@@ -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), `"`)
Copy link
Contributor

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.

Copy link
Author

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 {
Copy link
Contributor

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.

Copy link
Author

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 {
Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants