monger.conversion/to-object-id
This commit is contained in:
parent
1a8eb1ef80
commit
c25609a5c3
2 changed files with 35 additions and 4 deletions
|
|
@ -22,9 +22,10 @@
|
||||||
;; THE SOFTWARE.
|
;; THE SOFTWARE.
|
||||||
|
|
||||||
(ns monger.conversion
|
(ns monger.conversion
|
||||||
(:import (com.mongodb DBObject BasicDBObject BasicDBList DBCursor)
|
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
||||||
(clojure.lang IPersistentMap Keyword)
|
[clojure.lang IPersistentMap Keyword]
|
||||||
(java.util List Map)))
|
[java.util List Map Date]
|
||||||
|
[org.bson.types ObjectId]))
|
||||||
|
|
||||||
(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"))
|
||||||
|
|
@ -101,3 +102,22 @@
|
||||||
(assoc m k (from-db-object v false))))
|
(assoc m k (from-db-object v false))))
|
||||||
{} (reverse pairs)))
|
{} (reverse pairs)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(defprotocol ConvertToObjectId
|
||||||
|
(to-object-id [input] "Instantiates ObjectId from input unless the input itself is an ObjectId instance. In that case, returns input as is."))
|
||||||
|
|
||||||
|
(extend-protocol ConvertToObjectId
|
||||||
|
String
|
||||||
|
(to-object-id [^String input]
|
||||||
|
(ObjectId. input))
|
||||||
|
|
||||||
|
Date
|
||||||
|
(to-object-id [^Date input]
|
||||||
|
(ObjectId. input))
|
||||||
|
|
||||||
|
ObjectId
|
||||||
|
(to-object-id [^ObjectId input]
|
||||||
|
input))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
(:require [monger core collection]
|
(:require [monger core collection]
|
||||||
[monger.conversion :as cnv])
|
[monger.conversion :as cnv])
|
||||||
(:import [com.mongodb DBObject BasicDBObject BasicDBList]
|
(:import [com.mongodb DBObject BasicDBObject BasicDBList]
|
||||||
[java.util Date Calendar List ArrayList])
|
[java.util Date Calendar List ArrayList]
|
||||||
|
[org.bson.types ObjectId])
|
||||||
(:use [clojure.test]))
|
(:use [clojure.test]))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -131,3 +132,13 @@
|
||||||
(is (= (-> output (get "nested") (get "list")) ["red" "green" "blue"]))
|
(is (= (-> output (get "nested") (get "list")) ["red" "green" "blue"]))
|
||||||
(is (= (-> output (get "nested") (get "dblist")) [0 1]))))
|
(is (= (-> output (get "nested") (get "dblist")) [0 1]))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; ObjectId coercion
|
||||||
|
;;
|
||||||
|
|
||||||
|
(deftest test-conversion-to-object-id
|
||||||
|
(let [output (ObjectId. "4efb39370364238a81020502")]
|
||||||
|
(is (= output (cnv/to-object-id "4efb39370364238a81020502")))
|
||||||
|
(is (= output (cnv/to-object-id output)))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue