superruzafa

superruzafa

Load struct given its name from string

Hi!

I have a couple of structs and I want to build one of them depending on some STRING as input:

defmodule Mystruct1 do ... end
defmodule Mystruct2 do ... end
...
defmodule Mystructn do ... end

def get_struct(name) do
    name
    |> <transformations_here>
    |> struct()
end

the_struct = get_struct("Mystruct5")

Naively I’m trying something like

name |> String.capitalize |> String.to_atom |> struct

but I’m getting this error

** (UndefinedFunctionError) function :Mystruct5.__struct__/0 is undefined (module :Mystruct5 is not available)
    :Mystruct5.__struct__()
    (elixir) lib/kernel.ex:2161: Kernel.struct/3

Besides whether this can be done or not, is this a good idea in order to allow the app to scale by merely adding new structs instead of use pattern matching for every possible input?

Thanks

Most Liked

Nicd

Nicd

Maybe you should use Module.safe_concat([your_string]) for this? It will fail with ArgumentError if the module doesn’t exist.

al2o3cr

al2o3cr

Consider writing this out explicitly, unless there are a LOT of Mystructs:

def name_to_module(name) do
  case name do
    "Mystruct1" -> Mystruct1
    "Mysturct3" -> Mystruct3 # NOTE: client sends this one spelled wrong and won't change it
  end
end

Advantages:

  • very clear exactly which modules this could return
  • easy to add aliases / typo-corrections

Downsides:

  • more typing when adding a new struct
LostKobrakai

LostKobrakai

Module name in elixir are atoms with the :"Elixir." prefix, so :"Elixir.Mystruct5" == Mystruct5.

Where Next?

Popular in Questions Top

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 th...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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 th...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement