davids
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
Most Liked
benwilson512
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.
dom
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?
NobbZ
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.








