Dynamic Structs by json

Hi at all,

I am quit new to elixir coming from ruby, python, C or node.
What I try to do is to define a generic struct which has one static field ‘json’ with a json String as a value.
I want to define each key of that json object map to a function in the struct.

defmodule Models.Generic do
  alias __MODULE__

  defstruct json: {}

  Map.keys(
    Poison.Parser.parse!(json, %{keys: :atoms!})
    |> Enum.each(fn name ->
      def unquote(:"#{name}")() do
        json[name]
      end
    end)
  )
end

But I get

** (CompileError) lib/models/generic.ex:7: undefined function json/0

Can anyone explain what is happen here and how I can fulfill my use case, please?

Best regards

You probably also need to unquote the json inside the function.

Poison.Parser.parse!(json, %{keys: :atoms!})

You’re asking Poison to parse the contents of variable “json”, but it doesn’t look defined anywhere. What do you intend to parse?

1 Like

I try to do the following:

iex#> model = %Models.Generic{json: “{“test”: “hello world”}”}
iex#> model.test
“hello world”

You can’t.

Keys of a struct need to be known at compiletime, you can not add or remove them at runtime.

What you can do though, is to use dotted notation with the return value of Poison.Parse.parse!/2 if you enforce keys to be atoms.

1 Like

Why?
And how I can implement my Usecase?

Can you show me an example what you mean please. I am not sure.

I do not have a project at hands that uses :poison (well, my current machine doesn’t even have elixir installed), but the following should “just” work:

data = Poison.Parser.parse!(~s[{"foo": "bar"}], %{keys: :atoms!})
data.foo == "bar"

Of course it will work. But its not what I want to do.
I dont know the json string at compile time.

You do not need to. You can load it from anywhere if you want. But you need to know some keys at least, or you can’t use dotted notation anyway…

Can you talk more about your overall use case? What you’ve described is an implementation approach: building dynamic structs. This approach is not really feasible given how structs work. If you describe the overall problem you’re trying to solve though perhaps we can suggest alternative approaches.

2 Likes