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

common interfaces for marshaling primitive kinds #56178

Closed
dsnet opened this issue Oct 12, 2022 · 1 comment
Closed

common interfaces for marshaling primitive kinds #56178

dsnet opened this issue Oct 12, 2022 · 1 comment

Comments

@dsnet
Copy link
Member

dsnet commented Oct 12, 2022

The inspiration for this is #54582.

Adding MarshalJSON methods would work, but is not performant since we would always be allocating, which is unfortunate.

Alternative, these types could implement:

func (x *Int64) MarshalNextJSON(mo json.MarshalOptions, enc *jsontext.Encoder) error { ... }

However, this would cause the atomic package to have a dependency on both the json and jsontext packages, which is unreasonable.

I wonder if we should support something like:

type BoolMarshaler  interface { MarshalBool() (bool, error) }
type IntMarshaler   interface { MarshalInt() (int64, error) }
type UintMarshaler  interface { MarshalUint() (uint64, error) }
type FloatMarshaler interface { MarshalFloat() (float64, error) }

type BoolUnmarshaler  interface { UnmarshalBool(bool) error }
type IntUnmarshaler   interface { UnmarshalInt(int64) error }
type UintUnmarshaler  interface { UnmarshalUint(uint64) error }
type FloatUnmarshaler interface { UnmarshalFloat(float64) error }

Alternatively, we could even have:

type ScalarMarshaler[T bool | int64 | uint64 | float64]   interface { MarshalScalar() (T, error) }
type ScalarUnmarshaler[T bool | int64 | uint64 | float64] interface { UnmarshalScalar(T) error }

With the generics approach, all methods share the same name, but my have different types that it operates on. Perhaps this is a feature since it forces a given type to only implement MarshalScalar for one type. We wouldn't have to define the behavior for when MarshalInt and MarshalUint are both present.

@ianlancetaylor ianlancetaylor changed the title common interfaces for arshaling primitive kinds common interfaces for marshaling primitive kinds Oct 12, 2022
@dsnet
Copy link
Member Author

dsnet commented Oct 12, 2022

Ooops. This was meant to go in a different repo.

@dsnet dsnet closed this as completed Oct 12, 2022
@golang golang locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants