Support java.util.List convertion
This commit is contained in:
parent
bc8fccf429
commit
b38b357f50
2 changed files with 15 additions and 4 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
(ns monger.convertion
|
(ns monger.convertion
|
||||||
(:import (com.mongodb DBObject BasicDBObject)
|
(:import (com.mongodb DBObject BasicDBObject)
|
||||||
(clojure.lang IPersistentMap Keyword)))
|
(clojure.lang IPersistentMap Keyword)
|
||||||
|
(java.util List Map)))
|
||||||
|
|
||||||
(defprotocol ConvertToDBObject
|
(defprotocol ConvertToDBObject
|
||||||
(to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
(to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
||||||
|
|
@ -15,14 +16,18 @@
|
||||||
input)
|
input)
|
||||||
|
|
||||||
Keyword
|
Keyword
|
||||||
(to-db-object [#^Keyword o] (.getName o))
|
(to-db-object [#^Keyword input] (.getName input))
|
||||||
|
|
||||||
IPersistentMap
|
IPersistentMap
|
||||||
(to-db-object [#^IPersistentMap input]
|
(to-db-object [#^IPersistentMap input]
|
||||||
(let [o (BasicDBObject.)]
|
(let [o (BasicDBObject.)]
|
||||||
(doseq [[k v] input]
|
(doseq [[k v] input]
|
||||||
(.put o (to-db-object k) (to-db-object v)))
|
(.put o (to-db-object k) (to-db-object v)))
|
||||||
o)))
|
o))
|
||||||
|
|
||||||
|
List
|
||||||
|
(to-db-object [#^List input] (map to-db-object input)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,9 @@
|
||||||
(is (= 1 (.get output "int")))
|
(is (= 1 (.get output "int")))
|
||||||
(is (= "Mongo" (.get output "string")))
|
(is (= "Mongo" (.get output "string")))
|
||||||
(is (= 22.23 (.get output "float")))))
|
(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")))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue