NewBee

NewBee

Pattern Matching Map

hey,
So I have this function
def update(
conn,
%{
“expense_id” => id,
“name” => _name,
ocr => _ocr

    } = payload
  ) do ...

my ocr field is a map that I’m going to update :
when I compiled my code I had : cannot use variable ocr as map key inside a pattern. Map keys in patterns can only be literals (such as atoms, strings, tuples, etc.) or an existing variable matched with the pin operator (such as ^some_var) as error
Do you have any suggestions? did any one get by this problem?

Most Liked Responses

amnu3387

amnu3387

There’s really no way of knowing what that pattern match means at compile time, the options I see is to either use is_map_key as @qhwa mentions, like:

def update(conn, %{"expense_id" => id, "name" => _name} = params
) when is_map_key(params, "language") or is_map_key(params, "another_key") do ....

But you’ll probably have to handle then, down the line, which param was actually passed, so you don’t gain that much other than validating that at least one of those keys is present.

Or having multiple update function definitions, one for each expected key, pattern matching the exact key on each one (then in the body of update you’ll know exactly which one you’re dealing with), or to have a function that validates the map, called inside the update controller action and take it from there.

Without knowing what is the whole intended flow not sure what would be the best option.

qhwa

qhwa

Is the key for ocr content dynamic or static, e.g. "ocr" or :ocr ?

If it is dynamic, you can use is_map_key/2 guard, but I really doubt it because it seems you’re using Phoenix and it’s unlikely to have dynamic field names in general.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement