Refactor config
This commit is contained in:
parent
56e9bf3393
commit
6f60ac9f41
|
@ -11,33 +11,11 @@ def get(key), do: get(key, nil)
|
|||
|
||||
def get([key], default), do: get(key, default)
|
||||
|
||||
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)
|
||||
def get([_ | _] = path, default) do
|
||||
case fetch(path) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
def get(key, default) do
|
||||
|
@ -54,6 +32,22 @@ def get!(key) do
|
|||
end
|
||||
end
|
||||
|
||||
def fetch([root_key | keys]) do
|
||||
Enum.reduce_while(keys, Application.fetch_env(:pleroma, root_key), fn
|
||||
key, {:ok, config} when is_map(config) or is_list(config) ->
|
||||
case Access.fetch(config, key) do
|
||||
:error ->
|
||||
{:halt, :error}
|
||||
|
||||
value ->
|
||||
{:cont, value}
|
||||
end
|
||||
|
||||
_key, _config ->
|
||||
{:halt, :error}
|
||||
end)
|
||||
end
|
||||
|
||||
def put([key], value), do: put(key, value)
|
||||
|
||||
def put([parent_key | keys], value) do
|
||||
|
@ -70,12 +64,15 @@ def put(key, value) do
|
|||
|
||||
def delete([key]), do: delete(key)
|
||||
|
||||
def delete([parent_key | keys]) do
|
||||
{_, parent} =
|
||||
Application.get_env(:pleroma, parent_key)
|
||||
|> get_and_update_in(keys, fn _ -> :pop end)
|
||||
def delete([parent_key | keys] = path) do
|
||||
with {:ok, _} <- fetch(path) do
|
||||
{_, parent} =
|
||||
parent_key
|
||||
|> get()
|
||||
|> get_and_update_in(keys, fn _ -> :pop end)
|
||||
|
||||
Application.put_env(:pleroma, parent_key, parent)
|
||||
Application.put_env(:pleroma, parent_key, parent)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(key) do
|
||||
|
|
|
@ -127,7 +127,10 @@ test "doesn't do anything if rum disabled" do
|
|||
:ok
|
||||
end
|
||||
|
||||
setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
|
||||
setup do
|
||||
Pleroma.Config.get(:i_am_aware_this_may_cause_data_loss, 42) |> IO.inspect()
|
||||
clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
|
||||
end
|
||||
|
||||
test "raises if it detects unapplied migrations" do
|
||||
assert_raise Pleroma.ApplicationRequirements.VerifyError,
|
||||
|
|
|
@ -117,5 +117,21 @@ test "delete/2 with a list of keys" do
|
|||
Pleroma.Config.put([:delete_me, :delete_me], hello: "world", world: "Hello")
|
||||
Pleroma.Config.delete([:delete_me, :delete_me, :world])
|
||||
assert Pleroma.Config.get([:delete_me, :delete_me]) == [hello: "world"]
|
||||
|
||||
assert Pleroma.Config.delete([:this_key_does_not_exist])
|
||||
assert Pleroma.Config.delete([:non, :existing, :key])
|
||||
end
|
||||
|
||||
test "fetch/1" do
|
||||
Pleroma.Config.put([:lorem], :ipsum)
|
||||
Pleroma.Config.put([:ipsum], dolor: :sit)
|
||||
|
||||
assert Pleroma.Config.fetch([:lorem]) == {:ok, :ipsum}
|
||||
assert Pleroma.Config.fetch([:ipsum, :dolor]) == {:ok, :sit}
|
||||
assert Pleroma.Config.fetch([:lorem, :ipsum]) == :error
|
||||
assert Pleroma.Config.fetch([:loremipsum]) == :error
|
||||
|
||||
Pleroma.Config.delete([:lorem])
|
||||
Pleroma.Config.delete([:ipsum])
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue