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

Add querying helpers #3

Merged
merged 2 commits into from
Jul 1, 2018
Merged

Add querying helpers #3

merged 2 commits into from
Jul 1, 2018

Conversation

zoul
Copy link
Collaborator

@zoul zoul commented Jun 30, 2018

These are helpers to query values stored in a JSON value proposed by @cjmconie in #2. Cameron, is this OK? I have mostly just copied your code and added some tests.

@zoul zoul self-assigned this Jun 30, 2018
return nil
}

/// Return `true` iff this is `.null`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo "iff"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

///
/// If this is not an `.array` or the index is out of bounds, returns `nil`.
public subscript(index: Int) -> JSON? {
if case .array(let arr) = self, index < arr.count {
Copy link
Collaborator

@cjmconie cjmconie Jul 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check the upper bound of the array here, does it make sense to check the lower bound (i.e. index >= 0) too? Currently, an overflow in one direction returns nil and the other direction will trigger a fatal index out of range exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Will do.

@zoul zoul merged commit c659ba7 into master Jul 1, 2018
@cjmconie cjmconie mentioned this pull request Jul 1, 2018
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

Successfully merging this pull request may close these issues.

2 participants