如何将Erlang sys.config转换为Elixir config.exs?

时间:2015-11-10 09:58:51

标签: erlang elixir

这可能很简单。 例如,这个sys.config进入mix config.exs:

{gsms, [{interfaces,
  [
   {gsms_0705, 1, [{device,"/dev/tty.usbserial-FTF5DP2J"},
           {bnumber, "<phone-number>"},{baud,19200},
           {reopen_timeout, 5000}]}
   {gsms_0705, 2, [{device,"/dev/tty.HUAWEIMobile-Pcui"},
           {bnumber, "<phone-number>"},
           {reopen_timeout, 5000}]}
   {gsms_0705, 3, [{device, "/dev/tty.usbserial"},
          {bnumber, "<phone-number>"},
          {baud, 9600}]}
  ]}
]}

2 个答案:

答案 0 :(得分:3)

如果您修复语法错误(缺少逗号)并添加强制性尾随点,您甚至可以从Elixir解析Erlang术语并让解析器为您进行转换:

erlang_term = '{gsms, [{interfaces, [
  {gsms_0705, 1, [{device,"/dev/tty.usbserial-FTF5DP2J"},
                  {bnumber, "<phone-number>"},{baud,19200},
                  {reopen_timeout, 5000}]},
  {gsms_0705, 2, [{device,"/dev/tty.HUAWEIMobile-Pcui"},
                  {bnumber, "<phone-number>"},
                  {reopen_timeout, 5000}]},
  {gsms_0705, 3, [{device, "/dev/tty.usbserial"},
                  {bnumber, "<phone-number>"},
                  {baud, 9600}]}
 ]}
]}.'

{:ok, tokens, _} = :erl_scan.string(erlang_term)
{:ok, result} = :erl_parse.parse_term(tokens)
IO.inspect result, limit: :infinity

打印:

{:gsms,
 [interfaces: [{:gsms_0705, 1,
    [device: '/dev/tty.usbserial-FTF5DP2J', bnumber: '<phone-number>',
     baud: 19200, reopen_timeout: 5000]},
   {:gsms_0705, 2,
    [device: '/dev/tty.HUAWEIMobile-Pcui', bnumber: '<phone-number>',
     reopen_timeout: 5000]},
   {:gsms_0705, 3,
    [device: '/dev/tty.usbserial', bnumber: '<phone-number>', baud: 9600]}]]}

元组的第一部分是应用程序名称:gsms,元组的另一部分是包含实际选项的proplist。您可以放下周围的括号并稍微重新格式化,然后您将获得:

config :gsms, interfaces: [
  {:gsms_0705, 1, [
    device: '/dev/tty.usbserial-FTF5DP2J',
    bnumber: '<phone-number>',
    baud: 19200,
    reopen_timeout: 5000]},
  {:gsms_0705, 2, [
    device: '/dev/tty.HUAWEIMobile-Pcui',
    bnumber: '<phone-number>',
    reopen_timeout: 5000]},
  {:gsms_0705, 3, [
    device: '/dev/tty.usbserial',
    bnumber: '<phone-number>',
    baud: 9600]}]

答案 1 :(得分:1)

我想这就是它:

config :gsms, :interfaces, [
  {:gsms_0705, 1, [device: '/dev/tty.usbserial-FTF5DP2J',
                   bnumber: '<phone-number>',
                   baud: 19200,
                   reopen_timeout: 5000]},
  # and so on
]

:gsms是您要配置的应用,:interfaces是您通过Application.get_env/2Application.get_env(:gsms, :interfaces))和{:gsms_*, ...}列表检索的密钥} tuples是该键的值。