1
0
Fork 1
mirror of https://akkoma.dev/AkkomaGang/akkoma.git synced 2024-11-18 10:59:17 +00:00

Unify Config.get behaviour for atom/list key param

This commit is contained in:
Roman Chvanikov 2020-08-04 14:35:47 +03:00
parent e3953923ac
commit 56e9bf3393
3 changed files with 67 additions and 9 deletions

View file

@ -11,13 +11,33 @@ def get(key), do: get(key, nil)
def get([key], default), do: get(key, default)
def get([parent_key | keys], default) do
case :pleroma
|> Application.get_env(parent_key)
|> get_in(keys) do
nil -> default
any -> any
end
def get([root_key | keys], default) do
# This is to mimic Application.get_env/3 behaviour that returns `nil` if the
# actual value is `nil`.
Enum.reduce_while(keys, Application.get_env(:pleroma, root_key), fn key, config ->
case key do
[last_key] when is_map(config) ->
{:halt, Map.get(config, last_key, default)}
[last_key] when is_list(config) ->
{:halt, Keyword.get(config, last_key, default)}
_ ->
case config do
%{^key => value} ->
{:cont, value}
[_ | _] ->
case :lists.keyfind(key, 1, config) do
{_, value} -> {:cont, value}
_ -> {:halt, default}
end
_ ->
{:halt, default}
end
end
end)
end
def get(key, default) do

View file

@ -28,6 +28,34 @@ test "get/1 with a list of keys" do
assert Pleroma.Config.get([:azerty, :uiop], true) == true
end
describe "nil values" do
setup do
Pleroma.Config.put(:lorem, nil)
Pleroma.Config.put(:ipsum, %{dolor: [sit: nil]})
Pleroma.Config.put(:dolor, sit: %{amet: nil})
on_exit(fn -> Enum.each(~w(lorem ipsum dolor)a, &Pleroma.Config.delete/1) end)
end
test "get/1 with an atom for nil value" do
assert Pleroma.Config.get(:lorem) == nil
end
test "get/2 with an atom for nil value" do
assert Pleroma.Config.get(:lorem, true) == nil
end
test "get/1 with a list of keys for nil value" do
assert Pleroma.Config.get([:ipsum, :dolor, :sit]) == nil
assert Pleroma.Config.get([:dolor, :sit, :amet]) == nil
end
test "get/2 with a list of keys for nil value" do
assert Pleroma.Config.get([:ipsum, :dolor, :sit], true) == nil
assert Pleroma.Config.get([:dolor, :sit, :amet], true) == nil
end
end
test "get/1 when value is false" do
Pleroma.Config.put([:instance, :false_test], false)
Pleroma.Config.put([:instance, :nested], [])

View file

@ -17,9 +17,19 @@ defmacro clear_config(config_path) do
defmacro clear_config(config_path, do: yield) do
quote do
initial_setting = Config.get(unquote(config_path))
initial_setting = Config.get(unquote(config_path), :__clear_config_absent__)
unquote(yield)
on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
on_exit(fn ->
case initial_setting do
:__clear_config_absent__ ->
Config.delete(unquote(config_path))
_ ->
Config.put(unquote(config_path), initial_setting)
end
end)
:ok
end
end