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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









