Packages
enux
1.0.4
1.6.0
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.3
1.3.2
1.3.1
1.3.0
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.9.19
0.9.18
0.9.17
0.9.16
0.9.15
0.9.14
0.9.13
0.9.12
0.9.11
0.9.10
0.9.9
0.9.8
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
utility package for loading, validating and documenting your app's configuration variables from env, json, jsonc and toml files at runtime and injecting them into your environment
Current section
Files
Jump to
Current section
Files
lib/utils.ex
defmodule Enux.Utils do
@moduledoc false
@doc """
transforms a map into a keyword list
"""
def map_to_keyword_list(map, opts) when is_map(map) do
map
|> Enum.map(fn {k, v} ->
k = k |> handle_number() |> handle_whitespace() |> String.to_atom()
case {k, v} do
{k, v} when is_map(v) ->
{k, v |> map_to_keyword_list(opts)}
{k, v} ->
{k, v |> url_encode_conditional(opts)}
end
end)
|> Keyword.new()
end
@doc """
raises an error if a key contains whitespace
"""
def handle_whitespace(key) when is_binary(key) do
case ["\t", "\s"] |> Enum.any?(fn s -> String.contains?(key, s) end) do
true -> raise "#{key} contains whitespace"
false -> key
end
end
@doc """
raises an error if a key starts with a digit
"""
def handle_number(key) when is_binary(key) do
case key |> String.first() |> Integer.parse() do
{_, ""} -> raise "#{key} starts with a digit"
:error -> key
end
end
@doc """
if url_encoded: true is passed as an option to `Enux.load`, this function will url encode the binary values
"""
def url_encode_conditional(value, opts)
when is_binary(value) and is_list(opts) do
case opts |> Keyword.get(:url_encoded, false) do
true -> value |> URI.encode()
false -> value
end
end
def url_encode_conditional(value, _opts) do
value
end
end