Closed joel-u410 closed 1 year ago
Could you please add the unmarshaler?
Yep, will do!
The reason I hadn't originally implemented unmarshalling is because it's lossy going to JSON -- the error type is lost (only the message is marshaled).
Are you ok with an unmarshal implementation that simply uses errors.New
to construct a generic error object for the Err
case?
Here's a first pass at implementing UnmarshalJSON
-- please let me know if you agree with the implementation, particularly around malformed inputs (e.g. both result
and error
populated --> Err
)
Ok, I understand the limit of unmarshaling.
Using errors.New looks like a good workaround.
I thought it would be useful to support JSON marshaling for
Result[T]
. This would likely be used within the context of building a JSON-RPC service, so I implemented it here in a way that generally conforms to the JSON-RPC spec.There is one notable exception, the spec requires a
code
field in anerror
object that indicates the type of error that occurred, and this information is not readily available from anerror
in Go. So I left that out, resulting in slight nonconformance to the spec.I had thought about always including something like
code: -32000
as a general server error, but it seemed better to me to leave it out rather than inserting something that may or may not be correct in all situations.If you think this is too opinionated a way to represent a
Result[T]
in JSON, feel free to reject & close this PR. 🙂