Is it possible to get a sigil from a string?

For example, if I have a json payload:

{
  "inputs":{
    "key": "~jp(event/key1/1/key2)",
    "key2": "str"
  }
}

The goal is to transform the the string to a custom json-pointer sigil ~jp(json-pointer-string)

My naive approach would be a regex match but wondering if there is an Elixir magic way or if I’m missing something obvious :stuck_out_tongue:

Does Code.eval_string/3 not do the job?

No, sigils are compile time structures. If you have runtime data you can’t treat that data as code without evaling it. Evaling is generally not advised for security reasons.

3 Likes

For my purposes the input is trusted but I agree its better to be cautious. Ok leaning towards regex matching then.

Would eval_string/3 be faster than a regex though? My guess is no