Skip to content

Commit fee782c

Browse files
committed
Pass through BigInteger/BigDeciman to freeze-fn in serializable (closes #465, closes #466)
1 parent 009c628 commit fee782c

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Implement “constant substitution” optimization for queries #462
44
- Fixed :max-eid for dangling entities during reader-based serialization #463
55
- Fixed tempid in unique refs #464
6+
- Pass through BigInteger/BigDeciman to freeze-fn in serializable #465 #466
67

78
# 1.6.3
89

src/datascript/serialize.cljc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@
137137
write-v (fn [v]
138138
(cond
139139
(string? v) v
140-
#?@(:clj [(ratio? v) (write-other v)])
140+
#?@(:clj [(or
141+
(instance? BigInteger v)
142+
(instance? BigDecimal v)
143+
(instance? clojure.lang.Ratio v)
144+
(instance? clojure.lang.BigInt v))
145+
(write-other v)])
141146

142147
(number? v)
143148
(cond

test/datascript/test/serialize.cljc

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
(ns datascript.test.serialize
22
(:require
3-
[#?(:cljs cljs.reader :clj clojure.edn) :as edn]
4-
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
5-
:clj [clojure.test :as t :refer [is are deftest testing]])
6-
[datascript.core :as d]
7-
[datascript.db :as db]
8-
[datascript.test.core :as tdc]
9-
#?(:clj [cheshire.core :as cheshire])
10-
#?(:clj [jsonista.core :as jsonista]))
11-
#?(:clj
12-
(:import [clojure.lang ExceptionInfo])))
3+
[#?(:cljs cljs.reader :clj clojure.edn) :as edn]
4+
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
5+
:clj [clojure.test :as t :refer [is are deftest testing]])
6+
[datascript.core :as d]
7+
[datascript.db :as db]
8+
[datascript.test.core :as tdc]
9+
#?(:clj [cheshire.core :as cheshire])
10+
#?(:clj [jsonista.core :as jsonista]))
11+
#?(:clj
12+
(:import [clojure.lang ExceptionInfo])))
1313

1414
(t/use-fixtures :once tdc/no-namespace-maps)
1515

@@ -31,17 +31,17 @@
3131
(is (= d (read-fn (pr-str d)))))
3232

3333
(let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}})
34-
(d/db-with [ [:db/add 1 :name "Petr"]
35-
[:db/add 1 :age 44] ])
36-
(d/db-with [ [:db/add 2 :name "Ivan"] ]))]
34+
(d/db-with [[:db/add 1 :name "Petr"]
35+
[:db/add 1 :age 44]])
36+
(d/db-with [[:db/add 2 :name "Ivan"]]))]
3737
(is (= (pr-str db)
38-
(str "#datascript/DB {"
39-
":schema {:name {:db/unique :db.unique/identity}}, "
40-
":datoms ["
41-
"[1 :age 44 536870913] "
42-
"[1 :name \"Petr\" 536870913] "
43-
"[2 :name \"Ivan\" 536870914]"
44-
"]}")))
38+
(str "#datascript/DB {"
39+
":schema {:name {:db/unique :db.unique/identity}}, "
40+
":datoms ["
41+
"[1 :age 44 536870913] "
42+
"[1 :name \"Petr\" 536870913] "
43+
"[2 :name \"Ivan\" 536870914]"
44+
"]}")))
4545
(is (= db (read-fn (pr-str db))))))))
4646

4747

@@ -54,34 +54,38 @@
5454
[1 :email "[email protected]"]
5555
[1 :avatar 10]
5656
[10 :url "http://"]
57-
[1 :attach { :some-key :some-value }]
57+
[1 :attach {:some-key :some-value}]
5858
[2 :name "Oleg"]
5959
[2 :age 30]
6060
[2 :email "[email protected]"]
61-
[2 :attach [ :just :values ]]
61+
[2 :attach [:just :values]]
6262
[3 :name "Ivan"]
6363
[3 :age 15]
6464
[3 :follows 2]
65-
[3 :attach { :another :map }]
65+
[3 :attach {:another :map}]
6666
[3 :avatar 30]
6767
[4 :name "Nick" d/tx0]
6868
[5 :inf ##Inf]
6969
[5 :-inf ##-Inf]
70+
#?@(:clj [[5 :ratio 22/7]
71+
[5 :bigint (bigint 100)]
72+
[5 :biginteger (biginteger 100)]
73+
[5 :bigdec (bigdec 100.005)]])
7074
;; check that facts about transactions doesn’t set off max-eid
7175
[d/tx0 :txInstant 0xdeadbeef]
72-
[30 :url "https://" ]])
76+
[30 :url "https://"]])
7377

7478

7579
(def schema
76-
{ :name { } ;; nothing special about name
77-
:aka { :db/cardinality :db.cardinality/many }
78-
:age { :db/index true }
79-
:follows { :db/valueType :db.type/ref }
80-
:email { :db/unique :db.unique/identity }
81-
:avatar { :db/valueType :db.type/ref, :db/isComponent true }
82-
:url { } ;; just a component prop
83-
:attach { } ;; should skip index
84-
})
80+
{:name {} ;; nothing special about name
81+
:aka {:db/cardinality :db.cardinality/many}
82+
:age {:db/index true}
83+
:follows {:db/valueType :db.type/ref}
84+
:email {:db/unique :db.unique/identity}
85+
:avatar {:db/valueType :db.type/ref, :db/isComponent true}
86+
:url {} ;; just a component prop
87+
:attach {} ;; should skip index
88+
})
8589

8690

8791
(deftest test-init-db
@@ -98,7 +102,7 @@
98102
(testing "db-init produces the same max-eid as regular transactions"
99103
(let [assertions [ [:db/add -1 :name "Lex"] ]]
100104
(is (= (d/db-with db-init assertions)
101-
(d/db-with db-transact assertions)))))
105+
(d/db-with db-transact assertions)))))
102106

103107
(testing "Roundtrip"
104108
(doseq [[r read-fn] readers]

0 commit comments

Comments
 (0)