Skip to content

Commit

Permalink
Implemented default support for get functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Nov 29, 2023
1 parent 622959d commit e081fc1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bombadil.app.src
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{application, 'bombadil',
[{description, "A wrapper for the Erlang toml library with convenient data extraction functions"},
[{description, "A wrapper for the Erlang tomerl library"},
{vsn, "0.4.0"},
{registered, []},
{applications,
Expand Down
8 changes: 6 additions & 2 deletions src/bombadil.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
(get parsed (key->bin key))))

(defun get (parsed key default)
(get parsed key))
(if (not (maps:is_key key parsed))
default
(get parsed key)))

(defun get-in
((parsed keys)
Expand All @@ -34,7 +36,9 @@
(err err)))))

(defun get-in (parsed key default)
(get-in parsed key))
(case (get-in parsed key)
(#(error not_found) default)
(result result)))

(defun assoc
((data '())
Expand Down
10 changes: 9 additions & 1 deletion test/bombadil-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@
#"i" #M(#"j" #M(#"k" #M(#"no" #"way")))
#"l" #M(#"stuff" #"fur reelies"))
result)))
(deftest defaults
(let ((`#(ok ,data) (bombadil:parse "a = \"b\"")))
;; quick sanity check and test of the parse func:
(is-equal #"b" (bombadil:get data "a"))
;; now for the defaults ...
(is-equal #"d" (bombadil:get data "c" #"d"))
(is-equal #"bleep" (bombadil:get-in data '("a" "b") #"bleep"))
(is-equal #"bloop" (bombadil:get-in data '("c" "d" "e") #"bloop"))))

0 comments on commit e081fc1

Please sign in to comment.