silviurosu

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

Showing Posts 1 to 4

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? Top

Trending in Questions Top

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
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
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