federicotdn / verb

Organize and send HTTP requests from Emacs
https://melpa.org/#/verb
GNU General Public License v3.0
523 stars 21 forks source link

Handle negative indexes #46

Closed bigodel closed 1 year ago

bigodel commented 2 years ago

This PR allows the user to specify negative indexes when getting values from the response. That means that, given the following response JSON

{
  "foo": {
    "test": "Hello, World!",
    "nested": {"x": 99}
  },
  "bar": [42, 100, true, false, null],
  "empty-array":  [],
  "empty-object": {}
}

The user could access the last element of the "bar" key with (verb-json-get (oref verb-last body) "bar" -1). For the second to last, the user would call (verb-json-get (oref verb-last body) "bar" -2) and so forth.

If the requested negative index tries to access an element that isn't on the bounds, such as trying to access index -6 of "bar" in our example, an error is thrown just as it would for positive indexes.

bigodel commented 2 years ago

i have also taken the liberty to clean up and improve some functions that i ended up stumbling upon, but i could always create a new PR with those changes and some more, in due time.

i have also tried to format the verb-test.el file like verb.el. previously, verb.el was using spaces whilst verb-test.el was using tabs, which resulted in a bunch of lines with spurious indentation. if you don't like said change, i've added it as the last commit of the PR making it easier to just revert it before merging.

bigodel commented 1 year ago

Hey! Apologies for the delay. I liked your idea, I was just wondering about some additional changes I saw on the commit.

i should be the one apologizing :P. somehow i wasn't notified of your response, and only recently have i came back here to check on the status. i have addressed your comments

Would you mind also doing the indentation changes to verb-test.el in a separate PR? Thank you!

sure! i reverted it here

federicotdn commented 1 year ago

@bigodel Looks like there's a linting error, should be easy to fix:

emacs --batch -l vendor/package-lint/package-lint.el \
         -f package-lint-batch-and-exit "verb.el"
Entering directory '/home/runner/work/verb/verb/'
verb.el:1093:20: error: You should depend on (emacs "27.1") if you need `ignore-error'.
bigodel commented 1 year ago

Looks very good! I'll merge it

thank you!