silviurosu

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

First 4 of 4 Posts Switch mode

hauleth

hauleth

def update_existing(map, key, fun) do
  case map do
    %{^key => old} -> %{map | key => fun.(old)}
    _ -> map
  end
end

def update_indifferent(map, key, fun) when is_atom(key) do
  map
  |> update_existing(key, fun)
  |> update_existing(Atom.to_string(key), fun)
end

And then

params
|> update_indifferent(:start_time, &CalendarUtils.from_iso8601/1)
|> update_indifferent(:end_time, &CalendarUtils.from_iso8601/1)
kip

kip

ex_cldr Core Team

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 :slight_smile: )

Summary

Recursively traverse a map and invoke a function
that transforms the map for each key/value pair.

Arguments

  • map is any t:map/0
  • function is a 1-arity function or function reference that
    is called for each key/value pair of the provided map. It can
    also be a 2-tuple of the form {key_function, value_function}
    • In the case where function is a single function it will be
      called with the 2-tuple argument {key, value}
    • In the case where function is of the form {key_function, value_function}
      the key_function will be called with the argument key and the value
      function will be called with the argument value
  • options is a keyword list of options. The default is []

Options

  • :level indicates the starting (and optionally ending) levels of
    the map at which the function is executed. This can
    be an integer representing one level or a range
    indicating a range of levels. The default is 1..#{@max_level}
  • :only is a term or list of terms or a check function. If it is a term
    or list of terms, the function is only called if the key of the
    map is equal to the term or in the list of terms. If :only is a
    check function then the check function is passed the {k, v} of
    the current branch in the map. It is expected to return a truthy
    value that if true signals that the argument function will be executed.
  • :except is a term or list of terms or a check function. If it is a term
    or list of terms, the function is only called if the key of the
    map is not equal to the term or not in the list of terms. If :except is a
    check function then the check function is passed the {k, v} of
    the current branch in the map. It is expected to return a truthy
    value that if true signals that the argument function will not be executed.

Notes

If both the options :only and :except are provided then the function
is called only when a term meets both criteria.

Returns

  • The map transformed by the recursive application of
    function

Examples

  iex> map = %{a: :a, b: %{c: :c}}
  iex> fun = fn
  ...>   {k, v} when is_atom(k) -> {Atom.to_string(k), v}
  ...>   other -> other
  ...> end
  iex> Cldr.Map.deep_map map, fun
  %{"a" => :a, "b" => %{"c" => :c}}
  iex> map = %{a: :a, b: %{c: :c}}
  iex> Cldr.Map.deep_map map, fun, only: :c
  %{a: :a, b: %{"c" => :c}}
  iex> Cldr.Map.deep_map map, fun, except: [:a, :b]
  %{a: :a, b: %{"c" => :c}}
  iex> Cldr.Map.deep_map map, fun, level: 2
  %{a: :a, b: %{"c" => :c}}
hauleth

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

kip

ex_cldr Core Team

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).

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
michallepicki
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
rahultumpala
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 Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
type1fool
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
akoutmos
@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

We're in Beta

About us Mission Statement