NewBee

NewBee

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?

Showing Posts 1 to 5

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.

NewBee

NewBee OP

ocr has dynamics keys for the moment (I’ll define a struct later for it so it will be static ) And yes I’m using Phoenix.
Thank you for your answer

amnu3387

amnu3387

What would be the key that ocr represents?

NewBee

NewBee OP

name , language

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews