Skip to content

Commit

Permalink
Nested query filters (#302)
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Pachal <[email protected]>
  • Loading branch information
TylerPachal and Tyler Pachal authored Aug 3, 2023
1 parent 828284e commit 8247e10
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/jsonapi/utils/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defmodule JSONAPI.Utils.List do
iex> to_list_of_query_string_components(%{"filters" => %{"age" => 18, "name" => "John"}})
[{"filters[age]", 18}, {"filters[name]", "John"}]
iex> to_list_of_query_string_components(%{"filter" => %{"age" => 18, "car" => %{"make" => "honda", "model" => "civic"}}})
[{"filter[age]", 18}, {"filter[car][make]", "honda"}, {"filter[car][model]", "civic"}]
"""
@spec to_list_of_query_string_components(map()) :: list(tuple())
def to_list_of_query_string_components(map) when is_map(map) do
Expand All @@ -29,7 +31,9 @@ defmodule JSONAPI.Utils.List do
end

defp do_to_list_of_query_string_components({key, value}) when is_map(value) do
Enum.flat_map(value, fn {k, v} -> to_list_of_two_elem_tuple("#{key}[#{k}]", v) end)
Enum.flat_map(value, fn {k, v} ->
do_to_list_of_query_string_components({"#{key}[#{k}]", v})
end)
end

defp do_to_list_of_query_string_components({key, value}),
Expand Down

0 comments on commit 8247e10

Please sign in to comment.