From 5d31f9fc1a4a6bce4b8a610d06dc71593c52e3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=BF=A0=E5=AD=9D?= <32647908@qq.com> Date: Sat, 18 Nov 2017 11:31:45 +0800 Subject: [PATCH] support java Bigdecimal in mongodb 3.4 or later --- project.clj | 2 +- src/clojure/monger/conversion.clj | 10 +++++++--- test/monger/test/conversion_test.clj | 10 +++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/project.clj b/project.clj index f24d283..4c92739 100644 --- a/project.clj +++ b/project.clj @@ -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)) diff --git a/src/clojure/monger/conversion.clj b/src/clojure/monger/conversion.clj index 8ed2c5f..02e97a5 100644 --- a/src/clojure/monger/conversion.clj +++ b/src/clojure/monger/conversion.clj @@ -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.")) diff --git a/test/monger/test/conversion_test.clj b/test/monger/test/conversion_test.clj index c989574..7e3e49a 100644 --- a/test/monger/test/conversion_test.clj +++ b/test/monger/test/conversion_test.clj @@ -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))))