support java Bigdecimal in mongodb 3.4 or later
This commit is contained in:
parent
be43faf059
commit
5d31f9fc1a
3 changed files with 17 additions and 5 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
:license {:name "Eclipse Public License"
|
:license {:name "Eclipse Public License"
|
||||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||||
:dependencies [[org.clojure/clojure "1.8.0"]
|
:dependencies [[org.clojure/clojure "1.8.0"]
|
||||||
[org.mongodb/mongodb-driver "3.4.2"]
|
[org.mongodb/mongodb-driver "3.6.0-beta2"]
|
||||||
[clojurewerkz/support "1.1.0"]]
|
[clojurewerkz/support "1.1.0"]]
|
||||||
:test-selectors {:default (fn [m]
|
:test-selectors {:default (fn [m]
|
||||||
(and (not (:performance m))
|
(and (not (:performance m))
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,8 @@
|
||||||
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
||||||
[clojure.lang IPersistentMap Named Keyword Ratio]
|
[clojure.lang IPersistentMap Named Keyword Ratio]
|
||||||
[java.util List Map Date Set]
|
[java.util List Map Date Set]
|
||||||
org.bson.types.ObjectId))
|
org.bson.types.ObjectId
|
||||||
|
(org.bson.types Decimal128)))
|
||||||
|
|
||||||
(defprotocol ConvertToDBObject
|
(defprotocol ConvertToDBObject
|
||||||
(^com.mongodb.DBObject to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
(^com.mongodb.DBObject to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
||||||
|
|
@ -104,7 +105,6 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defprotocol ConvertFromDBObject
|
(defprotocol ConvertFromDBObject
|
||||||
(from-db-object [input keywordize] "Converts given DBObject instance to a piece of Clojure data"))
|
(from-db-object [input keywordize] "Converts given DBObject instance to a piece of Clojure data"))
|
||||||
|
|
||||||
|
|
@ -115,6 +115,11 @@
|
||||||
Object
|
Object
|
||||||
(from-db-object [input keywordize] input)
|
(from-db-object [input keywordize] input)
|
||||||
|
|
||||||
|
Decimal128
|
||||||
|
(from-db-object [^Decimal128 input keywordize]
|
||||||
|
(.bigDecimalValue input)
|
||||||
|
)
|
||||||
|
|
||||||
List
|
List
|
||||||
(from-db-object [^List input keywordize]
|
(from-db-object [^List input keywordize]
|
||||||
(vec (map #(from-db-object % keywordize) input)))
|
(vec (map #(from-db-object % keywordize) input)))
|
||||||
|
|
@ -140,7 +145,6 @@
|
||||||
{} (.keySet input))))
|
{} (.keySet input))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defprotocol ConvertToObjectId
|
(defprotocol ConvertToObjectId
|
||||||
(^org.bson.types.ObjectId to-object-id [input] "Instantiates ObjectId from input unless the input itself is an ObjectId instance. In that case, returns input as is."))
|
(^org.bson.types.ObjectId to-object-id [input] "Instantiates ObjectId from input unless the input itself is an ObjectId instance. In that case, returns input as is."))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
[monger.conversion :refer :all])
|
[monger.conversion :refer :all])
|
||||||
(:import [com.mongodb DBObject BasicDBObject BasicDBList]
|
(:import [com.mongodb DBObject BasicDBObject BasicDBList]
|
||||||
[java.util Date Calendar List ArrayList]
|
[java.util Date Calendar List ArrayList]
|
||||||
org.bson.types.ObjectId))
|
org.bson.types.ObjectId
|
||||||
|
(org.bson.types Decimal128)))
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
@ -101,6 +102,13 @@
|
||||||
(is (= 2 (from-db-object 2 false)))
|
(is (= 2 (from-db-object 2 false)))
|
||||||
(is (= 2 (from-db-object 2 true))))
|
(is (= 2 (from-db-object 2 true))))
|
||||||
|
|
||||||
|
(deftest convert-decimal-from-dbobject
|
||||||
|
(is (= 2.3M (from-db-object (Decimal128. 2.3M) false)))
|
||||||
|
(is (= 2.3M (from-db-object (Decimal128. 2.3M) true)))
|
||||||
|
(is (= 2.3M (from-db-object (Decimal128/parse "2.3") true)))
|
||||||
|
(is (not= 2.32M (from-db-object (Decimal128/parse "2.3") true)))
|
||||||
|
)
|
||||||
|
|
||||||
(deftest convert-float-from-dbobject
|
(deftest convert-float-from-dbobject
|
||||||
(is (= 3.3 (from-db-object 3.3 false)))
|
(is (= 3.3 (from-db-object 3.3 false)))
|
||||||
(is (= 3.3 (from-db-object 3.3 true))))
|
(is (= 3.3 (from-db-object 3.3 true))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue