Skip to content

Commit

Permalink
:xform is not called on ref attributes (fixes #455)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Oct 17, 2023
1 parent f4ac053 commit 941c2b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions dev/user.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns user
(:require
[clojure.test :as t]
[clojure.tools.namespace.repl :as ns]))

(ns/set-refresh-dirs "src" "bench" "test" #_"bench_datomic" #_"test_datomic")
Expand Down Expand Up @@ -29,3 +30,7 @@
(locking lock
(println (str "#p" (position) " " '~form " => (" (- (System/currentTimeMillis) t#) " ms) " res#)))
res#))

(defn test-all []
(reload)
(t/run-all-tests #"datascript\..*"))
8 changes: 4 additions & 4 deletions src/datascript/pull_api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
:let [^Datom datom (first-seq datoms)]

(or (nil? datom) (not= (.-a datom) (.-name attr)))
[(ResultFrame. ((.-xform attr) (not-empty (persistent! acc))) (or datoms ()))]
[(ResultFrame. (not-empty (persistent! acc)) (or datoms ()))]

; got limit, skip rest of the datoms
(and (.-limit attr) (>= (count acc) (.-limit attr)))
Expand Down Expand Up @@ -95,7 +95,7 @@
:let [^Datom datom (first-seq datoms)]

(or (nil? datom) (not= (.-a datom) (.-name attr)))
[(ResultFrame. ((.-xform attr) (not-empty (persistent! acc))) (or datoms ()))]
[(ResultFrame. (not-empty (persistent! acc)) (or datoms ()))]

; got limit, skip rest of the datoms
(and (.-limit attr) (>= (count acc) (.-limit attr)))
Expand All @@ -119,7 +119,7 @@
(AttrsFrame.
seen
recursion-limits
(assoc-some! acc (.-as attr) (.-value ^ResultFrame result))
(assoc-some! acc (.-as attr) ((.-xform attr) (.-value ^ResultFrame result)))
pattern
(first-seq attrs)
(next-seq attrs)
Expand Down Expand Up @@ -202,7 +202,7 @@
(ReverseAttrsFrame.
seen
recursion-limits
(assoc-some! acc (.-as attr) (.-value ^ResultFrame result))
(assoc-some! acc (.-as attr) ((.-xform attr) (.-value ^ResultFrame result)))
pattern
(first-seq attrs)
(next-seq attrs)
Expand Down
20 changes: 16 additions & 4 deletions test/datascript/test/pull_api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,24 @@
:aka [["Devil" "Tupen"]]
:child [[{:db/id [2], :name ["David"], :aka [nil], :child [nil]}
{:db/id [3], :name ["Thomas"], :aka [nil], :child [nil]}]]}
(d/pull test-db
'[[:db/id :xform vector]
(d/pull test-db
[[:db/id :xform vector]
[:name :xform vector]
[:aka :xform vector]
{[:child :xform vector] ...}]
1)))
{[:child :xform vector] '...}]
1)))

(testing ":xform on cardinality/one ref #455"
(is (= {:name "David" :father "Petr"}
(d/pull test-db [:name {[:father :xform #(:name %)] ['*]}] 2))))

(testing ":xform on reverse ref"
(is (= {:name "Petr" :_father ["David" "Thomas"]}
(d/pull test-db [:name {[:_father :xform #(mapv :name %)] [:name]}] 1))))

(testing ":xform on reverse component ref"
(is (= {:name "Part A.A" :_part "Part A"}
(d/pull test-db [:name {[:_part :xform #(:name %)] [:name]}] 11))))

(testing "missing attrs are processed by xform"
(is (= {:normal [nil]
Expand Down

0 comments on commit 941c2b1

Please sign in to comment.