{"action":"created","comment":{"url":"https://api.github.com/repos/nalundgaard/jsn/pulls/comments/138742565","pull_request_review_id":62569412,"id":138742565,"diff_hunk":"@@ -357,6 +359,71 @@ path_elements(_Type, _Path, _Acc) -> erlang:error(badarg).\n \n \n %%==============================================================================\n+%% select API\n+%%==============================================================================\n+\n+%%------------------------------------------------------------------------------\n+%% @doc Transforms a list of Elements according to a given OutputSpecification\n+%%------------------------------------------------------------------------------\n+-spec select(selection(), json_array()) -> json_array().\n+select(OutputSpec, Elements) when is_list(Elements) ->\n+    select(OutputSpec, [], Elements);\n+select(_OutpuSpec, _Elements) ->\n+    erlang:error(badarg).\n+\n+%%------------------------------------------------------------------------------\n+%% @doc Same as select/2 but it first filters the elements according to a given\n+%% list of Conditions\n+%%------------------------------------------------------------------------------\n+-spec select(selection(), conditions(), json_array()) -> json_array().\n+select(OutputSpec, Conditions, Elements) when is_list(Elements) ->\n+    %% ensures Conditions are a list\n+    ConditionList =\n+        case Conditions of\n+            _ when is_list(Conditions) -> Conditions;\n+            _ -> [Conditions]\n+        end,\n+    %% This function applies a single condition to a single element\n+    ApplyConditionFun = \n+        fun({Path, FilterFun}, E) when is_function(FilterFun) ->\n+                erlang:apply(FilterFun, [?MODULE:get(Path, E)]);\n+           ({Path, Value}, E) ->\n+                ?MODULE:get(Path, E) == Value;\n+           (FilterFun, E) when is_function(FilterFun) ->\n+                erlang:apply(FilterFun, [E]);\n+           (_, _) ->\n+                erlang:error(badarg)\n+        end,\n+    FilteredElements =\n+        lists:foldl(fun(Condition, CurrentElements) ->\n+                        lists:filter(fun(E) ->\n+                                        erlang:apply(ApplyConditionFun, [Condition, E])\n+                                    end, CurrentElements)\n+                    end, Elements, ConditionList),\n+    %% Converts Filtered Elements by applying output specifications\n+    OutputFun =\n+        case OutputSpec of\n+            identity ->\n+                fun(E) -> E end;\n+            {value, Path} ->\n+                fun(E) -> ?MODULE:get(Path, E) end;\n+            {value, Path, Default} ->\n+                fun(E) -> ?MODULE:get(Path, E, Default) end;\n+            _ when is_list(OutputSpec) ->\n+                fun(E) ->\n+                    lists:map(fun({value, Path}) -> ?MODULE:get(Path, E);\n+                                 ({value, Path, Default}) -> ?MODULE:get(Path, E, Default)\n+                              end, OutputSpec)\n+                end;\n+            _ ->\n+                erlang:error(badarg)","path":"src/jsn.erl","position":70,"original_position":70,"commit_id":"8d5604a0907811376a7bb16d0fcc11d528102e10","original_commit_id":"8d5604a0907811376a7bb16d0fcc11d528102e10","user":{"login":"nalundgaard","id":1851445,"avatar_url":"https://avatars3.githubusercontent.com/u/1851445?v=4","gravatar_id":"","url":"https://api.github.com/users/nalundgaard","html_url":"https://github.com/nalundgaard","followers_url":"https://api.github.com/users/nalundgaard/followers","following_url":"https://api.github.com/users/nalundgaard/following{/other_user}","gists_url":"https://api.github.com/users/nalundgaard/gists{/gist_id}","starred_url":"https://api.github.com/users/nalundgaard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nalundgaard/subscriptions","organizations_url":"https://api.github.com/users/nalundgaard/orgs","repos_url":"https://api.github.com/users/nalundgaard/repos","events_url":"https://api.github.com/users/nalundgaard/events{/privacy}","received_events_url":"https://api.github.com/users/nalundgaard/received_events","type":"User","site_admin":false},"body":"Same issue here W.R.T. traceable exceptions. Either this should include the bad value that triggered the input, or it should fail at a function level where the immediate argument that triggered the failure is clear.","created_at":"2017-09-13T21:19:11Z","updated_at":"2017-09-13T21:20:55Z","html_url":"https://github.com/nalundgaard/jsn/pull/13#discussion_r138742565","pull_request_url":"https://api.github.com/repos/nalundgaard/jsn/pulls/13","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/comments/138742565"},"html":{"href":"https://github.com/nalundgaard/jsn/pull/13#discussion_r138742565"},"pull_request":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/nalundgaard/jsn/pulls/13","id":140914021,"html_url":"https://github.com/nalundgaard/jsn/pull/13","diff_url":"https://github.com/nalundgaard/jsn/pull/13.diff","patch_url":"https://github.com/nalundgaard/jsn/pull/13.patch","issue_url":"https://api.github.com/repos/nalundgaard/jsn/issues/13","number":13,"state":"open","locked":false,"title":"implements select()","user":{"login":"juantascon","id":309519,"avatar_url":"https://avatars0.githubusercontent.com/u/309519?v=4","gravatar_id":"","url":"https://api.github.com/users/juantascon","html_url":"https://github.com/juantascon","followers_url":"https://api.github.com/users/juantascon/followers","following_url":"https://api.github.com/users/juantascon/following{/other_user}","gists_url":"https://api.github.com/users/juantascon/gists{/gist_id}","starred_url":"https://api.github.com/users/juantascon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juantascon/subscriptions","organizations_url":"https://api.github.com/users/juantascon/orgs","repos_url":"https://api.github.com/users/juantascon/repos","events_url":"https://api.github.com/users/juantascon/events{/privacy}","received_events_url":"https://api.github.com/users/juantascon/received_events","type":"User","site_admin":false},"body":"Initial implementation of select() according to https://github.com/nalundgaard/jsn/issues/10\r\n\r\nThis implementation uses a few anonymous functions but I'm not sure you guys like this syntax. I could change it to private functions instead or just pass the functions directly to the lists:*\r\n\r\nAdditionally, I'm not 100% ok with the documentation I added but also I'm not sure I should explain all the input options here or should we just leave it to the user to go to jsn.hrl and figure it out by himself","created_at":"2017-09-13T19:48:49Z","updated_at":"2017-09-13T21:20:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"be0f19bd79d8031d33534d2c1a6c110320178681","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/nalundgaard/jsn/pulls/13/commits","review_comments_url":"https://api.github.com/repos/nalundgaard/jsn/pulls/13/comments","review_comment_url":"https://api.github.com/repos/nalundgaard/jsn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nalundgaard/jsn/issues/13/comments","statuses_url":"https://api.github.com/repos/nalundgaard/jsn/statuses/8d5604a0907811376a7bb16d0fcc11d528102e10","head":{"label":"juantascon:select","ref":"select","sha":"8d5604a0907811376a7bb16d0fcc11d528102e10","user":{"login":"juantascon","id":309519,"avatar_url":"https://avatars0.githubusercontent.com/u/309519?v=4","gravatar_id":"","url":"https://api.github.com/users/juantascon","html_url":"https://github.com/juantascon","followers_url":"https://api.github.com/users/juantascon/followers","following_url":"https://api.github.com/users/juantascon/following{/other_user}","gists_url":"https://api.github.com/users/juantascon/gists{/gist_id}","starred_url":"https://api.github.com/users/juantascon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juantascon/subscriptions","organizations_url":"https://api.github.com/users/juantascon/orgs","repos_url":"https://api.github.com/users/juantascon/repos","events_url":"https://api.github.com/users/juantascon/events{/privacy}","received_events_url":"https://api.github.com/users/juantascon/received_events","type":"User","site_admin":false},"repo":{"id":101423315,"name":"jsn","full_name":"juantascon/jsn","owner":{"login":"juantascon","id":309519,"avatar_url":"https://avatars0.githubusercontent.com/u/309519?v=4","gravatar_id":"","url":"https://api.github.com/users/juantascon","html_url":"https://github.com/juantascon","followers_url":"https://api.github.com/users/juantascon/followers","following_url":"https://api.github.com/users/juantascon/following{/other_user}","gists_url":"https://api.github.com/users/juantascon/gists{/gist_id}","starred_url":"https://api.github.com/users/juantascon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juantascon/subscriptions","organizations_url":"https://api.github.com/users/juantascon/orgs","repos_url":"https://api.github.com/users/juantascon/repos","events_url":"https://api.github.com/users/juantascon/events{/privacy}","received_events_url":"https://api.github.com/users/juantascon/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/juantascon/jsn","description":"Utilities for interacting with decoded JSON in erlang","fork":true,"url":"https://api.github.com/repos/juantascon/jsn","forks_url":"https://api.github.com/repos/juantascon/jsn/forks","keys_url":"https://api.github.com/repos/juantascon/jsn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/juantascon/jsn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/juantascon/jsn/teams","hooks_url":"https://api.github.com/repos/juantascon/jsn/hooks","issue_events_url":"https://api.github.com/repos/juantascon/jsn/issues/events{/number}","events_url":"https://api.github.com/repos/juantascon/jsn/events","assignees_url":"https://api.github.com/repos/juantascon/jsn/assignees{/user}","branches_url":"https://api.github.com/repos/juantascon/jsn/branches{/branch}","tags_url":"https://api.github.com/repos/juantascon/jsn/tags","blobs_url":"https://api.github.com/repos/juantascon/jsn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/juantascon/jsn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/juantascon/jsn/git/refs{/sha}","trees_url":"https://api.github.com/repos/juantascon/jsn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/juantascon/jsn/statuses/{sha}","languages_url":"https://api.github.com/repos/juantascon/jsn/languages","stargazers_url":"https://api.github.com/repos/juantascon/jsn/stargazers","contributors_url":"https://api.github.com/repos/juantascon/jsn/contributors","subscribers_url":"https://api.github.com/repos/juantascon/jsn/subscribers","subscription_url":"https://api.github.com/repos/juantascon/jsn/subscription","commits_url":"https://api.github.com/repos/juantascon/jsn/commits{/sha}","git_commits_url":"https://api.github.com/repos/juantascon/jsn/git/commits{/sha}","comments_url":"https://api.github.com/repos/juantascon/jsn/comments{/number}","issue_comment_url":"https://api.github.com/repos/juantascon/jsn/issues/comments{/number}","contents_url":"https://api.github.com/repos/juantascon/jsn/contents/{+path}","compare_url":"https://api.github.com/repos/juantascon/jsn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/juantascon/jsn/merges","archive_url":"https://api.github.com/repos/juantascon/jsn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/juantascon/jsn/downloads","issues_url":"https://api.github.com/repos/juantascon/jsn/issues{/number}","pulls_url":"https://api.github.com/repos/juantascon/jsn/pulls{/number}","milestones_url":"https://api.github.com/repos/juantascon/jsn/milestones{/number}","notifications_url":"https://api.github.com/repos/juantascon/jsn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/juantascon/jsn/labels{/name}","releases_url":"https://api.github.com/repos/juantascon/jsn/releases{/id}","deployments_url":"https://api.github.com/repos/juantascon/jsn/deployments","created_at":"2017-08-25T16:42:12Z","updated_at":"2017-08-25T16:42:13Z","pushed_at":"2017-09-13T19:46:34Z","git_url":"git://github.com/juantascon/jsn.git","ssh_url":"git@github.com:juantascon/jsn.git","clone_url":"https://github.com/juantascon/jsn.git","svn_url":"https://github.com/juantascon/jsn","homepage":"","size":1046,"stargazers_count":0,"watchers_count":0,"language":"Erlang","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"nalundgaard:master","ref":"master","sha":"e0e924e428838adf1b67db131796bcc1cdaa1e60","user":{"login":"nalundgaard","id":1851445,"avatar_url":"https://avatars3.githubusercontent.com/u/1851445?v=4","gravatar_id":"","url":"https://api.github.com/users/nalundgaard","html_url":"https://github.com/nalundgaard","followers_url":"https://api.github.com/users/nalundgaard/followers","following_url":"https://api.github.com/users/nalundgaard/following{/other_user}","gists_url":"https://api.github.com/users/nalundgaard/gists{/gist_id}","starred_url":"https://api.github.com/users/nalundgaard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nalundgaard/subscriptions","organizations_url":"https://api.github.com/users/nalundgaard/orgs","repos_url":"https://api.github.com/users/nalundgaard/repos","events_url":"https://api.github.com/users/nalundgaard/events{/privacy}","received_events_url":"https://api.github.com/users/nalundgaard/received_events","type":"User","site_admin":false},"repo":{"id":39092441,"name":"jsn","full_name":"nalundgaard/jsn","owner":{"login":"nalundgaard","id":1851445,"avatar_url":"https://avatars3.githubusercontent.com/u/1851445?v=4","gravatar_id":"","url":"https://api.github.com/users/nalundgaard","html_url":"https://github.com/nalundgaard","followers_url":"https://api.github.com/users/nalundgaard/followers","following_url":"https://api.github.com/users/nalundgaard/following{/other_user}","gists_url":"https://api.github.com/users/nalundgaard/gists{/gist_id}","starred_url":"https://api.github.com/users/nalundgaard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nalundgaard/subscriptions","organizations_url":"https://api.github.com/users/nalundgaard/orgs","repos_url":"https://api.github.com/users/nalundgaard/repos","events_url":"https://api.github.com/users/nalundgaard/events{/privacy}","received_events_url":"https://api.github.com/users/nalundgaard/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nalundgaard/jsn","description":"Utilities for interacting with decoded JSON in erlang","fork":false,"url":"https://api.github.com/repos/nalundgaard/jsn","forks_url":"https://api.github.com/repos/nalundgaard/jsn/forks","keys_url":"https://api.github.com/repos/nalundgaard/jsn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nalundgaard/jsn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nalundgaard/jsn/teams","hooks_url":"https://api.github.com/repos/nalundgaard/jsn/hooks","issue_events_url":"https://api.github.com/repos/nalundgaard/jsn/issues/events{/number}","events_url":"https://api.github.com/repos/nalundgaard/jsn/events","assignees_url":"https://api.github.com/repos/nalundgaard/jsn/assignees{/user}","branches_url":"https://api.github.com/repos/nalundgaard/jsn/branches{/branch}","tags_url":"https://api.github.com/repos/nalundgaard/jsn/tags","blobs_url":"https://api.github.com/repos/nalundgaard/jsn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nalundgaard/jsn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nalundgaard/jsn/git/refs{/sha}","trees_url":"https://api.github.com/repos/nalundgaard/jsn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nalundgaard/jsn/statuses/{sha}","languages_url":"https://api.github.com/repos/nalundgaard/jsn/languages","stargazers_url":"https://api.github.com/repos/nalundgaard/jsn/stargazers","contributors_url":"https://api.github.com/repos/nalundgaard/jsn/contributors","subscribers_url":"https://api.github.com/repos/nalundgaard/jsn/subscribers","subscription_url":"https://api.github.com/repos/nalundgaard/jsn/subscription","commits_url":"https://api.github.com/repos/nalundgaard/jsn/commits{/sha}","git_commits_url":"https://api.github.com/repos/nalundgaard/jsn/git/commits{/sha}","comments_url":"https://api.github.com/repos/nalundgaard/jsn/comments{/number}","issue_comment_url":"https://api.github.com/repos/nalundgaard/jsn/issues/comments{/number}","contents_url":"https://api.github.com/repos/nalundgaard/jsn/contents/{+path}","compare_url":"https://api.github.com/repos/nalundgaard/jsn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nalundgaard/jsn/merges","archive_url":"https://api.github.com/repos/nalundgaard/jsn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nalundgaard/jsn/downloads","issues_url":"https://api.github.com/repos/nalundgaard/jsn/issues{/number}","pulls_url":"https://api.github.com/repos/nalundgaard/jsn/pulls{/number}","milestones_url":"https://api.github.com/repos/nalundgaard/jsn/milestones{/number}","notifications_url":"https://api.github.com/repos/nalundgaard/jsn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nalundgaard/jsn/labels{/name}","releases_url":"https://api.github.com/repos/nalundgaard/jsn/releases{/id}","deployments_url":"https://api.github.com/repos/nalundgaard/jsn/deployments","created_at":"2015-07-14T18:17:46Z","updated_at":"2017-06-22T08:31:58Z","pushed_at":"2017-09-13T19:48:49Z","git_url":"git://github.com/nalundgaard/jsn.git","ssh_url":"git@github.com:nalundgaard/jsn.git","clone_url":"https://github.com/nalundgaard/jsn.git","svn_url":"https://github.com/nalundgaard/jsn","homepage":"","size":1045,"stargazers_count":24,"watchers_count":24,"language":"Erlang","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":24,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/13"},"html":{"href":"https://github.com/nalundgaard/jsn/pull/13"},"issue":{"href":"https://api.github.com/repos/nalundgaard/jsn/issues/13"},"comments":{"href":"https://api.github.com/repos/nalundgaard/jsn/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nalundgaard/jsn/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/nalundgaard/jsn/statuses/8d5604a0907811376a7bb16d0fcc11d528102e10"}},"author_association":"NONE"}}
{"actor":{"display_login":"nalundgaard"}}