Unify Config.get behaviour for atom/list key param
This commit is contained in:
parent
e3953923ac
commit
56e9bf3393
|
@ -11,13 +11,33 @@ def get(key), do: get(key, nil)
|
||||||
|
|
||||||
def get([key], default), do: get(key, default)
|
def get([key], default), do: get(key, default)
|
||||||
|
|
||||||
def get([parent_key | keys], default) do
|
def get([root_key | keys], default) do
|
||||||
case :pleroma
|
# This is to mimic Application.get_env/3 behaviour that returns `nil` if the
|
||||||
|> Application.get_env(parent_key)
|
# actual value is `nil`.
|
||||||
|> get_in(keys) do
|
Enum.reduce_while(keys, Application.get_env(:pleroma, root_key), fn key, config ->
|
||||||
nil -> default
|
case key do
|
||||||
any -> any
|
[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
|
end
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:halt, default}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(key, default) do
|
def get(key, default) do
|
||||||
|
|
|
@ -28,6 +28,34 @@ test "get/1 with a list of keys" do
|
||||||
assert Pleroma.Config.get([:azerty, :uiop], true) == true
|
assert Pleroma.Config.get([:azerty, :uiop], true) == true
|
||||||
end
|
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
|
test "get/1 when value is false" do
|
||||||
Pleroma.Config.put([:instance, :false_test], false)
|
Pleroma.Config.put([:instance, :false_test], false)
|
||||||
Pleroma.Config.put([:instance, :nested], [])
|
Pleroma.Config.put([:instance, :nested], [])
|
||||||
|
|
|
@ -17,9 +17,19 @@ defmacro clear_config(config_path) do
|
||||||
|
|
||||||
defmacro clear_config(config_path, do: yield) do
|
defmacro clear_config(config_path, do: yield) do
|
||||||
quote do
|
quote do
|
||||||
initial_setting = Config.get(unquote(config_path))
|
initial_setting = Config.get(unquote(config_path), :__clear_config_absent__)
|
||||||
unquote(yield)
|
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
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue