-
Notifications
You must be signed in to change notification settings - Fork 175
/
util.clj
44 lines (34 loc) · 1.14 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns cider.nrepl.middleware.util
"Utility functions that might be useful in middleware.")
(defmulti transform-value "Transform a value for output" type)
(defmethod transform-value :default [v] (str v))
(defmethod transform-value Number
[v]
;; bencode supports only integers, so we convert
;; other numbers (e.g. floats) to string
(if (integer? v)
v
(str v)))
(defmethod transform-value nil [_v] nil)
(defmethod transform-value java.io.File
[v]
(.getAbsolutePath ^java.io.File v))
(defmethod transform-value clojure.lang.Sequential
[v]
(list* (map transform-value v)))
(defmethod transform-value clojure.lang.Symbol
[v]
(let [[the-ns the-name] [(namespace v) (name v)]]
(if the-ns
(str the-ns "/" the-name)
the-name)))
(defmethod transform-value clojure.lang.Keyword
[v]
(transform-value (.sym ^clojure.lang.Keyword v)))
(defmethod transform-value clojure.lang.Associative
[m]
(->> (for [[k v] m] ; bencode keys must be strings
[(str (transform-value k)) (transform-value v)])
(into {})))
;; handles vectors
(prefer-method transform-value clojure.lang.Sequential clojure.lang.Associative)