Skip to content

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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

brooksmtownsend
Copy link

  • 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

@brooksmtownsend brooksmtownsend requested a review from ydnar as a code owner March 7, 2025 17:27
@brooksmtownsend brooksmtownsend marked this pull request as draft March 7, 2025 17:27
Copy link
Collaborator

@ydnar ydnar left a comment

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?

@@ -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) {
Copy link
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.

Copy link
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
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.

Copy link
Author

@brooksmtownsend brooksmtownsend Mar 11, 2025

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.

@brooksmtownsend brooksmtownsend marked this pull request as ready for review March 10, 2025 18:14
@brooksmtownsend brooksmtownsend requested a review from ydnar March 11, 2025 20:39
Copy link
Collaborator

@ydnar ydnar left a comment

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.

}

// UnmarshalJSON implements the json.Unmarshaler interface for the public option type.
func (o *Option[T]) UnmarshalJSON(data []byte) error {
Copy link
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.

@@ -57,3 +59,41 @@ func (o option[T]) Value() T {
}
return o.some
}

// MarshalJSON implements the json.Marshaler interface for the public option type.
func (o Option[T]) MarshalJSON() ([]byte, error) {
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants