smetana

smetana

Hello all,

I need to convert AST with possible long binary to string.
Macro.to_string() uses Inspect and Inspect has the printable_limit option set to 4096 for binaries by default.
I am looking for the way to increase it so binary will not be truncated on to_string()

Thanks,
Serge

First 10 of 10 Posts Switch mode

michalmuskala

michalmuskala

This seems like a bug in Macro.to_string to me - it should print everything out. Could you open a bug report?

net

net

I believe this is already fixed in master. Until that’s released you can do

Macro.to_string(v, fn
  node, _ when is_binary(node) -> inspect(node, printable_limit: :infinity)
  _, string -> string
end)
mruoss

mruoss

Now that Macro.to_string/2 is deprecated, how would one do this?

axelson

axelson

Scenic Core Team

The bug is fixed so now you can use Macro.to_string/1 and if you’re trying to do something else then that would be best to discuss in a new thread where you state what you’re trying to accomplish.

mruoss

mruoss

This is too related to open a new thread. But when posting before, I wasn’t aware that the problem exists only for maps now. It’s basically the same issue but for maps. Opened a GH issue here:

https://github.com/elixir-lang/elixir/issues/13875

axelson

axelson

Scenic Core Team

Ah, I see. Yeah that does look like a related bug to me :+1:

mruoss

mruoss

Copying over José’s answer for shortcuts:

The difference here is that maps are not valid AST. We fallback to inspect in Macro.to_string for convenience, because the goal of the macro is mostly debugging, but if you returned that from a macro, Elixir would raise accordingly. In any case, it won’t be formatted accordingly. The correct fix is to call Macro.escapeor build the correct Map ast, as shown in the result of quote do: %{1 => 2, foo: :bar}|

For me, Macro.escape did the trick:

my_map = Map.new(1..51, &{&1, "value"}) |> Macro.escape()

(quote do: unquote(my_map))
|> Macro.to_string()
|> IO.inspect(printable_limit: :infinity)
nhpip

nhpip

Ironically I had a similar issue the other day. Code.quoted_to_algebra/2 offers a lot more control:

macro_to_string = 
    fn m ->
       Code.quoted_to_algebra(m,
           pretty: true,
           locals_without_parens: [field: 4, field: 3, field: 2, value: 2, value: 3]
       )
       |> Inspect.Algebra.format(:infinity)
       |> IO.iodata_to_binary()
  end
mruoss

mruoss

This doesn’t truncate the map. However, it still does a better job formatting the code when escaping the map to a valid AST.

— All posts loaded —

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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement