-
Notifications
You must be signed in to change notification settings - Fork 14
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
Handle Option[] parameters with no default. #31
Conversation
Codecov Report
@@ Coverage Diff @@
## master #31 +/- ##
=======================================
Coverage 99.16% 99.16%
=======================================
Files 11 11
Lines 120 120
Branches 10 9 -1
=======================================
Hits 119 119
Misses 1 1
Continue to review full report at Codecov.
|
@@ -34,7 +34,11 @@ private[magnolia] object MagnoliaDecoder { | |||
val keyCursor = c.downField(key) | |||
keyCursor.focus match { | |||
case Some(json) => json.as[p.PType](p.typeclass) | |||
case None => p.default.toRight(DecodingFailure("Attempt to decode value on failed cursor", keyCursor.history)) | |||
case None => p.default.map { default => Right(default) }.getOrElse { |
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.
good, I would just use fold:
p.default.fold(p.typeclass.tryDecode(keyCursor))(Right(_))
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.
Sure! I've never liked Option.fold because it puts the None
case first, which is not natural to me, but happy to make the change.
RFAL, thanks! |
Thanks, @adampauls ! |
@adampauls Pushed 0.5.0 release with this change. |
Circe permits case class parameters of type
Option[T]
with no default specified to still be decoded (toNone
) when the parameter is missing. This PR brings circe-magnolia in line with this behavior.