Support java.util.List convertion

This commit is contained in:
Michael S. Klishin 2011-08-13 07:36:44 +04:00
parent bc8fccf429
commit b38b357f50
2 changed files with 15 additions and 4 deletions

View file

@ -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)))

View file

@ -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")))))