fireproofsocks

fireproofsocks

Is it possible to use a dependency's structs inside of config?

I am bumping into some limits of my own understanding when it comes to application configuration. Specifically, I was wanting to reference a dependency’s structs inside my app’s configuration. I’m getting errors like this:

** (CompileError) config/components.exs:27: SomeDep.__struct__/1 is undefined, cannot expand struct

Is there a way to ensure that the dependency is compiled and that its structs are available when my app is being compiled? Or am I putting the carriage before the horse?

Thanks for any clarifications!

Most Liked

fireproofsocks

fireproofsocks

Ah… from the docs:

  • config/config.exs - this file is read at build time, before we compile our application and before we even load our dependencies. This means we can’t access the code in our application nor in our dependencies. However, it means we can control how they are compiled

I guess I either move the structs over to the runtime.exs or I make the config work without structs.

fireproofsocks

fireproofsocks

The only reason that the compile-time config would be nice in this case is for documentation purposes: I can reference the config from inside a @moduledoc and generate accurate information about how the app is set up (e.g. via a Mermaid chart). For a complex app, that starts to feel like more than a “nice-to-have”, but technically, the app is totally functional if that info is supplied at run-time.

For a quick fix (albeit ugly), I can reference the structs via apply/3 so they don’t cause problems during build-time.

A better option might be to figure out a mix task/alias that would generate the necessary charts at runtime and include these pages linked up to the other ExDoc outputs.

Last Post!

fireproofsocks

fireproofsocks

I’d really be curious why you need the data to be structs in config.exs .

The simple answer is, of course, that I don’t need to do it this way. I’m just trying to figure out a “low effort” solution that works with our current code while we further weigh the pros and cons. The structs used in the configuration are used in conjunction with a custom protocol – each implementation of the protocol defines supervisor children for an app that acts something like a job queue with lots of interconnected processes.

Previously, we had configured this with a list of simple maps; the functionality pivoted based on a :type key in the maps. However, this got a bit messy – partly because it was easy to fat-finger keys and partly because the list of all these maps became quite long because the maps had to be verbose (there aren’t default values in regular maps, so each key had to be included). The configs were sometimes thousands of lines.

After several trip-ups from the use of simple maps, we thought we’d try using structs and a custom protocol. Perhaps the most important factor was that the configuration became easier to read and document when defined with structs. Readability was helped because structs can require certain keys and provide default values, and by using a protocol, we could open the door for adding customizations on top of this dependency: we have multiple apps that use this dependency in different ways – otherwise we could feed the configs into calls to struct/2 as you demonstrated.

So this latest iteration has some pros and cons that I need to sit with before taking another stab at it. I’m always wary when a solution feels too much like “pushing the river”, so I don’t think this is where things will land permanently, but I’m not yet ready to retreat back to how we had this before.

Where Next?

Popular in Questions Top

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
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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement