Skip to content

Commit

Permalink
fix visitor for joins with collections of maps
Browse files Browse the repository at this point in the history
  • Loading branch information
lilactown committed Nov 15, 2024
1 parent 270c29d commit 3e2c7ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ used to transform the data in a depth-first, post-order traversal (just like
:items {}})

(defn fullname
[{:keys [given-name surname] :as person}]
[_db {:keys [given-name surname] :as person}]
(str given-name " " surname))

(def query [{:people ^{:visitor fullname} [:given-name :surname]}])
Expand Down Expand Up @@ -118,7 +118,7 @@ It does not provide arbitrary logic like SQL or Datalog.

**Entity map**: a Clojure map which contains information that uniquely identifies
the domain entity it is about. E.g. `{:person/id 1234 :person/name "Bill"
:person/age 67}` could be uniquely identified by it's `:person/id` key. By
:person/age 67}` could be uniquely identified by it's `:person/id` key. By
default, any map which contains a key which `(= "id" (name key))` is true, is an
entity map and can be normalized using `pyramid.core/db`.

Expand Down
28 changes: 17 additions & 11 deletions src/pyramid/pull.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,23 @@
[(:children parent)
parent]))
k' (comp k #(vector (:key node) %))
k' (if-let [visitor (-> node :query meta :visitor)]
#(k' (visitor db %))
k')
node-visitor (if-let [visitor (some-> node :query meta :visitor)]
(fn [x]
(visitor db x))
identity)
union-child? (and (= 1 (count (:children node)))
(= :union (:type (first (:children node)))))]
(cond
(map? data)
;; handle union, which might have a visitor
(if (and union-child? (map? data))
#(visit k' db (first (:children node))
#(visit (comp k' node-visitor)
db (first (:children node))
{:data data
:parent new-parent
:entities entities})
(cc/into
k'
(comp k' node-visitor)
(with-meta {} (:meta node))
(comp
(cc/map (fn [k x]
Expand All @@ -257,14 +259,16 @@
;; f
(fn [k datum]
(if union-child?
#(visit (comp k (fn [x]
(with-meta x (:meta node))))
#(visit (comp k
node-visitor
(fn [x]
(with-meta x (:meta node))))
db (first children)
{:data datum
:parent new-parent
:entities entities})
(cc/into
k
(comp k node-visitor)
(with-meta (empty datum) (:meta node))
(comp
(cc/map (fn [k x]
Expand All @@ -282,14 +286,16 @@
(cc/map
(fn [k datum]
(if union-child?
#(visit (comp k (fn [x]
(with-meta x (:meta node))))
#(visit (comp k
node-visitor
(fn [x]
(with-meta x (:meta node))))
db (first children)
{:data datum
:parent new-parent
:entities entities})
(cc/into
k
(comp k node-visitor)
(with-meta (empty datum) (:meta node))
(comp
(cc/map (fn [k x]
Expand Down
17 changes: 9 additions & 8 deletions test/pyramid/pull_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@
(let [query [{:foo (visit [:bar :baz])}]
data {:foo [{:bar 123 :baz 456}
{:bar 789 :baz "qux"}]}]
(is (= {:foo (->Visited
[:bar :baz]
[{:bar 123 :baz 456} {:bar 789 :baz "qux"}])}
(is (= {:foo [(->Visited [:bar :baz] {:bar 123 :baz 456})
(->Visited [:bar :baz] {:bar 789 :baz "qux"})]}
(:data (trampoline p/pull-report data query)))
"multiple items")))
(testing "nested join"
Expand All @@ -93,14 +92,16 @@
(let [query [{:foo (visit [{:bar (visit [:baz])}])}]
data {:foo [{:bar {:baz 123}}
{:bar {:baz 456}}]}]
(is (= {:foo (->Visited
[{:bar (visit [:baz])}]
[{:bar (->Visited
(is (= {:foo [(->Visited
[{:bar [:baz]}]
{:bar (->Visited
[:baz]
{:baz 123})}
{:baz 123})})
(->Visited
[{:bar [:baz]}]
{:bar (->Visited
[:baz]
{:baz 456})}])}
{:baz 456})})]}
(:data (trampoline p/pull-report data query))))))
(testing "union"
(let [query [{:foo (visit
Expand Down

0 comments on commit 3e2c7ea

Please sign in to comment.