8000 Support array remove with negative index by ashanbrown · Pull Request #60 · evanphx/json-patch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support array remove with negative index #60

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

Merged
merged 4 commits into from
Sep 7, 2018

Conversation

ashanbrown
Copy link
Contributor

Also return an error when when a negative index is out of range

}
idx = len(ary) - idx
}
if idx < 0 || idx >= len(ary) || idx > len(cur) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the > and >= checks here are redundant so I removed one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, multiplying the index by -1 before reporting an error made the error message a bit misleading, so I check the index before altering it.

8000
@ashanbrown
Copy link
Contributor Author

Sorry about that last comment. Put it on the wrong PR. ;) . I think this PR should work fine. I'm resolving the changes after you merged my other PR (thanks for that!).

@evanphx
Copy link
Owner
evanphx commented Jul 23, 2018

I don’t believe negative indexes are in the RFC...

@clagraff
Copy link
Contributor

I did a quick check @evanphx

JSON Patch RFC 6902 section 4.1:

If the "-" character is used to index the end of the array (see [RFC6901]), this has the effect of appending the value to the array.

JSON Pointer RFC 6901 Section 4

Note that the use of the "-" character to index an array will always result in such an error condition because by definition it refers to a nonexistent array element.

So it seems like only - is explicitly supported by the RFC. No other negative indices like-1, -2, -3, etc etc.


The repo currently supports the use of negative indexes, as indicated in this test.

So I'd think this PR would be a good addition for now, and if you wanted to remove the ability to specify negative indices that might be best tackled in a separate pr?

@ashanbrown
Copy link
Contributor Author

FWIW, I created this PR just because I wanted the error handling. But when I saw that “add” handled negative indices, I figured we needed to support that as well. We personally don’t use or expect them.

@evanphx evanphx merged commit d58c2bf into evanphx:master Sep 7, 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.

3 participants
0