dokicro

dokicro

Map key is a atom or string

Hi,

I am new to elixir and here is part of the code I dont like

slider =  case Map.has_key?(attrs, :slider_id) do
  true -> get_slider!(attrs.slider_id)
  false -> get_slider!(attrs["slider_id"])
end

Reason for this is because when I do attrs = Map.put(attrs, :slider_id, slider.id) in sliders_test map key is atom

But when I do the same thing in slider_controller_test map key is string.

Most Liked

LostKobrakai

LostKobrakai

Params in contollers have string keys because anybody can send you any param and creating an atom for those would be a attack vector to crash the vm. It’s still generally adviced to work with atom keys in your business part of the app and to sanitize and atomize params before passing things forward into it.

kokolegorille

kokolegorille

You could…

slider = Map.get(attrs, :slider_id) || Map.get(attrs, "slider_id")

As a rule of thumb, expect data coming from outside to be “key”, and after cast to be :key.

Nicd

Nicd

To elaborate a bit, the reason why map keys from external sources (like user input) are strings, is that atoms are not garbage collected and there is a set maximum amount of atoms available in the system. If you exceed that amount, the VM will crash. So that’s why you shouldn’t create atoms from user input for example.

Last Post!

LostKobrakai

LostKobrakai

Which one are you talking about? There are a few things mentioned in this topic.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement