silviurosu
I need to convert in place in a map some values only of they exist. The key can be either binary or atom.
Is there a more Elixir way to do it than my solution below?
defp convert_time_params(params) do
params
|> (fn x ->
if Map.has_key?(x, "start_time"), do: update_in(x, ["start_time"], &CalendarUtils.from_iso8601/1), else: x
end).()
|> (fn x ->
if Map.has_key?(x, :start_time),
do: update_in(x, [Access.key!(:start_time)], &CalendarUtils.from_iso8601/1),
else: x
end).()
|> (fn x ->
if Map.has_key?(x, "end_time"), do: update_in(x, ["end_time"], &CalendarUtils.from_iso8601/1), else: x
end).()
|> (fn x ->
if Map.has_key?(x, :end_time),
do: update_in(x, [Access.key!(:end_time)], &CalendarUtils.from_iso8601/1),
else: x
end).()
end
The map is used in Ecto so I can not change the key type from atom to binary. I need to keep the input the same
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
And then
kip
I have a function called deep_map that I use in several different projects to simplify certain classes of map operations. On top of that I have other functions like atomize_keys/3 and so on. It might be useful (or it might be not
)
Summary
Recursively traverse a map and invoke a function
that transforms the map for each key/value pair.
Arguments
mapis anyt:map/0functionis a1-arityfunction or function reference thatis called for each key/value pair of the provided map. It can
also be a 2-tuple of the form
{key_function, value_function}functionis a single function it will becalled with the 2-tuple argument
{key, value}{key_function, value_function}the
key_functionwill be called with the argumentkeyand the valuefunction will be called with the argument
valueoptionsis a keyword list of options. The default is[]Options
:levelindicates the starting (and optionally ending) levels ofthe map at which the
functionis executed. This canbe an integer representing one
levelor a rangeindicating a range of levels. The default is
1..#{@max_level}:onlyis a term or list of terms or acheck function. If it is a termor list of terms, the
functionis only called if thekeyof themap is equal to the term or in the list of terms. If
:onlyis acheck functionthen thecheck functionis passed the{k, v}ofthe current branch in the
map. It is expected to return atruthyvalue that if
truesignals that the argumentfunctionwill be executed.:exceptis a term or list of terms or acheck function. If it is a termor list of terms, the
functionis only called if thekeyof themap is not equal to the term or not in the list of terms. If
:exceptis acheck functionthen thecheck functionis passed the{k, v}ofthe current branch in the
map. It is expected to return atruthyvalue that if
truesignals that the argumentfunctionwill not be executed.Notes
If both the options
:onlyand:exceptare provided then thefunctionis called only when a
termmeets both criteria.Returns
maptransformed by the recursive application offunctionExamples
hauleth
It is very important to point out that this should be used ONLY if you trust source of the binaries. If used on untrusted source then you can experience DoS when malicious party will send data with a lot different strings.
kip
Yes, good to point it out. The function actually has both safe and unsafe modes for exactly that reason. And all my use cases are on CLDR data (and never on user input).