Skip to content

Commit

Permalink
don't use the new config API for persist
Browse files Browse the repository at this point in the history
  • Loading branch information
idyll committed May 23, 2020
1 parent ae3abab commit 2ce0bd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
68 changes: 30 additions & 38 deletions lib/provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,54 +101,46 @@ defmodule Toml.Provider do
end
end

if has_config_api? do
defp persist(config, keyword) when is_list(keyword) do
config = Config.Reader.merge(config, keyword)
Application.put_all_env(config, persistent: true)
config
end
else
defp persist(config, keyword) when is_list(keyword) do
# For each app
for {app, app_config} <- keyword do
# Get base config
base = Application.get_all_env(app)
base = deep_merge(base, Keyword.get(config, app, []))
# Merge this app's TOML config over the base config
merged = deep_merge(base, app_config)
# Persist key/value pairs for this app
for {k, v} <- merged do
Application.put_env(app, k, v, persistent: true)
end

# Return merged config
{app, merged}
defp persist(config, keyword) when is_list(keyword) do
# For each app
for {app, app_config} <- keyword do
# Get base config
base = Application.get_all_env(app)
base = deep_merge(base, Keyword.get(config, app, []))
# Merge this app's TOML config over the base config
merged = deep_merge(base, app_config)
# Persist key/value pairs for this app
for {k, v} <- merged do
Application.put_env(app, k, v, persistent: true)
end
end

defp deep_merge(a, b) when is_list(a) and is_list(b) do
if Keyword.keyword?(a) and Keyword.keyword?(b) do
Keyword.merge(a, b, &deep_merge/3)
else
b
end
# Return merged config
{app, merged}
end
end

defp deep_merge(_k, a, b) when is_list(a) and is_list(b) do
if Keyword.keyword?(a) and Keyword.keyword?(b) do
Keyword.merge(a, b, &deep_merge/3)
else
b
end
defp deep_merge(a, b) when is_list(a) and is_list(b) do
if Keyword.keyword?(a) and Keyword.keyword?(b) do
Keyword.merge(a, b, &deep_merge/3)
else
b
end
end

defp deep_merge(_k, a, b) when is_map(a) and is_map(b) do
Map.merge(a, b, &deep_merge/3)
defp deep_merge(_k, a, b) when is_list(a) and is_list(b) do
if Keyword.keyword?(a) and Keyword.keyword?(b) do
Keyword.merge(a, b, &deep_merge/3)
else
b
end
end

defp deep_merge(_k, _a, b), do: b
defp deep_merge(_k, a, b) when is_map(a) and is_map(b) do
Map.merge(a, b, &deep_merge/3)
end

defp deep_merge(_k, _a, b), do: b

# At the top level, convert the map to a keyword list of keyword lists
# Keys with no children (i.e. keys which are not tables) are dropped
defp to_keyword(map) when is_map(map) do
Expand Down
3 changes: 1 addition & 2 deletions test/provider_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ defmodule Toml.Test.ProviderTest do
file = Path.join([__DIR__, "fixtures", "provider.toml"])
Application.put_all_env(toml: [nested: [deep: "success!"]])

opts = Toml.Provider.init(path: file, keys: :atoms!)
Toml.Provider.init(path: file, keys: :atoms!)

assert Toml.Provider.is_distillery_env?()
assert [deep: "success!", foo: "bar"] = Application.get_env(:toml, :nested)
end

Expand Down

0 comments on commit 2ce0bd1

Please sign in to comment.