-
Notifications
You must be signed in to change notification settings - Fork 227
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
feat: allow configuring NewDecoder
via callbacks
#193
base: main
Are you sure you want to change the base?
Conversation
Since gorillaGH-59 was rejected on the basis that it wasn't useful enough to break the public API. I took a stab at an alternate solution that doesn't break the public API. While still allowing one to configure the decoder when defining it as a package global (as recommended by the README). ```go var decoder = NewDecoder(func(d *Decoder) { d.IgnoreUnknownKeys(true) }) ```
Maybe worth keeping both functions: the original one with no parameters, and a new one called something like Concerning the options, why there is an array of functions ? I think one can set all the options no ?
In this case we will not be gaining much. I think your approach would helpful, if there is already predefined options ready to use:
In this case, having something like this would be more elegant:
Also how about the Encoder struct, we should do the same for NewEncoder ? Maybe worth having a second opinion. |
Yeah I'm just a bit unsure how to design the API for that to be able to add new options without breaking backwards compatibility 🤔 You could of course apply the same strategy with the callbacks. But I feel like there must be a nicer approach available when you can start from scratch....
Purely to be able to release it as a non-breaking change since one could still pass in zero arguments to get the old behaviour.
Hmm yeah I guess that would look a bit neater. I guess it could be implemented separately though since the function signature would remain the same.
Most likely! To be honest, I didn't consider it because decoding was my real world use-case.
Indeed 🙂 |
Codecov Report
@@ Coverage Diff @@
## main #193 +/- ##
==========================================
+ Coverage 83.23% 83.33% +0.09%
==========================================
Files 4 4
Lines 710 714 +4
==========================================
+ Hits 591 595 +4
Misses 103 103
Partials 16 16
|
I've finally had the time to look at this PR. I'd agree with @zak905's suggestion of the Let me know your thoughts? Thanks for the effort! |
A problem I noticed now when trying to make it consistent with the // encoder.go
func WithAliasTag(tag string) func(d *Encoder) {
return func(d *Encoder) {
d.SetAliasTag(tag)
}
}
// decoder.go
func WithAliasTag(tag string) func(d *Decoder) { // <- Error! Already defined
return func(d *Decoder) {
d.SetAliasTag(tag)
}
} |
Guess I could do something like |
Hi @lithammer, What do you think about:
The definitions of the constructor functions would be:
|
That seems pretty smooth, not going to lie |
Hi @lithammer, are you intending to provide an implementation ? other issues like #203 may also find this useful ( for adding a new toggle flag). When introducing your changes, I think we can also eventually remove the setter functions like IgnoreUnknownKeys, ZeroEmpty...etc to make the decoder/encoder config immutable. |
My plan was to provide an implementation. But I'm currently on parental leave so I haven't really gotten around to it 😅 I still plan to do it, but if someone beats me to it, that's fine. |
Allright then, enjoy the time off. Looking forward to seeing the implementation when you are back |
Since GH-59 was rejected on the basis that it wasn't useful enough to break the public API. I took a stab at an alternate solution that doesn't break the public API. While still allowing one to configure the decoder when defining it as a package global (as recommended by the README).
Also partially solves GH-93.