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"
|
||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||
: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"]]
|
||||
:test-selectors {:default (fn [m]
|
||||
(and (not (:performance m))
|
||||
|
|
|
|||
|
|
@ -46,7 +46,8 @@
|
|||
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
||||
[clojure.lang IPersistentMap Named Keyword Ratio]
|
||||
[java.util List Map Date Set]
|
||||
org.bson.types.ObjectId))
|
||||
org.bson.types.ObjectId
|
||||
(org.bson.types Decimal128)))
|
||||
|
||||
(defprotocol ConvertToDBObject
|
||||
(^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
|
||||
(from-db-object [input keywordize] "Converts given DBObject instance to a piece of Clojure data"))
|
||||
|
||||
|
|
@ -115,6 +115,11 @@
|
|||
Object
|
||||
(from-db-object [input keywordize] input)
|
||||
|
||||
Decimal128
|
||||
(from-db-object [^Decimal128 input keywordize]
|
||||
(.bigDecimalValue input)
|
||||
)
|
||||
|
||||
List
|
||||
(from-db-object [^List input keywordize]
|
||||
(vec (map #(from-db-object % keywordize) input)))
|
||||
|
|
@ -140,7 +145,6 @@
|
|||
{} (.keySet input))))
|
||||
|
||||
|
||||
|
||||
(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."))
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@
|
|||
[monger.conversion :refer :all])
|
||||
(:import [com.mongodb DBObject BasicDBObject BasicDBList]
|
||||
[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 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
|
||||
(is (= 3.3 (from-db-object 3.3 false)))
|
||||
(is (= 3.3 (from-db-object 3.3 true))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue