-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
misc.clj
47 lines (41 loc) · 1.79 KB
/
misc.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
45
46
47
(ns nrepl.misc
"Misc utilities used in nREPL's implementation (potentially also
useful for anyone extending it)."
{:author "Chas Emerick"}
(:require [clojure.tools.logging :as logging]))
(defn log
[& args]
(logging/error args))
(defmacro returning
"Executes `body`, returning `x`."
[x & body]
`(let [x# ~x] ~@body x#))
(defn uuid
"Returns a new UUID string."
[]
(str (java.util.UUID/randomUUID)))
(defn response-for
"Returns a map containing the :session and :id from the \"request\" `msg`
as well as all entries specified in `response-data`, which can be one
or more maps (which will be merged), *or* key-value pairs.
(response-for msg :status :done :value \"5\")
(response-for msg {:status :interrupted})
The :session value in `msg` may be any Clojure reference type (to accommodate
likely implementations of sessions) that has an :id slot in its metadata,
or a string."
[{:keys [session id]} & response-data]
{:pre [(seq response-data)]}
(let [{:keys [status] :as response} (if (map? (first response-data))
(reduce merge response-data)
(apply hash-map response-data))
response (if (not status)
response
(assoc response :status (if (coll? status)
status
#{status})))
basis (merge (when id {:id id})
;; AReference should make this suitable for any session implementation?
(when session {:session (if (instance? clojure.lang.AReference session)
(-> session meta :id)
session)}))]
(merge basis response)))