Merge branch 'apps-api-endpoint' into 'develop'
[akkoma] / lib / pleroma / maps.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Maps do
6 def put_if_present(map, key, value, value_function \\ &{:ok, &1}) when is_map(map) do
7 with false <- is_nil(key),
8 false <- is_nil(value),
9 {:ok, new_value} <- value_function.(value) do
10 Map.put(map, key, new_value)
11 else
12 _ -> map
13 end
14 end
15
16 def safe_put_in(data, keys, value) when is_map(data) and is_list(keys) do
17 Kernel.put_in(data, keys, value)
18 rescue
19 _ -> data
20 end
21 end