Skip to content
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

Allow map/2 and struct/2 to take subsets of embed fields #4558

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Ecto.Integration.RepoTest do
alias Ecto.Integration.Barebone
alias Ecto.Integration.CompositePk
alias Ecto.Integration.PostUserCompositePk
alias Ecto.Integration.Item
alias Ecto.Integration.ItemColor

test "returns already started for started repos" do
assert {:error, {:already_started, _}} = TestRepo.start_link()
Expand Down Expand Up @@ -1423,6 +1425,32 @@ defmodule Ecto.Integration.RepoTest do
assert p3 == %{id: pid3}
end

@tag :json_extract_path
test "take with embed" do
today = Date.utc_today()

TestRepo.insert!(%Order{
metadata: %{test: "test"},
item: %Item{price: 1, valid_at: today, primary_color: %ItemColor{name: "1"}}
})

TestRepo.insert!(%Order{
item: %Item{price: 2, valid_at: today}
})

TestRepo.insert!(%Order{metadata: %{test3: "test3"}})

[o1, o2, o3] =
Order
|> select([o], map(o, [:metadata, item: [:price, primary_color: [:name]]]))
|> order_by([o], o.id)
|> TestRepo.all()

assert o1 == %{metadata: %{"test" => "test"}, item: %Item{price: 1, primary_color: %ItemColor{name: "1"}}}
assert o2 == %{metadata: nil, item: %Item{price: 2, primary_color: nil}}
assert o3 == %{metadata: %{"test3" => "test3"}, item: nil}
end

test "take with preload assocs" do
%{id: pid} = TestRepo.insert!(%Post{title: "post"})
TestRepo.insert!(%Comment{post_id: pid, text: "comment"})
Expand Down
22 changes: 22 additions & 0 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,14 @@ defmodule Ecto.Query.Planner do
{source, type, writable} = Map.get(dumper, field, {field, :any, :always})
{[{field, type} | types], [select_field(source, ix, writable) | exprs]}

{embed_field, sub_fields}, {types, exprs}
when is_map_key(dumper, embed_field) and is_list(sub_fields) and
not is_map_key(drop, embed_field) ->
{source, type, writable} = Map.get(dumper, embed_field)
embed_field_expr = select_field(source, ix, writable)
embed_path_exprs = embed_paths(sub_fields, embed_field_expr, [], [])
{[{embed_field, {type, sub_fields}} | types], Enum.reverse(embed_path_exprs, exprs)}

_field, acc ->
acc
end)
Expand All @@ -2246,6 +2254,20 @@ defmodule Ecto.Query.Planner do
{{:., [writable: writable], [{:&, [], [ix]}, field]}, [], []}
end

defp embed_paths([], _embed_expr, _curr_path, acc), do: acc

defp embed_paths([field | rest], embed_expr, curr_path, acc)
when is_atom(field) do
path = Enum.reverse([Atom.to_string(field) | curr_path])
expr = {:json_extract_path, [], [embed_expr, path]}
embed_paths(rest, embed_expr, curr_path, [expr | acc])
end

defp embed_paths([{field, sub_fields} | rest], embed_expr, curr_path, acc) do
acc = embed_paths(sub_fields, embed_expr, [Atom.to_string(field) | curr_path], acc)
embed_paths(rest, embed_expr, curr_path, acc)
end

defp get_ix!({:&, _, [ix]} = expr, _kind, query) do
{ix, expr, query}
end
Expand Down
29 changes: 29 additions & 0 deletions lib/ecto/repo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ defmodule Ecto.Repo.Queryable do
@doc """
Load structs from query.
"""
def struct_load!(
[{field, {type, sub_fields}} | types],
values,
acc,
all_nil?,
struct,
adapter
)
when is_list(sub_fields) do
values = combine_embed_values(sub_fields, values, true, %{})
struct_load!([{field, type} | types], values, acc, all_nil?, struct, adapter)
end

def struct_load!([{field, type} | types], [value | values], acc, all_nil?, struct, adapter) do
all_nil? = all_nil? and value == nil
value = load!(type, value, field, struct, adapter)
Expand All @@ -202,6 +215,22 @@ defmodule Ecto.Repo.Queryable do
{Map.merge(struct, Map.new(acc)), values}
end

defp combine_embed_values([], values, true, _acc), do: [nil | values]
defp combine_embed_values([], values, false, acc), do: [acc | values]

defp combine_embed_values([field | fields], [value | values], all_nil?, acc)
when is_atom(field) do
all_nil? = all_nil? and value == nil
combine_embed_values(fields, values, all_nil?, Map.put(acc, field, value))
end

defp combine_embed_values([{field, sub_fields} | fields], values, all_nil?, acc)
when is_atom(field) and is_list(sub_fields) do
[sub_map | values] = combine_embed_values(sub_fields, values, true, %{})
all_nil? = all_nil? and sub_map == nil
combine_embed_values(fields, values, all_nil?, Map.put(acc, field, sub_map))
end

## Helpers

defp execute(operation, name, query, {adapter_meta, opts} = tuplet) do
Expand Down
19 changes: 19 additions & 0 deletions test/ecto/query/planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ defmodule Ecto.Query.PlannerTest do
end
end

defp select_embed_field(embed_field, path, ix) do
embed_expr = {{:., [writable: :always], [{:&, [], [ix]}, embed_field]}, [], []}
{:json_extract_path, [], [embed_expr, path]}
end

test "plan: merges all parameters" do
uuid = Ecto.UUID.generate()
{:ok, dump_uuid} = Ecto.UUID.dump(uuid)
Expand Down Expand Up @@ -2317,6 +2322,20 @@ defmodule Ecto.Query.PlannerTest do
select_fields([:b], 1)
end

test "normalize: select with map/2 on embed" do
query = Post |> select([p], map(p, [:title, meta: [:slug, author: [:name]]])) |> normalize()

expected_fields = select_fields([:post_title], 0)

expected_embed_fields =
Enum.map([["slug"], ["author", "name"]], fn path ->
select_embed_field(:meta, path, 0)
end)

assert query.select.expr == {:&, [], [0]}
assert query.select.fields == expected_fields ++ expected_embed_fields
end

test "normalize: select_merge with map/2 does not duplicate fields" do
{query, _, _, _} =
from(s in "schema", select: %{id: s.id})
Expand Down
Loading