Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass keys to tag readers via opts. #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/aero/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,23 @@
(fn [v]
(tagged-literal (:tag tl) v)))

(defn- pop*
[ks]
;; pop doesn't work with clojure.lang.Cons so we use pop
;; for vector and rest for anything that resembles a list
(if (vector? ks)
(pop ks)
(rest ks)))

(defmethod eval-tagged-literal :default
[tl opts env ks]
(let [{:keys [:aero.core/incomplete?] :as expansion}
(expand (:form tl) opts env ks)]
(if incomplete?
(update expansion ::value (rewrap tl))
(update expansion ::value #(reader opts (:tag tl) %)))))
(update expansion ::value #(reader (assoc opts :key-path (pop* ks))
(:tag tl)
%)))))

(defmethod eval-tagged-literal 'ref
[tl opts env ks]
Expand Down
11 changes: 11 additions & 0 deletions test/aero/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
[opts tag value]
(if (= value :favorite) :chocolate :vanilla))

(defmethod reader 'return-key-path
[opts _ _]
(:key-path opts))

(deftest basic-test
(let [config (read-config "test/aero/config.edn")]
(is (= "Hello World!" (:greeting config))))
Expand Down Expand Up @@ -250,3 +254,10 @@
{:a :b}
#{:a :b}
'(1)))

(deftest pass-keys-to-reader
(is (= [:a :b :c]
(get-in (read-config
(string-reader
"{:a {:b {:c #return-key-path 1}}}"))
[:a :b :c]))))