-
Notifications
You must be signed in to change notification settings - Fork 532
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
Proposed design changes for optional decoders #226
Comments
This proposal feels 💯 to me. Personally, I would rather have these two types of failures and punt #217 to
That we should have two distinct Failure types to represent these seems correct to me. I would much prefer this be fleshed out rather than cutting the baby in half to force something in to 👍 |
Chiming in here because GitHub reactions have yet to express enough weight for me yet with a 👍. I think you summarized it well with the dichotomy of failure, which is what I expected as user, where we return |
Fixed by #231. |
Add type annotations and access modifiers
This issue is closely related to #217 and #218, but there are a couple of unresolved underlying questions about what the "correct" behavior should be that I think deserve their own discussion.
Setup
Suppose we have a case class like this:
And we want to decode a document like this into
Foo(Some(1))
:Chains of operations
We can currently write a decoder like this:
And confirm that it works on valid input:
If there's no
b
field in the inner JSON object we get aNone
, which I think is pretty reasonable:If there is a
b
field but it's not an integral number, decoding fails (also reasonable to me):If there's no
a
field in the outer object, we also get aNone
(somewhat more questionable to my eye, but still I think the right choice):The next part is where it gets weird. Suppose we do have an
a
field, but its value isn't an object:Or that we don't have an
a
field at all (this is more or less what #217 reports):I think both of these cases should result in decoding failures.
Optional decoders and JSON arrays
A similar batch of issues comes up when our optional decoder uses positional selectors:
This is designed to decode JSON like the following as
Foo(Some(1))
(it says "optionally decode the second element of the JSON array that's the first element of the focus as anInt
"):If there is no second element of the inner array, we get a
None
(fine by me):But we also get a
None
if the first element isn't a JSON array at all (ugh):Proposal
My proposed solution is for optional decoders to consider two kinds of failure. The first kind of failure would be when a navigation operation is appropriate for the current JSON context (array or object), but still fails (either because the array doesn't contain enough elements or the object doesn't contain the key). These failures would result in successful decoding to
None
—even if they occur at the beginning of a chain of operations.The second kind of failure is when the navigation operation isn't appropriate for the current JSON value—e.g. we try to
downField("a")
on a JSON number instead of a JSON object. This kind of mismatch would fail the decoding, not return aNone
.This requires a little more bookkeeping and should probably be implemented by adding some information about operations at the
CursorOp
level, which means the fix for #217 would appear in 0.4.0 and probably wouldn't be backported to 0.3.Does this sound like a reasonable proposal for the behavior of optional decoders? Is anyone uncomfortable with #217 not getting fixed in the 0.3 series?
The text was updated successfully, but these errors were encountered: