Skip to content
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

Treat optionals as JSON.null to remove all the explicit unwrapping #4

Closed
helje5 opened this issue Jan 6, 2019 · 3 comments
Closed

Comments

@helje5
Copy link

helje5 commented Jan 6, 2019

The thing you present primarily looks ugly because of the optional chaining at every single step:

if let street = json.person?.address?[0].street.stringValue { }

But it doesn't have to be like that (it is dynamic anyways). I think you could improve that a lot by essentially adding "nil messaging":

if let street = json.person.address[0].street.stringValue { .. }

Instead of returning an optional in every item of the chain, only make the leaf accessors (stringValue, intValue, etc) optional. When a lookup is not successful, return JSON.null (which itself returns JSON.null on all JSON accesses, so that the result propagates).

If you'd like you could also add a JSON.lookupError(parent, key) as a result for missed lookups for debugging purposes (and which also propagates down the call chain, and could even persist the lookup path from the root of the first lookup failure).

@helje5 helje5 changed the title Treat optionals as nil Treat optionals as JSON.null to remove all the explicit unwrapping Jan 6, 2019
@saoudrizwan
Copy link
Owner

Thank you for the awesome suggestion, I just implemented this in version 2.0.0. Give it a try and let me know how you like it!

@helje5
Copy link
Author

helje5 commented Jan 6, 2019

Not worth a notable mention? 😜

@saoudrizwan
Copy link
Owner

It absolutely is 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants