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
Support custom json and base64 encoders for Token and Parser #301
base: main
Are you sure you want to change the base?
Conversation
Very nice! I will do a review over the next days, but it’s basically the direction I envisioned. Just some general pointers: We want to avoid the inclusion of any third party libs, even if it’s only in the tests. I would suggest only testing the interface itself in the test without the actual sonic implementation. |
cd88b49
to
7c726ab
Compare
I've updated the PR according to your advice. |
parser_option.go
Outdated
@@ -125,3 +125,16 @@ func WithStrictDecoding() ParserOption { | |||
p.decodeStrict = true | |||
} | |||
} | |||
|
|||
// WithJSONEncoder supports | |||
func WithJSONEncoder(enc JSONEncoder) ParserOption { |
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.
Missing docstring
thanks @dcalsky |
Co-Authored-By: Christian Banse <oxisto@aybaze.com>
Thanks for the initial work on this @dcalsky. I had another go at this, basically splitting up encoding and decoding and also converting most of the interfaces into function types. This should be pretty good integrated in the existing code base now. It even supports both parsing JSON with and without JSON number (as long as the underlying JSON library supports it). I wonder whether I could also make our "special" base64 options (strict, allow padding) work with external libraries as well. For now, they are disabled when an external decoder is used. If we are happy with this approach, I want to finalise docs and examples. |
@mfridman Do we still want to pursue this? It did gather some interest from the community. If yes, I would do another cleanup pass and add docs and examples/tests. |
I'll review this in the next few days. |
It seems useful, albeit the number of requests for this was quite low. I was mainly waiting to see what comes of golang/go#63397. If the native std lib could be improved then bringing your own encoding package becomes unnecessary? I'm a bit on the fence on this one, but if we get it into a good enough shape I wouldn't block. My main hesitation is all the plumbing we have to do to support this in the current /v5 API. |
This PR adds support for
Token
andParser
using custom JSON and base64 encoders to encode/decode related data for performance and experimentation purpose.By implementing the
Base64Encoder
interface withEncodeToString
andDecodeString
methods to create a custom base64 encoder, while implementingJSONEncoder
withMarshal
andUnmarshal
methods to create a custom json encoder.Then Using
WithJSONEncoder
andWithBase64Encoder
to create a Parser, and usingWithTokenJSONEncoder
andWithTokenBase64Encoder
to create a Token.The disadvantages of this PR are (Any suggestions are welcomed):
Introduce the third-party libs into the go moduleWithJSONEncoder
andWithTokenJSONEncoder
Resolves #168