inspect/2 and its friend IO.inspect/2 are intended as debugging aids. So its very helpful to encode non-printing characters so a developer can see clearly the content. Noting that the Inspect protocol is implemented per data type - what you are describing is the behaviour for a String.t().
As you are seeing, the String.Chars protocol is intended to produce a string for “human” consumption hence implementations of the protocol (including for String.t() doesn’t encode non-printing characters.
Also you should be just able to do:
IO.inspect from(u in User, where: u.id == ^1))
since IO.inspect/2 will leverage the Inspect protocol implementation defined for %Ecto.Query{}.
The reason I do not write IO.inspect query is because it will then print:
ecto.Query<from u0 in MyApp.User, where: u0.id == ^1>
That’s why I rely on Inspect.Ecto.Query.to_string
The reason I like IO.inspect is because it returns the inspected value + I find its :label very useful. I guess I’ll stick with IO.puts in order to print the return lines, but I will write a utility function wrapping it, to make it behave like inspect (return the query instead of :ok and allow to add a label).
** (Protocol.UndefinedError) protocol String.Chars not implemented for ecto.Query<…> of type Ecto.Query (a struct). This protocol is implemented for the following type(s): Postgrex.Copy, Postgrex.Query, Decimal, Atom, BitString, Date, DateTime, Float, Integer, List, NaiveDateTime, Time, URI, Version, Version.Requirement
(elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) lib/string/chars.ex:22: String.Chars.to_string/1
(elixir) lib/io.ex:654: IO.puts/2
def inspect_query(query, label \\ nil)
def inspect_query(query, nil) do
IO.puts(Inspect.Ecto.Query.to_string(query))
query
end
def inspect_query(query, label: label) do
IO.puts("#{label}:\n#{Inspect.Ecto.Query.to_string(query)}")
query
end
For debugging purposes inspect should be sufficient.
Though you shouldn’t rely on the exact output format of either, as it’s meant for debugging only, or not documented at all, formats can change without a warning as they might be considered necessary.
@kip@NobbZ guys, thank you for the help, IO.inspect query is actually enough, you were right. It even does print return lines, just a little less pretty than Inspect.Ecto.Query.to_string; I was being too difficult
Thank you for your help!