Our last blog post talked about ExConstructor, a tool we wrote at Appcues to make it incredibly easy to turn data from the outside world into structs. We’re pleased to announce ExConstructor version 1.0 to the Elixir community!
Elixir structs are a great way to represent data in your apps, but there is often an impedance mismatch between structs and the data used to initialize them. Often, a programmer must compensate for key data type (string or atom), key formatting (under_score or camelCase), and container data structure (map, keyword list, list of tuples).
gets all this
out of your way. Add
use ExConstructor after a
to generate a constructor function that handles atoms vs strings,
under_score vs camelCase, and map vs list issues automatically.
As discussed in our last post, it’s good practice to write functions that use structs instead of generic maps or keyword lists, because your functions become clearer in their intent (what kind of data do they deal with? what’s it look like?) and usage (precisely what arguments am I allowed to give?).
Elixir doesn’t make it easy to create structs from data which doesn’t
already perfectly match the struct, but
ExConstructor does. Here is
a small example:
Why Not Just Use Native Elixir?
It sounds almost too simple. The task of creating structs may seem to be so trivial that the gains from doing so are barely worth adding a dependency to your app.
This is almost true. There’s only about 100 lines of code in ExConstructor.
So, really, what can ExConstructor do that e.g.,
struct(TestStruct, your_input_here) can’t? A table will hammer it home.
|Data Structure||Key Type||Key Format||Native||ExConstructor|
If you handle any of the data formats without a checkbox in the Native column, you should look at ExConstructor. It’s not that you couldn’t do it yourself – it’s that you are going to do it yourself hundreds of times.