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

New
fuelen
I finally found time to write down my journey away from ExMachina, including the workaround I tried first and the API experiments that fo...
New
lawik
The words are insufficient to describe what transpired. What we tried to do met what the people wanted it to be. It became beautiful. Th...
New
davejs
Hi everyone, I’ve been building BetterTyping (https://bettertyping.org) on and off since 2021. It’s a free typing game: practice alone, ...
New
funboy
Turns out a 1ms network hiccup is enough to silently lose broadcasts for a node and that node is just quietly wrong forever Wrote it up ...
New
AstonJ
If you’ve found an interesting or cool Elixir related blog post and think it’s worth sharing please add it to this thread :icon_biggrin: ...
New
videsnelson
The code in this post is Erlang, but it all applies to Elixir binaries just the same: how appends, match contexts and sub-binaries behave...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews