smolcatgirl
StructCreate, a library to create a struct from a keyword list or a map
Hi,
I am trying to get better at Elixir and I was looking for a reason to make a library. Because writing libraries seem to be a good way to learn Elixir, not just doing code but actually implementing low level tech and seeing what they return. So I found myself wanting to take a map and create a struct from it. The function should also tolerate if any keys are missing or if there are more keys than whats specified. So i wrote this library.
It has a function that takes a keyword list or a map and converts into a struct given a module name.
Here is the link for anyone interested: https://github.com/smolcatgirl/struct_create
I would like any feedback
Thank you!
Most Liked
smolcatgirl
What if I change it to use String.to_existing_atom?
smolcatgirl
struct/2 fails if I include too many keys. I could filter the keys before but my library does it for me and it works with keyword lists too.
edit: wait it seems like i got it wrong, struct/2 works with this.
ityonemo
Yes. Also, modules with structs define a magic __struct__/0 function that you can use to get a hold of the default struct, bypassing @enforce_keys requirements (use with caution).







