silviurosu
Convert multiple map values conditionally
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
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 4 of 4 Posts
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).