{"action":"created","issue":{"url":"https://api.github.com/repos/Araq/Nim/issues/2431","labels_url":"https://api.github.com/repos/Araq/Nim/issues/2431/labels{/name}","comments_url":"https://api.github.com/repos/Araq/Nim/issues/2431/comments","events_url":"https://api.github.com/repos/Araq/Nim/issues/2431/events","html_url":"https://github.com/Araq/Nim/issues/2431","id":65114951,"number":2431,"title":"json {} proc is missing variant to handle varargs[string] (and naming is inconsistent)","user":{"login":"maxgrenderjones","id":6059365,"avatar_url":"https://avatars.githubusercontent.com/u/6059365?v=3","gravatar_id":"","url":"https://api.github.com/users/maxgrenderjones","html_url":"https://github.com/maxgrenderjones","followers_url":"https://api.github.com/users/maxgrenderjones/followers","following_url":"https://api.github.com/users/maxgrenderjones/following{/other_user}","gists_url":"https://api.github.com/users/maxgrenderjones/gists{/gist_id}","starred_url":"https://api.github.com/users/maxgrenderjones/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxgrenderjones/subscriptions","organizations_url":"https://api.github.com/users/maxgrenderjones/orgs","repos_url":"https://api.github.com/users/maxgrenderjones/repos","events_url":"https://api.github.com/users/maxgrenderjones/events{/privacy}","received_events_url":"https://api.github.com/users/maxgrenderjones/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-30T01:28:20Z","updated_at":"2015-04-05T12:20:08Z","closed_at":null,"body":"Warning: nim newbie!\r\n\r\nThe `json` module has a ``{}`` operator defined, with a docstring that suggests it ought to take a `varargs[string]` so that users can provide a list of keys to safely index into a `json` object and get `nil` back if one of the intermediate keys is missing. However the only provided method takes a single `string` parameter:\r\n\r\n```nim\r\nproc `{}`*(node: JsonNode, key: string): JsonNode =\r\n  ## Transverses the node and gets the given value. If any of the\r\n  ## keys do not exist, returns nil\r\n  result = node\r\n  if isNil(node): return nil\r\n  result = result[key]\r\n```\r\n\r\n(Aside: I think this method is equivalent to the  ``[]`` operator, and would anyway be covered by the `varargs[string]` version below, so perhaps it's actually redundant?)\r\n\r\nIt would therefore make sense if we had a varargs[string] version to work with too:\r\n```nim\r\nproc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =\r\n  ## Transverses the node and gets the given value. If any of the\r\n  ## keys do not exist, returns nil\r\n  result = node\r\n  for key in keys:\r\n    if isNil(result) or not result.hasKey(key):\r\n      return nil\r\n    else:\r\n      result=result[key]\r\n```\r\n\r\nAnd while we're about it, ``{}=`` takes `names` as an argument, however everywhere else in the module, such functionality is denoted by `key`. For consistency, I'd therefore recommend renaming `names` to `keys` (a breaking change, but isn't that what the pre v1 is for?)\r\n\r\n```nim\r\nproc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =\r\n  ## Transverses the node and tries to set the value at the given location\r\n  ## to `value` If any of the keys are missing, they are added\r\n  var node = node\r\n  for i in 0..(keys.len-2):\r\n    if isNil(node[keys[i]]):\r\n      node[keys[i]] = newJObject()\r\n    node = node[keys[i]]\r\n  node[keys[keys.len-1]] = value\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Araq/Nim/issues/comments/89757880","html_url":"https://github.com/Araq/Nim/issues/2431#issuecomment-89757880","issue_url":"https://api.github.com/repos/Araq/Nim/issues/2431","id":89757880,"user":{"login":"dom96","id":246651,"avatar_url":"https://avatars.githubusercontent.com/u/246651?v=3","gravatar_id":"","url":"https://api.github.com/users/dom96","html_url":"https://github.com/dom96","followers_url":"https://api.github.com/users/dom96/followers","following_url":"https://api.github.com/users/dom96/following{/other_user}","gists_url":"https://api.github.com/users/dom96/gists{/gist_id}","starred_url":"https://api.github.com/users/dom96/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dom96/subscriptions","organizations_url":"https://api.github.com/users/dom96/orgs","repos_url":"https://api.github.com/users/dom96/repos","events_url":"https://api.github.com/users/dom96/events{/privacy}","received_events_url":"https://api.github.com/users/dom96/received_events","type":"User","site_admin":false},"created_at":"2015-04-05T12:20:08Z","updated_at":"2015-04-05T12:20:08Z","body":"Sure, make a PR :)"}}