Current section
Files
Jump to
Current section
Files
lib/exqute/support/config.ex
defmodule Exqute.Support.Config do
@moduledoc """
A support library for Configuration of the Exqute library
"""
@default_config %{
json_library: Jason,
log_job_enqueued: false,
serializer: Exqute.Serializers.JsonSerializer
}
@doc """
Get specified `key` from the application configuration
"""
def get(key) do
get(key, Map.get(@default_config, key))
end
def get(key, fallback) do
case Application.get_env(:exqute, key, fallback) do
{:system, varname} -> System.get_env(varname)
{:system, varname, default} -> System.get_env(varname) || default
value -> value
end
end
@doc """
Fetch the serializer from `Exqute.Support.Config` please.
"""
def serializer do
get(:serializer)
end
# https://medium.com/@kay.sackey/create-a-struct-from-a-map-within-elixir-78bf592b5d3b
def struct_from_map(a_map, as: a_struct) do
# Find the keys within the map
keys =
Map.keys(a_struct)
|> Enum.filter(fn x -> x != :__struct__ end)
# Process map, checking for both string / atom keys
processed_map =
for key <- keys, into: %{} do
value = Map.get(a_map, key) || Map.get(a_map, to_string(key))
{key, value}
end
a_struct = Map.merge(a_struct, processed_map)
a_struct
end
end