bencoppock

bencoppock

I write about the frustration that can occur when dealing with string and atom-based keys in maps and how to (safely) translate string-based map keys to atoms:

Showing Posts 1 to 5

D4no0

D4no0

Isn’t it easier to translate all keys to string, I don’t see any downsides for that as cast/4 casts required params.

derpycoder

derpycoder

Thanks for the writeup, learnt a lot. Ability to use when in anonymous function was enlightening.


I am learning Elixir, and thought I would give list comprehension a try:

params = %{:foo => "Foo", "bar" => "Bar"}

# Convert keys to String:
for {k, v} <- params, into: %{}, do: if is_atom(k), do: {Atom.to_string(k), v}, else: {k, v}
# %{"bar" => "Bar", "foo" => "Foo"}

# Convert keys to Atoms:
for {k, v} <- params, into: %{}, do: if is_binary(k), do: {String.to_existing_atom(k), v}, else: {k, v}
# %{bar: "Bar", foo: "Foo"}

I will never mixup the two, anything external will always be string, and hand written values will be atoms.

bencoppock

bencoppock OP

Makes sense to me!

Assuming you just want to translate the params to be atom-based (and you’re not directly creating a record with Ecto), are you thinking something like this…?

data = %{}
types = %{title: :string}
changeset = Ecto.Changeset.cast({data, types}, %{"title" => "foo"}, [:title])
changeset.changes

I imagine that working well if you have Ecto in the mix and don’t need to give feedback on invalid params.

gregvaughn

gregvaughn

I would be looking at Ecto.Changeset.apply_action/2 to return either {:ok, struct} or {:error, changeset} which contains errors in the 2nd case. But it certainly depends on ecto (though not ecto_sql).

bencoppock

bencoppock OP

I wasn’t writing this with Ecto in mind, but I usually do have Ecto in the mix, so I like that.

It also feels cleaner to use the return of the apply_action function (which can include validation errors) as opposed to directly relying on the changes field in the Changeset struct (even though I think the field is more or less considered “public”).

— All posts loaded —

Where Next? Top

Trending in Blog Posts Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
pckrishnadas88
A while back, I had to process millions of database updates in a legacy system that was already hitting its 64 GB RAM limit, so scaling t...
New
rhcarvalho
At the heart of every Phoenix application is the often “invisible” HTTP server layer. For over a decade Cowboy has served the community ...
New
smaller_infinity
I recently figured out how to the the Rust hotpath profiling crate running in an elixir benchmark script (for profiling NIFs). I had some...
New
rhcarvalho
The Phoenix framework is notorious for its long term stability and dependability. Unlike most comparable projects, the Phoenix team activ...
New
pckrishnadas88
I’ve published Part 3 of my Elixir distributed systems learning series. This part explores process monitoring using the low-level primit...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews