How to read config files on elixir mix project

旧城冷巷雨未停 提交于 2019-12-05 09:00:31

You can read keyword lists stored in a *.exs file, using Mix.Config.read(path). For writing Elixir terms to a *.exs file, you can use Inspect.Algebra.to_doc(%Inspect.Opts{pretty: true}) and write the resulting string content to a file using File.write. It's not as well formatted as if you did it by hand, but it's definitely still readable.

If you don't mind using Erlang terms, you can read and write those easily using :file.consult(path) and :file.write_file(:io_lib.fwrite('~p.\n', [config]), path) respectively.

Using Code.eval_file

Adding another option, is to evaluate the file as a code file, using Code.eval_file and get in return the result as an elixir construct.

Config file config1.ex:

%{configKey1: "configValue1", configKey2: "configValue2"} 

Reading the file:

{content, _} = Code.eval_file("config1.ex")

*evaluating a code file has security consideration needs to take in mind.

Regarding using Mix.Config.read! in @bitwalker correct answer

the config file needs to be in a specific format of:

[
   appName: [key1: "val1", key2: "val2"]
]

In the Mix.Config.read code, it try to validate the contents and expect a main keyword list [ {}, {}.. ] which includes keys that has value of type keyword list also.

The code is not long:

 def validate!(config) do
    if is_list(config) do
      Enum.all?(config, fn
        {app, value} when is_atom(app) ->
          if Keyword.keyword?(value) do
            true
          else
            raise ArgumentError,
              "expected config for app #{inspect app} to return keyword list, got: #{inspect value}"
          end
        _ ->
          false
      end)
    else
      raise ArgumentError,
        "expected config file to return keyword list, got: #{inspect config}"
    end
  end

We can circumvent and use a first key which is not atom, and then the validate stops but does not throw:

[
  {"mockFirstKey", "mockValue"},
  myKey1: "myValue1",
  myKey2: "myValue2"
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!