Skip to content

Commit

Permalink
Fix inspecting fields from sources with take (#4461)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Jul 25, 2024
1 parent 20239b3 commit e403496
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/ecto/query/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 245,27 @@ defimpl Inspect, for: Ecto.Query do
@doc false
def expr(expr, names, part) do
expr
|> Macro.traverse(:ok, &{prewalk(&1), &2}, &{postwalk(&1, names, part), &2})
|> Macro.traverse(:ok, &{prewalk(&1, names), &2}, &{postwalk(&1, names, part), &2})
|> elem(0)
|> macro_to_string()
end

defp macro_to_string(expr), do: Macro.to_string(expr)

# Tagged values
defp prewalk(%Ecto.Query.Tagged{value: value, tag: nil}) do
defp prewalk(%Ecto.Query.Tagged{value: value, tag: nil}, _) do
value
end

defp prewalk(%Ecto.Query.Tagged{value: value, tag: tag}) do
defp prewalk(%Ecto.Query.Tagged{value: value, tag: tag}, _) do
{:type, [], [value, tag]}
end

defp prewalk(node) do
defp prewalk({{:., dot_meta, [{:&, _, [ix]}, field]}, meta, []}, names) do
{{:., dot_meta, [binding(names, ix), field]}, meta, []}
end

defp prewalk(node, _) do
node
end

Expand Down
3 changes: 3 additions & 0 deletions test/ecto/query/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 477,9 @@ defmodule Ecto.Query.InspectTest do

assert i(from(p in Post, select: merge(p, %{foo: p.foo}))) ==
~s"from p0 in Inspect.Post, select: merge(p0, %{foo: p0.foo})"

assert i(from(p in Post, select: %{title: p.title}, select_merge: map(p, [:visits]))) ==
~s"from p0 in Inspect.Post, select: merge(%{title: p0.title}, map(p0, [:visits]))"
end

test "select after planner" do
Expand Down

0 comments on commit e403496

Please sign in to comment.