smolcatgirl
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!
Trending in Announcing
Hey everyone!
Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application.
This library uses Erlang esaml to provide
plug enabl...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Just in case, there is
Kernel.struct/2for that.ityonemo
As an exercise it’s nice! But you very likely don’t want to use this in production since it will cause an atom table leak if it’s passed user maps. This is a security regression as a malicious user can start spamming your server with strings that aren’t atoms yet and eventually cause your server to crash.
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
That’s slightly better but keep in mind that crashes the calling process. You should probably do a reverse check, which is to say, filter props in your proplist which don’t match Atom.to_string for the struct properties. Do you know how to obtain the list of fields in a struct?
smolcatgirl
Map.from_struct, Map.keys and then map them and convert them to strings?
Should I return a errror value in case the user provide a bad module name or a non keyword list or non map?
ityonemo
Yes. Also, modules with structs define a magic
__struct__/0function that you can use to get a hold of the default struct, bypassing @enforce_keys requirements (use with caution).