diff --git a/src/monger/convertion.clj b/src/monger/convertion.clj new file mode 100644 index 0000000..36c2d6f --- /dev/null +++ b/src/monger/convertion.clj @@ -0,0 +1,23 @@ +(ns monger.convertion + (:import (com.mongodb DBObject BasicDBObject) + (java.util Map List))) + +(defprotocol ConvertToDBObject + (to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses")) + +(extend-protocol ConvertToDBObject + nil + (to-db-object [input] + input) + Object + (to-db-object [input] + input) + Map + (to-db-object [input] + {})) + + + + +(defprotocol ConvertFromDBObject + (from-db-object [input] "Converts given DBObject instance to a piece of Clojure data")) diff --git a/test/monger/test/convertion.clj b/test/monger/test/convertion.clj new file mode 100644 index 0000000..606f5a2 --- /dev/null +++ b/test/monger/test/convertion.clj @@ -0,0 +1,30 @@ +(ns monger.test.convertion + (:require [monger core collection convertion]) + (:use [clojure.test])) + +(deftest convert-nil-to-dbobject + (let [input nil + output (monger.convertion/to-db-object input)] + (is (nil? output)))) + +(deftest convert-integer-to-dbobject + (let [input 1 + output (monger.convertion/to-db-object input)] + (is (= input output)))) + +(deftest convert-float-to-dbobject + (let [input 11.12 + output (monger.convertion/to-db-object input)] + (is (= input output)))) + +(deftest convert-string-to-dbobject + (let [input "MongoDB" + output (monger.convertion/to-db-object input)] + (is (= input output)))) + + +(deftest convert-map-to-dbobject + (let [input { :int 1, :string "Mongo", :float 22.23 } + output (monger.convertion/to-db-object input)] + (is (= 1 (.get output "int"))) + ))