Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read config files on elixir mix project

I am creating an elixir project to search for patterns in files. I want to store those patterns a config files to allow for easy changes in the app. My first idea is storing those files as exs files in the config folder in the mix project. So, the questions are:

  1. Is there any easy way to store the config in the files a a keyword list?
  2. How would I load it in the app?

I see there are modules like File to read the file, but is there no standard way to parse keyword lists in elixir? I was thinking something similar as the yml files in Rails.

like image 653
Batou99 Avatar asked May 27 '14 19:05

Batou99


2 Answers

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.

like image 178
bitwalker Avatar answered Sep 21 '22 02:09

bitwalker


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"
]
like image 23
elpddev Avatar answered Sep 19 '22 02:09

elpddev