Further work on monger.convertion protocols

This commit is contained in:
Michael S. Klishin 2011-08-13 07:30:54 +04:00
parent 6c0eb8f1e0
commit bc8fccf429
2 changed files with 15 additions and 6 deletions

View file

@ -1,6 +1,6 @@
(ns monger.convertion
(:import (com.mongodb DBObject BasicDBObject)
(java.util Map List)))
(clojure.lang IPersistentMap Keyword)))
(defprotocol ConvertToDBObject
(to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
@ -9,13 +9,21 @@
nil
(to-db-object [input]
input)
Object
(to-db-object [input]
input)
Map
(to-db-object [input]
{}))
Keyword
(to-db-object [#^Keyword o] (.getName o))
IPersistentMap
(to-db-object [#^IPersistentMap input]
(let [o (BasicDBObject.)]
(doseq [[k v] input]
(.put o (to-db-object k) (to-db-object v)))
o)))

View file

@ -27,4 +27,5 @@
(let [input { :int 1, :string "Mongo", :float 22.23 }
output (monger.convertion/to-db-object input)]
(is (= 1 (.get output "int")))
))
(is (= "Mongo" (.get output "string")))
(is (= 22.23 (.get output "float")))))