smolcatgirl

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 :slight_smile: Thank you!

Showing Posts 1 to 7

hauleth

hauleth

Just in case, there is Kernel.struct/2 for that.

ityonemo

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

smolcatgirl OP

What if I change it to use String.to_existing_atom?

smolcatgirl

smolcatgirl OP

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

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

smolcatgirl OP

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

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).

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
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
handnot2
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
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
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
marciok
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
fuelen
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
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
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
sorenone
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
akoutmos
@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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews