-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code for tomerl 0.5.0 library.
- Loading branch information
Showing
3 changed files
with
138 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,26 +21,10 @@ | |
## About [↟](#table-of-contents) | ||
|
||
This is an LFE TOML library 100% usable in any other BEAM language that is | ||
capable of building `rebar3`-based projects. It does a couple of useful and | ||
convenient things: | ||
|
||
1. Provides a `read` function that recursively converts all of the `toml` | ||
Erlang `dict`s to maps and converts the `toml` library's type tuples to | ||
just the values themselves. | ||
1. Provides Clojure-inspired functions for accessing and updating map data | ||
(`assoc`, `assoc-in`, `get-in`). Note however that, due to the fact that | ||
Erlang and LFE, unlike Clojure, do not support arbitrary arity in function | ||
definitions, the signature and use of some of these functions is different | ||
than the similar ones in Clojure. | ||
1. Provides a [set of functions](https://github.com/lfex/bombadil/blob/master/src/bombadil.lfe#L10) | ||
in the `bombadil` module which just call the | ||
[same functions](http://dozzie.jarowit.net/api/erlang-toml/default/toml.html#index) | ||
in the `toml` library. Note that these aliases have been Lispified | ||
(underscores to dashes); for Erlangers we've supplied aliases with | ||
underscores. | ||
1. Lastly, makes public the utility functions used for the conversion from | ||
`toml` dicts to `bombadil` maps, in the event that these are useful for | ||
your own project (or debugging). | ||
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 [↟](#table-of-contents) | ||
|
||
|
@@ -72,13 +56,13 @@ Read a TOML file, parse its contents, and convert the parsed data to an Erlang | |
map: | ||
|
||
```lisp | ||
(set data (bombadil:read "priv/testing/deep-sections.toml")) | ||
(set `#(ok ,data) (bombadil:read "priv/testing/deep-sections.toml")) | ||
``` | ||
|
||
or from Erlang: | ||
|
||
```erlang | ||
1> Data = bombadil:read("priv/testing/deep-sections.toml"). | ||
1> {ok, Data} = bombadil:read("priv/testing/deep-sections.toml"). | ||
``` | ||
|
||
One may work with the resulting data structure in the following ways: | ||
|
@@ -89,17 +73,17 @@ lfe> (bombadil:get data "ab") | |
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")) | ||
#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: | ||
|
@@ -108,19 +92,21 @@ or from Erlang: | |
2> bombadil:get(Data, "ab"). | ||
42 | ||
3> bombadil:get_in(Data, ["a", "b", "d", "e"]). | ||
#{"in" => "deep"} | ||
#{<<"in">> => <<"deep">>} | ||
4> bombadil:assoc_in(Data, ["a", "b", "d", "e"], 42). | ||
#{"ab" => 42, | ||
"cd" => 3.14159, | ||
"a" => #{"stuff" => "things" | ||
"b" => #{"other" => "stuff" | ||
"c" => #{"things" => "n stuff"}, | ||
"d" => #{"things" => "n other things" | ||
"e" => 42,}}, | ||
"f" => #{"other" => "things"}}, | ||
"g" => #{"h" => #{"woah" => "wut"}}, | ||
"i" => #{"j" => #{"k" => #{"no" => "way"}}}, | ||
"l" => #{"stuff" => "fur reelies"}} | ||
#{<<"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 | ||
|
@@ -134,17 +120,17 @@ lfe> (-> data | |
42 | ||
``` | ||
|
||
This is, of course, a contrived example; those who have used the data threading | ||
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 LFE macro convenience is not usable from Erlang. | ||
Unfortunately this last LFE macro convenience is not usable from Erlang. | ||
|
||
## License [↟](#table-of-contents) | ||
|
||
Apache License, Version 2.0 | ||
|
||
Copyright © 2020, Duncan McGreggor <[email protected]>. | ||
Copyright © 2020-2023, Duncan McGreggor <[email protected]>. | ||
|
||
[//]: ---Named-Links--- | ||
|
||
|
Oops, something went wrong.