davids

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

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.

Where Next?

Popular in Questions Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
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

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement