diff --git a/src/bombadil.app.src b/src/bombadil.app.src index fd7add0..8024ef3 100644 --- a/src/bombadil.app.src +++ b/src/bombadil.app.src @@ -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, diff --git a/src/bombadil.lfe b/src/bombadil.lfe index 47f20d9..ef4efdc 100644 --- a/src/bombadil.lfe +++ b/src/bombadil.lfe @@ -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) @@ -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 '()) diff --git a/test/bombadil-tests.lfe b/test/bombadil-tests.lfe index 7d6737d..a9387c1 100644 --- a/test/bombadil-tests.lfe +++ b/test/bombadil-tests.lfe @@ -64,4 +64,12 @@ #"i" #M(#"j" #M(#"k" #M(#"no" #"way"))) #"l" #M(#"stuff" #"fur reelies")) result))) - \ No newline at end of file + +(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"))))