-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
config.ex
57 lines (50 loc) · 1.3 KB
/
config.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
defmodule Pow.Config do
@moduledoc """
Methods to parse and modify configurations.
"""
@type t :: Keyword.t()
defmodule ConfigError do
defexception [:message]
end
@doc """
Gets the key value from the configuration.
If not found, it'll fall back to environment config, and lastly to the
default value which is `nil` if not specified.
"""
@spec get(t(), atom(), any()) :: any()
def get(config, key, default \\ nil) do
Keyword.get(config, key, get_env_config(config, key, default))
end
@doc """
Puts a new key value to the configuration.
"""
@spec put(t(), atom(), any()) :: t()
def put(config, key, value) do
Keyword.put(config, key, value)
end
@doc """
Merges two configurations.
"""
@spec merge(t(), t()) :: t()
def merge(l_config, r_config) do
Keyword.merge(l_config, r_config)
end
defp get_env_config(config, key, default) do
config
|> env_config()
|> Keyword.get(key, default)
end
defp env_config(config) do
case Keyword.get(config, :otp_app) do
nil -> Application.get_all_env(:pow)
otp_app -> Application.get_env(otp_app, :pow, [])
end
end
@doc """
Raise a ConfigError exception.
"""
@spec raise_error(binary()) :: no_return
def raise_error(message) do
raise ConfigError, message: message
end
end