Skip to content
Browse files

Moved ->str to a protocol

  • Loading branch information...
1 parent a7d4f2f commit 6508b13b0bd3777bab385ddf65acc1cfb4868686 @wallrat committed May 29, 2012
Showing with 10 additions and 9 deletions.
  1. +10 −9 src/labs/redis/core.clj
19 src/labs/redis/core.clj
@@ -6,7 +6,7 @@
[clojure.pprint :only (pprint)]
[ :only (resource)])
(:require [ :as json])
- (:import [labs.redis Client ClientPool Reply BulkReply MultiBulkReply]))
+ (:import [labs.redis Client ClientPool Reply BulkReply StatusReply MultiBulkReply LinkedReplyFuture]))
(set! *warn-on-reflection* true)
@@ -60,15 +60,16 @@
(defn value [^Reply reply]
(.getValue reply))
+(defprotocol StringCoerce
+ (->str [reply] "Coerces a reply into a String."))
-(defn ->str
- "Coerces reply into a String."
- [reply]
- (when reply
- (condp instance? reply
- byte-array-class (String. ^bytes reply)
- BulkReply (let [bs (.bytes ^BulkReply reply)] (when bs (String. bs)))
- java.lang.Object (.toString ^java.lang.Object reply))))
+(extend-protocol StringCoerce
+ (Class/forName "[B") (->str [reply] (String. ^bytes reply))
+ BulkReply (->str [reply] (let [bs (.bytes reply)] (when bs (String. bs))))
+ StatusReply (->str [reply] (->str @reply))
+ LinkedReplyFuture (->str [reply] (->str @reply))
+ Object (->str [reply] (.toString reply))
+ nil (->str [reply] nil))
(defn ->strs [reply]
(when reply

0 comments on commit 6508b13

Please sign in to comment.
Something went wrong with that request. Please try again.