sribe

sribe

Given the output of IO.inspect on a map, is there any way to parse that string back into a map?

Given the output of IO.inspect on a map, is there any way to parse that string back into a map?

For a one-shot hack, one could put them into source files and compile. For ongoing parsing out of a log, one could JSON encode them instead. But assume there’s a pile of maps written out by IO.inspect: is there a way to rehydrate them directly?

A tiny bit of context: I actually thought I had JSON in the files, but lolz, I do not.

Most Liked

al2o3cr

al2o3cr

One gotcha: inspect will truncate output by default if the maps are large. For instance:

0..100 |> Enum.with_index() |> Map.new() |> inspect() |> Code.eval_string()

fails (on my machine anyways) with:

warning: variable "..." does not exist and is being expanded to "...()", please use parentheses to remove the ambiguity or change the variable name
  nofile:1

** (CompileError) nofile:1: undefined function .../0 (there is no such import)
    (elixir 1.13.4) lib/code.ex:404: Code.validated_eval_string/3
lud

lud

For a single map you can do this:

 %{a: 1} |> inspect |> Code.eval_string() |> elem(0)

Now if you have more, you will have to find a way to split the string into valid chunks of code.

Now if this for a test or a quick made tool it’s fine but otherwise consider that eval is evil and be sure that there is no proper way of doing what you want before eval’ing code.

sribe

sribe

Thank you, eval_string is what I was missing.

I do have more, and I am already splitting them–I got to the part where I tried to parse them after splitting, Jason.decode kept failing, I looked closer and realized D’OH! NOT JSON!! :wink:

Nicd

Nicd

Another gotcha is that there’s no requirement for the inspect output to be valid Elixir. Many structs are inspected as something like #Foo<something> instead of %Foo{something}. It may not be feasible to parse your inspect output at all.

Obligatory note, evaling strings is very dangerous and a wide open invitation for remote code execution, if an attacker can control the inputs.

msimonborg

msimonborg

You can also Code.eval_file if it was written to a file.

File.write!("maps.txt", inspect([%{foo: :bar}, %{biz: :baz}]))
# => :ok
"maps.txt"
|> Code.eval_file()
|> elem(0)
# => [%{foo: :bar}, %{biz: :baz}]

Again, eval is very dangerous! Only use it if you trust the content 100%. Sounds like maybe you wrote the files though but just be careful.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement