A wrapper for the Erlang toml library that uses the map data structure
About ↟
This is an LFE TOML library 100% usable in any other BEAM language that is
capable of building rebar3
-based projects. While it used to offer unique
features for the old Erlang TOML library, the newer tomerl library provides
very similar features. As such, this library has become little more than a
convenience wrapper for tomerl, with just a touch of sugar on top.
Build ↟
rebar3 compile
Tests ↟
rebar3 as test ltest
Usage ↟
Add to your rebar.config
file:
{deps, [
{bombadil, "0.4.0", {pkg, toml_bombadil}}
]}.
Read a TOML file, parse its contents, and convert the parsed data to an Erlang map:
(set `#(ok ,data) (bombadil:read "priv/testing/deep-sections.toml"))
or from Erlang:
1> {ok, Data} = bombadil:read("priv/testing/deep-sections.toml").
One may work with the resulting data structure in the following ways:
lfe> (bombadil:get data "ab")
42
lfe> (bombadil:get-in data '("a" "b" "d" "e"))
#M("in" "deep")
lfe> (bombadil:assoc-in data '("a" "b" "d" "e") 42)
#M(#"ab" 42
#"cd" 3.14159
#"a" #M(#"stuff" #"things"
#"b" #M(#"other" #"stuff"
#"c" #M(#"things" #"n stuff")
#"d" #M(#"things" #"n other things"
#"e" 42))
#"f" #M(#"other" #"things"))
#"g" #M(#"h" #M(#"woah" #"wut"))
#"i" #M(#"j" #M(#"k" #M(#"no" #"way")))
#"l" #M(#"stuff" #"fur reelies"))
or from Erlang:
2> bombadil:get(Data, "ab").
42
3> bombadil:get_in(Data, ["a", "b", "d", "e"]).
#{<<"in">> => <<"deep">>}
4> bombadil:assoc_in(Data, ["a", "b", "d", "e"], 42).
#{<<"a">> =>
#{<<"b">> =>
#{<<"c">> => #{<<"things">> => <<"n stuff">>},
<<"d">> =>
#{<<"e">> => 42,<<"things">> => <<"n other things">>},
<<"other">> => <<"stuff">>},
<<"f">> => #{<<"other">> => <<"things">>},
<<"stuff">> => <<"things">>},
<<"ab">> => 42,<<"cd">> => 3.14159,
<<"g">> => #{<<"h">> => #{<<"woah">> => <<"wut">>}},
<<"i">> =>
#{<<"j">> => #{<<"k">> => #{<<"no">> => <<"way">>}}},
<<"l">> => #{<<"stuff">> => <<"fur reelies">>}}
Note that since all of these functions have data
as their first argument, the
"thread-left" macro in LFE -- ->
-- may be used to chain outputs:
lfe> (include-lib "lfe/include/clj.lfe")
lfe> (-> data
(bombadil:assoc-in '("a" "b" "d" "e") 42)
(bombadil:get-in '("a" "b" "d" "e")))
42
That last is, of course, a contrived example; those who have used the data threading macros in the past are well familiar with the benefits (mostly in code clarity and thus ease of debugging and maintenance).
Unfortunately this last LFE macro convenience is not usable from Erlang.
License ↟
Apache License, Version 2.0
Copyright © 2020-2023, Duncan McGreggor oubiwann@gmail.com.