-
Notifications
You must be signed in to change notification settings - Fork 16
cm: implement JSON [un]marshaling for Option types #301
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
Open
brooksmtownsend
wants to merge
4
commits into
bytecodealliance:main
Choose a base branch
from
brooksmtownsend:feat/option-json-marshal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce4ca98
cm: implement JSON [un]marshaling for Option types
brooksmtownsend e755c07
cm: test JSON [un]marshaling for Option types
brooksmtownsend 450b98c
cm: implement JSON [un]marshaling for option types
brooksmtownsend c7d2e56
cm: test JSON [un]marshaling for named and nested options
brooksmtownsend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package cm | ||
|
||
import "encoding/json" | ||
|
||
// Option represents a Component Model [option<T>] type. | ||
// | ||
// [option<T>]: https://component-model.bytecodealliance.org/design/wit.html#options | ||
|
@@ -57,3 +59,25 @@ func (o option[T]) Value() T { | |
} | ||
return o.some | ||
} | ||
|
||
// MarshalJSON implements the json.Marshaler interface. | ||
func (o Option[T]) MarshalJSON() ([]byte, error) { | ||
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 is not necessary. |
||
if !o.isSome { | ||
return json.Marshal(nil) | ||
} | ||
return json.Marshal(o.Some()) | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface. | ||
func (o *Option[T]) UnmarshalJSON(data []byte) error { | ||
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 is not necessary. |
||
if string(data) == "null" { | ||
*o = None[T]() | ||
return nil | ||
} | ||
var v T | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
*o = Some(v) | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Methods should be on option[T] so named option types inherit the methods.
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.
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?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.
This is why the methods are implemented on
option
, which is embedded inOption
, so methods are preserved.Uh oh!
There was an error while loading. Please reload this page.
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.
@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 removingOption[T].UnmarshalJSON
did fail the cases that we have, so I left both in.