Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

cm: implement JSON [un]marshaling for Option types#301

Open
brooksmtownsend wants to merge 4 commits into
bytecodealliance:main from
brooksmtownsend:feat/option-json-marshal
Open

cm: implement JSON [un]marshaling for Option types #301
brooksmtownsend wants to merge 4 commits into
bytecodealliance:main from
brooksmtownsend:feat/option-json-marshal

Conversation

@brooksmtownsend

@brooksmtownsend brooksmtownsend commented Mar 7, 2025

Copy link
Copy Markdown
  • cm: implement JSON [un]marshaling for Option types
  • cm: test JSON [un]marshaling for Option types

This PR implements (naively) option marshaling/unmarshaling for Option types. This case is fairly straightforward, marshal to null if None and to the value if present.

Opening this as a draft just to ensure that this is a change that's well received that follows contribution structure, open to changing anything here!

Addresses Option un/marshaling for #239

lxfontes reacted with heart emoji
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>

@ydnar ydnar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

This seems roughly right, but how would option<option> serialize and deserialize symmetrically?

Comment thread cm/option.go
}

// MarshalJSON implements the json.Marshaler interface.
func (o Option[T]) MarshalJSON() ([]byte, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Methods should be on option[T] so named option types inherit the methods.

@brooksmtownsend brooksmtownsend Mar 10, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Is there an example I can follow for a named option type? I assumed that if external implementations named a type it would be for Option[T], perhaps I should implement the un/marshaling for both?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

// This type would not have the JSON methods.
type OptionalI32 cm.Option[int32]

This is why the methods are implemented on option, which is embedded in Option, so methods are preserved.

brooksmtownsend reacted with thumbs up emoji

@brooksmtownsend brooksmtownsend Mar 11, 2025
edited
Loading

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@ydnar added a few more test cases that show that this works for named and nested options 🫡

I did find that I could remove the Option[T].MarshalJSON implementation and still have tests work, but removing Option[T].UnmarshalJSON did fail the cases that we have, so I left both in.

Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
@brooksmtownsend brooksmtownsend marked this pull request as ready for review March 10, 2025 18:14
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>

@ydnar ydnar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Can you convert the tests to table driven?
  • Please remove the methods on Option[T]. They will not be used for named option types.
  • Please use a serialization format that is symmetrical. "null" for none is not it.

Comment thread cm/option.go
}

// UnmarshalJSON implements the json.Unmarshaler interface for the public option type.
func (o *Option[T]) UnmarshalJSON(data []byte) error {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not necessary.

Comment thread cm/option.go
}

// MarshalJSON implements the json.Marshaler interface for the public option type.
func (o Option[T]) MarshalJSON() ([]byte, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ydnar ydnar ydnar requested changes

Requested changes must be addressed to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /