|
1 |
| -# converge/transit |
| 1 | +# converge/nippy |
2 | 2 |
|
3 |
| -transit-(clj|cljs) handlers for Converge |
| 3 | +Nippy serialization support for Converge in Clojure. |
4 | 4 |
|
5 | 5 | ## Usage
|
6 | 6 |
|
7 |
| -### Clojure + transit-clj |
8 |
| - |
9 |
| -This is the transit-clj Usage example, modified to include a |
10 |
| -ConvergentRef and the read/write handlers provided by this library. |
| 7 | +Nippy provides a global extension mechanism for supporting custom |
| 8 | +types. Simply requiring the `converge.nippy` namespace adds support |
| 9 | +for Converge types: |
11 | 10 |
|
12 | 11 | ``` clojure
|
13 |
| -(require '[cognitect.transit :as transit] |
14 |
| - '[converge.transit :as converge-transit] |
| 12 | +(require '[taoensso.nippy :as nippy] |
| 13 | + 'converge.nippy |
15 | 14 | '[converge.api :as convergent])
|
16 |
| -(import [java.io ByteArrayInputStream ByteArrayOutputStream]) |
17 |
| - |
18 |
| -;; Write data to a stream |
19 |
| -(def out (ByteArrayOutputStream. 4096)) |
20 |
| -(def writer |
21 |
| - (transit/writer out :json |
22 |
| - {:handlers (merge transit/default-write-handlers |
23 |
| - converge-transit/write-handlers)})) |
24 |
| -(def cref (convergent/ref {:foo :bar |
25 |
| - :a [1 2]})) |
26 |
| -(transit/write writer cref) |
27 |
| - |
28 |
| -cref |
29 |
| - |
30 |
| -;; Take a peek at the JSON |
31 |
| -(.toString out) |
32 |
| -;; => "[\"~#opset/ref\", <snip a bunch of operations ...>]" |
33 |
| - |
34 |
| -;; Read data from a stream |
35 |
| -(def in (ByteArrayInputStream. (.toByteArray out))) |
36 |
| -(def reader |
37 |
| - (transit/reader in :json |
38 |
| - {:handlers (merge transit/default-read-handlers |
39 |
| - converge-transit/read-handlers)})) |
40 |
| -(def read-ref (transit/read reader)) |
41 |
| -(prn read-ref) |
42 |
| -;; => #converge.opset.ref.OpsetConvergentRef[{:status :ready, :val {:a [1 2], :foo :bar}} 0x3bd98fc8] |
43 |
| -(prn @read-ref) |
44 |
| -;; => {:a [1 2], :foo :bar} |
45 |
| -``` |
46 |
| - |
47 |
| -### ClojureScript + transit-cljs |
48 |
| - |
49 |
| -This is the transit-cljs Usage example, modified to include a |
50 |
| -ConvergentRef and the read/write handlers provided by this library. |
51 |
| - |
52 |
| - |
53 | 15 |
|
54 |
| -## Development |
| 16 | +(def r (convergent/ref {:foo [:bar #{:baz}]})) |
55 | 17 |
|
56 |
| -Invoke a library API function from the command-line: |
| 18 | +(def nippy-bytes (nippy/freeze r)) |
57 | 19 |
|
58 |
| - $ clojure -X converge.transit/foo :a 1 :b '"two"' |
59 |
| - {:a 1, :b "two"} "Hello, World!" |
| 20 | +(def roundtripped (nippy/thaw nippy-bytes)) |
60 | 21 |
|
61 |
| -Run the project's tests (they'll fail until you edit them): |
| 22 | +@roundtripped |
| 23 | +;=> {:foo [:bar #{:baz}]} |
62 | 24 |
|
63 |
| - $ clojure -T:build test |
| 25 | +(def r2 (convergent/ref-from-ops (convergent/ref-log r))) |
64 | 26 |
|
65 |
| -Run the project's CI pipeline and build a JAR (this will fail until you edit the tests to pass): |
| 27 | +(swap! r assoc :baz :quux) |
66 | 28 |
|
67 |
| - $ clojure -T:build ci |
| 29 | +(def patch (convergent/patch-from-clock r (convergent/clock r2))) |
68 | 30 |
|
69 |
| -This will produce an updated `pom.xml` file with synchronized dependencies inside the `META-INF` |
70 |
| -directory inside `target/classes` and the JAR in `target`. You can update the version (and SCM tag) |
71 |
| -information in generated `pom.xml` by updating `build.clj`. |
| 31 | +(nippy/thaw (nippy/freeze patch)) |
| 32 | +;=> <the patch> |
72 | 33 |
|
73 |
| -Install it locally (requires the `ci` task be run first): |
74 |
| - |
75 |
| - $ clojure -T:build install |
76 |
| - |
77 |
| -Deploy it to Clojars -- needs `CLOJARS_USERNAME` and `CLOJARS_PASSWORD` environment |
78 |
| -variables (requires the `ci` task be run first): |
79 |
| - |
80 |
| - $ clojure -T:build deploy |
81 |
| - |
82 |
| -Your library will be deployed to net.clojars.converge/transit on clojars.org by default. |
| 34 | +(def clock (convergent/clock r1)) |
| 35 | +(nippy/thaw (nippy/freeze clock)) |
| 36 | +;=> <the clock> |
| 37 | +``` |
83 | 38 |
|
84 | 39 | ## License
|
85 | 40 |
|
|
0 commit comments