-
Notifications
You must be signed in to change notification settings - Fork 544
[generic-extra] Decode default value on Option if key is missing. #994
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
Conversation
fc477a0
to
1c37551
Compare
Codecov Report
@@ Coverage Diff @@
## master #994 +/- ##
==========================================
- Coverage 85.49% 85.21% -0.29%
==========================================
Files 75 75
Lines 2386 2388 +2
Branches 174 181 +7
==========================================
- Hits 2040 2035 -5
- Misses 346 353 +7
Continue to review full report at Codecov.
|
1c37551
to
28d16b7
Compare
d9d3bb9
to
7d33d8e
Compare
@@ -34,10 +34,10 @@ abstract class ReprDecoder[A] extends Decoder[A] { | |||
name: String, | |||
defaults: Map[String, Any] | |||
): Decoder.Result[B] = result match { | |||
case r @ Right(_) => r | |||
case l @ Left(_) => defaults.get(name) match { | |||
case r @ Right(_) if r ne Decoder.keyMissingNone => r |
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.
Why not just case r @ Right(v) if v.isEmpty => r
?
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.
This trick is required to discriminate null-given case from key-missing case.
If null given, it is dedoded to Decoder.rightNone
which is ne Decoder.keyMissingNone
, so Right(None)
will be returned.
Otherwise, it tests there is default or not, and return the found default value in case of Decoder.keyMissingNone
.
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.
@travisbrown
Can I have your moment to get review?
Perhaps, you may missed my response due to last GitHub outage.
Thanks in advance 😃
b9a371e
to
0d0f2d0
Compare
0d0f2d0
to
e3758c6
Compare
Thanks much, @exoego—sorry for the long delay in review, but this looks reasonable to me, and will be released in 0.11.0. |
@exoego Thank you for this! |
Closes #878
Foo(a: Option[Int])
Foo(a: Option[Int]=Some(0))
{"a":null}
Foo(None)
Foo(None)
{}
Foo(None)
Foo(None)
This PR:
Foo(Some(0))