I ended up going with the idea from @michalmuskala - it makes sense to use atoms everywhere I can, then just have integers in the DB and frontend client.
defmodule API.Const do
@root_dir File.cwd!
@consts_dir Path.join(~w(#{@root_dir} lib api common constants))
consts = Path.wildcard("#{@consts_dir}/**/*.json")
|> Enum.map(fn(filename) -> File.read!(filename) |> Poison.decode! end)
|> Enum.reduce(fn(c, map) -> Map.merge(map, c) end)
for {key, value} <- consts do
def encode(unquote(String.to_atom(key))), do: unquote(value)
def decode(unquote(value)), do: unquote(String.to_atom(key))
end
end
I decided to have a folder of JSON files, that way I can easily use it on the frontend as well (at the moment it runs some scripts to convert to JSON files from PHP)
Are there any issues doing it this way?
I was thinking of prepending the name of the file to each key in the JSON file to help namespace it a bit, so system.json would be
{
"SOURCE_LVL1_API": 301023,
"SOURCE_LVL1_HTTP": 301024,
"SOURCE_LVL1_EMAIL": 301025,
"SOURCE_LVL1_SMS": 301026,
"SOURCE_LVL1_UNKNOWN": 301027
}
and would end up being
%{
:SYSTEM_SOURCE_LVL1_API => 301023,
:SYSTEM_SOURCE_LVL1_HTTP => 301024,
:SYSTEM_SOURCE_LVL1_EMAIL => 301025,
:SYSTEM_SOURCE_LVL1_SMS => 301026,
:SYSTEM_SOURCE_LVL1_UNKNOWN => 301027
}
Not sure though!


















