I'm getting this string as query result from my database:
"%Sample.Struct{list: [], total: \"0.00\", day: 6, id: \"8vfts6\"}"
Is there any way to convert this one back to map? I'm getting this error decoding it with poison
** (Poison.SyntaxError) Unexpected token: %
(poison) lib/poison/parser.ex:56: Poison.Parser.parse!/2
(poison) lib/poison.ex:83: Poison.decode!/2
I can't fix the way data is being added to database, i must find a proper way for a key/value route to easily retrive data from that. (this is just a sample for a more complex result)
As it was mentioned in comments, you should not use
Code.eval_string
. But, there is a way to safely convert your code to Elixir struct, usingCode
module:First, get the AST from the string, but use the pattern matching to ensure it is a struct you are looking for (
{:__aliases__, _, [:Sample, :Struct]}
). All other (potentially malicious) code will fail this match:Here you have the full
ast
for you struct, and thekeymap
. You may now be tempted to useeval_quoted
with the AST, to get the struct you needed:But it is still not safe! Someone may put a function causing side effect into the string, like
"%Sample.Struct{list: IO.puts \"Something\"}"
, which will be executed during the evaluation. So you will need to check thekeymap
firsts, if it contain safe data.Or you may just use
keymap
directly, without evaluating anyting: