From afa436975f25f9bde6857ab2970f60b4d39a3ed7 Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 24 Feb 2024 03:58:39 -0500 Subject: [PATCH] inspect --- lib/ecto/query/inspect.ex | 6 ++++++ test/ecto/query/inspect_test.exs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/ecto/query/inspect.ex b/lib/ecto/query/inspect.ex index faa926f2ed..107b4f6bdc 100644 --- a/lib/ecto/query/inspect.ex +++ b/lib/ecto/query/inspect.ex @@ -275,6 +275,12 @@ defimpl Inspect, for: Ecto.Query do binding_to_expr(ix, names, part) end + # Format field/2 with string name + defp postwalk({{:., _, [{_, _, _} = binding, field]}, meta, []}, _names, _part) + when is_binary(field) do + {:field, meta, [binding, field]} + end + # Remove parens from field calls defp postwalk({{:., _, [_, _]} = dot, meta, []}, _names, _part) do {dot, [no_parens: true] ++ meta, []} diff --git a/test/ecto/query/inspect_test.exs b/test/ecto/query/inspect_test.exs index 826fbb01fa..a0ece93445 100644 --- a/test/ecto/query/inspect_test.exs +++ b/test/ecto/query/inspect_test.exs @@ -505,6 +505,11 @@ defmodule Ecto.Query.InspectTest do assert i(plan(query)) == "from v0 in values (#{fields})" end + test "field/2 with string name" do + query = from p in Post, select: field(p, "visit") + assert i(query) == ~s + end + def plan(query) do {query, _, _} = Ecto.Adapter.Queryable.plan_query(:all, Ecto.TestAdapter, query) query