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

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
Hey everyone! :waving_hand: I’ve published Part 7 of the Building Distributed Systems in Elixir series, where we build core distributed ...
New
zorn
An educational side project in Elixir, Phoenix, and Tauri. I share what I learned while wiring Automerge into the BEAM, including how I s...
New
nathanl
Process labels are useful for visualization and debugging. Here’s why you should use them.
New
andreasronge
What happens if you design tools for LLMs instead of letting LLM use human tools ? Wrote a blog on why and what that enables. As I see ...
New
mudasobwa
Somewhere, right now, a senior engineer is on the verge of a nervous breakdown because his company will not let him switch from Claude to...
New
zorn
:pencil: Phoenix’s built-in LiveView test helpers require you to hand-build the form payload and start a fresh pipe after every click. Wo...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews