如何读取elixir混合项目的配置文件 [英] How to read config files on elixir mix project

查看:135
本文介绍了如何读取elixir混合项目的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个elixir项目来搜索文件中的模式。
我想将这些模式存储在一个配置文件中,以便在应用程序中进行简单的更改。
我的第一个想法是将这些文件作为exs文件存储在混合项目的config文件夹中。
所以,问题是:


  1. 有什么简单的方法可以将配置文件存储在关键字列表中吗? li>
  2. 我如何在应用程序中加载它?

我看到有像File这样的模块来读取文件,但是没有标准的方法来解析elixir中的关键字列表吗?我正在考虑类似于Rails中的yml文件。

解决方案

您可以读取存储在* .exs文件中的关键字列表,使用 Mix.Config.read(path)。要将Elixir条款写入* .exs文件,可以使用 Inspect.Algebra.to_doc(%Inspect.Opts {pretty:true}),并将结果字符串内容写入一个使用 File.write 的文件。它的格式不像你手工制作的那么好,但它绝对是可读的。



如果你不介意使用Erlang术语,你可以读写那些使用:file.consult(path):file.write_file(:io_lib.fwrite('〜p.\\\
',[config ]),path)


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.

解决方案

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.

这篇关于如何读取elixir混合项目的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆