diff --git a/src/monger/convertion.clj b/src/monger/convertion.clj index 454b1c1..ed662e4 100644 --- a/src/monger/convertion.clj +++ b/src/monger/convertion.clj @@ -1,6 +1,7 @@ (ns monger.convertion (:import (com.mongodb DBObject BasicDBObject) - (clojure.lang IPersistentMap Keyword))) + (clojure.lang IPersistentMap Keyword) + (java.util List Map))) (defprotocol ConvertToDBObject (to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses")) @@ -15,14 +16,18 @@ input) Keyword - (to-db-object [#^Keyword o] (.getName o)) - + (to-db-object [#^Keyword input] (.getName input)) + IPersistentMap (to-db-object [#^IPersistentMap input] (let [o (BasicDBObject.)] (doseq [[k v] input] (.put o (to-db-object k) (to-db-object v))) - o))) + o)) + + List + (to-db-object [#^List input] (map to-db-object input))) + diff --git a/test/monger/test/convertion.clj b/test/monger/test/convertion.clj index 5646649..628fa38 100644 --- a/test/monger/test/convertion.clj +++ b/test/monger/test/convertion.clj @@ -29,3 +29,9 @@ (is (= 1 (.get output "int"))) (is (= "Mongo" (.get output "string"))) (is (= 22.23 (.get output "float"))))) + + +(deftest convert-nested-map-to-dbobject + (let [input { :int 1, :string "Mongo", :float 22.23, :map { :int 10, :string "Clojure", :float 11.9, :list '(1 "a" :b) } } + output (monger.convertion/to-db-object input)] + (is (= 10 (.get (.get output "map") "int")))))