thojanssens1

thojanssens1

When executing the following:

IO.inspect Inspect.Ecto.Query.to_string(from u in User, where: u.id == ^1)

special characters such as \n are printed literally.

"from u0 in MyApp.User,\n  where: u0.id == ^1"

However, if I use IO.puts:

IO.puts Inspect.Ecto.Query.to_string(from u in User, where: u.id == ^1)

the output is as needed:

 from u0 in MyApp.User,
   where: u0.id == ^1

Anyone has any idea why?

Showing Posts 1 to 10

kip

kip

ex_cldr Core Team

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{}.

thojanssens1

thojanssens1 OP

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).

Let me know if you think it’s nonsense :flushed_face:

thojanssens1

thojanssens1 OP

Also I tried the :pretty option:

IO.inspect Inspect.Ecto.Query.to_string(from u in User, where: u.id == ^1), pretty: true

But it doesn’t print return lines:

“from u0 in MyApp.User,\n where: u0.id == ^1”

Isn’t :pretty supposed to render the return lines?

kip

kip

ex_cldr Core Team

In that case I think you would find IO.puts query would do what you want rather than calling the protocol implementation directly.

thojanssens1

thojanssens1 OP

IO.puts query gives the following error:

** (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

thojanssens1

thojanssens1 OP

We will stick with a util function for now.

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
query
|> by_author(author_id)
|> inspect_query(label: "does this query look good?")
|> Repo.one!()
NobbZ

NobbZ

That function is not part of the publicly documented API and therefore might be removed at any time.

thojanssens1

thojanssens1 OP

In that case, I have no idea how to pretty-print a query as Inspect.Ecto.Query.to_string does.

NobbZ

NobbZ

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.

Perhaps tell us why you need them pretty printed?

thojanssens1

thojanssens1 OP

Because it’s easier to read. When there is an error with your query, Ecto also pretty-prints them (that’s where I got that function from).

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews