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).
Popular in Announcing
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








