Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for keywords with a namespace has been added. #61

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/somnium/congomongo/coerce.clj
Expand Up @@ -17,13 +17,19 @@
(defprotocol ConvertibleFromMongo
(mongo->clojure [o keywordize]))

(def ^{:private true} dot-replacement "_DOT_")

(defn- to-keyword [s]
(let [s (.replaceAll s dot-replacement ".")]
(keyword s)))

(defn- assocs->clojure [kvs keywordize]
;; Taking the keywordize test out of the fn reduces derefs
;; dramatically, which was the main barrier to matching pure-Java
;; performance for this marshalling
(reduce (if keywordize
(fn [m [^String k v]]
(assoc m (keyword k) (mongo->clojure v true)))
(assoc m (to-keyword k) (mongo->clojure v true)))
(fn [m [^String k v]]
(assoc m k (mongo->clojure v false))))
{} (reverse kvs)))
Expand Down Expand Up @@ -65,12 +71,16 @@
(clojure->mongo [m] (let [dbo (BasicDBObject.)]
(doseq [[k v] m]
(.put dbo
(clojure->mongo k)
(if (keyword? k)
(if-let [n (namespace k)]
(str (.replaceAll n "\\." dot-replacement) "/" (name k))
(name k))
(clojure->mongo k))
(clojure->mongo v)))
dbo))

Keyword
(clojure->mongo [^Keyword o] (.getName o))
(clojure->mongo [^Keyword o] (.substring (str o) 1))

List
(clojure->mongo [^List o] (map clojure->mongo o))
Expand Down