-
Notifications
You must be signed in to change notification settings - Fork 76
feat: support nested filtering in the query parser #366
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
Changes from 1 commit
9c5aa26
e1fbb47
54adec7
d9ee357
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,21 @@ defmodule JSONAPI.Utils.IncludeTree do | |
Internal utility for building trees of resource relationships | ||
""" | ||
|
||
def deep_merge(acc, []), do: acc | ||
|
||
def deep_merge(acc, [{key, val} | tail]) do | ||
acc | ||
|> Keyword.update( | ||
key, | ||
val, | ||
fn | ||
[_first | _rest] = old_val -> deep_merge(old_val, val) | ||
old_val -> Keyword.put([], old_val, val) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we need to do some defensive programming here.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I've addressed concerns over robustness with my latest commit. Please take a look and let me know what you think. I've protected the first case by ensuring we have more keyword list to go before deep merging. |
||
) | ||
|> deep_merge(tail) | ||
end | ||
|
||
@spec put_as_tree(term(), term(), term()) :: term() | ||
def put_as_tree(acc, items, val) do | ||
[head | tail] = Enum.reverse(items) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,12 +64,25 @@ defmodule JSONAPI.QueryParserTest do | |
end | ||
end | ||
|
||
test "parse_filter/2 turns filters key/val pairs" do | ||
test "parse_filter/2 returns filters key/val pairs" do | ||
config = struct(Config, opts: [filter: ~w(name)], view: MyView) | ||
filter = parse_filter(config, %{"name" => "jason"}).filter | ||
assert filter[:name] == "jason" | ||
end | ||
|
||
test "parse_filter/2 handles nested filters" do | ||
config = struct(Config, opts: [filter: ~w(author.username)], view: MyView) | ||
filter = parse_filter(config, %{"author.username" => "jason"}).filter | ||
assert filter[:author][:username] == "jason" | ||
end | ||
|
||
test "parse_filter/2 handles nested filters with overlap" do | ||
config = struct(Config, opts: [filter: ~w(author.username author.id)], view: MyView) | ||
filter = parse_filter(config, %{"author.username" => "jason", "author.id" => "123"}).filter | ||
assert filter[:author][:username] == "jason" | ||
assert filter[:author][:id] == "123" | ||
end | ||
mattpolzin marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about whether Config could come with a filter other than |
||
|
||
test "parse_filter/2 raises on invalid filters" do | ||
config = struct(Config, opts: [], view: MyView) | ||
|
||
|
@@ -89,6 +102,12 @@ defmodule JSONAPI.QueryParserTest do | |
assert parse_include(config, "").include == [] | ||
end | ||
|
||
test "parse_include/2 succeds given valid nested include specified in allowed list" do | ||
config = struct(Config, view: MyView, opts: [include: ~w(comments.user)]) | ||
|
||
assert parse_include(config, "comments.user").include == [comments: :user] | ||
end | ||
mattpolzin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test "parse_include/2 errors with invalid includes" do | ||
config = struct(Config, view: MyView) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.