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