-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransit.repl
43 lines (33 loc) · 1.13 KB
/
transit.repl
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
;; An example of how to use Rad with Transit.
(import '(java.io ByteArrayInputStream ByteArrayOutputStream))
(require '[clojure.spec.alpha :as spec])
(require '[clojure.repl.deps :as deps])
(spec/check-asserts true)
;; Presuming Clojure 1.12 or newer.
(deps/add-lib 'com.cognitect/transit-clj)
(require '[cognitect.transit :as transit])
;; Decode a byte array of Transit data.
(defn decode
[ba]
(with-open [in (ByteArrayInputStream. ba)]
(let [reader (transit/reader in :msgpack)]
(transit/read reader))))
;; Encode a value as Transit and write it into a byte array.
(defn encode
[x]
(with-open [out (ByteArrayOutputStream.)]
(let [writer (transit/writer out :msgpack)]
(transit/write writer x)
(.toByteArray out))))
;; Try round-tripping to make sure everything works OK.
(-> {:a 1} encode decode)
(assert (= *1 {:a 1}))
;; Make a Redis client.
(require '[rad.api :as rad])
(def redis (rad/client))
;; Set "a" to a Transit-encoded value.
@(redis [:SET "a" (encode {:b 2})])
;; Decode the Transit-encoded value in "a".
(decode @(redis [:GET "a"]))
;; Check that it's what we expected.
(assert (= *1 {:b 2}))