Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Transit Write and Read Handler. #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
:url "https://github.com/replikativ/incognito"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0" :scope "provided"]
[org.clojure/clojurescript "1.9.946" :scope "provided"]
:dependencies [[org.clojure/clojure "1.10.0" :scope "provided"]
[org.clojure/clojurescript "1.10.439" :scope "provided"]
[org.clojure/data.fressian "0.2.1" :scope "provided"]
[com.cognitect/transit-clj "0.8.285" :scope "provided"]
[com.cognitect/transit-cljs "0.8.239" :scope "provided"]
[fress "0.3.1" :scope "provided"]]
[com.cognitect/transit-clj "0.8.313" :scope "provided"]
[com.cognitect/transit-cljs "0.8.256":scope "provided"]
[fress "0.3.1":scope "provided" ]]

:plugins [[lein-cljsbuild "1.1.7"]]

Expand Down
12 changes: 1 addition & 11 deletions src/incognito/fressian.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,8 @@
{cljs.core/List plist-writer
cljs.core/EmptyList plist-writer
cljs.core/PersistentTreeMap write-tree-map
cljs.core/MapEntry plist-writer
"record" (record-writer write-handlers)
cljs.core/LazySeq plist-writer
cljs.core/PersistentVector pvec-writer})

(comment

(do
(defrecord SomeRecord [f1 f2])
(def rec (SomeRecord. [1 2 2] {:c "213"}))
(def buf (fress.api/byte-stream))
(def writer (fress.api/create-writer buf :handlers (incognito-write-handlers (atom {'incognito.fressian.SomeRecord (fn [foo] (println "foos") (assoc foo :c "donkey"))}))))
(fress.api/write-object writer rec)
(fress.api/read buf :handlers (incognito-read-handlers (atom {}))))

)
70 changes: 24 additions & 46 deletions src/incognito/transit.clj
Original file line number Diff line number Diff line change
@@ -1,52 +1,30 @@
(ns incognito.transit
(:require [cognitect.transit :as transit]
[incognito.base :refer [incognito-reader incognito-writer]])
(:import [com.cognitect.transit.impl WriteHandlers$MapWriteHandler]))
(:import [com.cognitect.transit.impl WriteHandlers$MapWriteHandler]
[com.cognitect.transit WriteHandler]))

(defn incognito-write-handler [write-handlers]
(proxy [WriteHandlers$MapWriteHandler] []
(tag [o]
(if (isa? (type o) clojure.lang.IRecord)
"incognito"
(proxy-super tag o)))
(rep [o]
(if (isa? (type o) clojure.lang.IRecord)
(if (isa? (type o) incognito.base.IncognitoTaggedLiteral)
(into {} o) ;; carry on as map
(incognito-writer @write-handlers o))
(proxy-super rep o)))))
(defrecord WriteRecords [tag value])

(defn incognito-read-handler [read-handlers]
(transit/read-handler
(partial incognito-reader @read-handlers)))


(comment
(import '[java.io ByteArrayInputStream ByteArrayOutputStream]
'[com.cognitect.transit.impl WriteHandlers$MapWriteHandler])

(defrecord Foo [a b])

(import '[com.cognitect.transit.impl WriteHandlerMap])

(import '[com.cognitect.transit TransitFactory])

(get (WriteHandlerMap/defaults) java.util.Map)

(keys (WriteHandlerMap/defaults))

(get (TransitFactory/defaultWriteHandlers) java.util.Map)

(map pr-str (keys (TransitFactory/defaultWriteHandlers)))

(isa? (type (Foo. 1 2)) java.util.Map)


(with-open [baos (ByteArrayOutputStream.)]
(let [writer (transit/writer baos :json
{:handlers {clojure.lang.PersistentArrayMap (incognito-write-handler
(atom {'incognito.transit.Foo (fn [foo] (println "foo") (assoc foo :c "banana"))}))}})]
(transit/write writer (map->Foo {:a [1 2 3] :b {:c "Fooos"}}))
(let [bais (ByteArrayInputStream. (.toByteArray baos))
reader (transit/reader bais :json {:handlers {"incognito" (incognito-read-handler (atom {'incognito.transit.Foo map->Foo}))}})]
(transit/read reader)))))
{:handlers {"incognito"
(transit/read-handler
(partial incognito-reader @read-handlers))}})

(defn incognito-write-helper
"For :transform. Will write any metadata present on the value."
[write-handlers o]
(if (record? o)
(if (isa? (type o) incognito.base.IncognitoTaggedLiteral)
(into {} o)
(let [{:keys [tag value]} (incognito-writer @write-handlers o)]
(WriteRecords. tag value)))
o))

(defn incognito-write-handler
[write-handlers]
{:transform (partial incognito-write-helper write-handlers)
:handlers {WriteRecords
(proxy [WriteHandlers$MapWriteHandler] []
(tag [_] "incognito")
(rep [o] (into {} o)))}})
33 changes: 13 additions & 20 deletions test/incognito/transit_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,38 @@
(deftest incognito-roundtrip-test
(testing "Test incognito transport."
(let [bar (map->Bar {:a [1 2 3] :b {:c "Fooos"}})]
(is (= #incognito.base.IncognitoTaggedLiteral{:tag incognito.transit_test.Bar,
(is (= #incognito.base.IncognitoTaggedLiteral{:tag incognito.transit_test.Bar,
:value {:a [1 2 3],
:b {:c "Fooos"}
:c "banana"}}
(with-open [baos (ByteArrayOutputStream.)]
(let [writer (transit/writer baos :json
{:handlers {java.util.Map
(incognito-write-handler
(atom {'incognito.transit_test.Bar
(fn [foo] (assoc foo :c "banana"))}))}})]
(incognito-write-handler (atom {'incognito.transit_test.Bar
(fn [foo] (assoc foo :c "banana"))})))]
(transit/write writer bar)
(let [bais (ByteArrayInputStream. (.toByteArray baos))
(let [bais (ByteArrayInputStream. (.toByteArray baos))
reader (transit/reader bais :json
{:handlers {"incognito"
(incognito-read-handler (atom {}))}})]
(incognito-read-handler (atom {})))]
(transit/read reader)))))))))



(deftest double-roundtrip-test
(testing "Test two roundtrips, one incognito and deserialize at end."
(let [bar (map->Bar {:a [1 2 3] :b {:c "Fooos"}})]
(is (= bar
(with-open [baos (ByteArrayOutputStream.)]
(let [writer (transit/writer baos :json
{:handlers {java.util.Map
(incognito-write-handler
(atom {}))}})]
(incognito-write-handler (atom {})))]
(transit/write writer bar)
(let [bais (ByteArrayInputStream. (.toByteArray baos))
(let [bais (ByteArrayInputStream. (.toByteArray baos))
reader (transit/reader bais :json
{:handlers {"incognito"
(incognito-read-handler (atom {'incognito.transit_test.Bar map->Bar}))}})]
(incognito-read-handler (atom {'incognito.transit_test.Bar map->Bar})))]
(with-open [baos (ByteArrayOutputStream.)]
(let [writer (transit/writer baos :json
{:handlers {java.util.Map
(incognito-write-handler
(atom {}))}})]
(incognito-write-handler (atom {})))]
(transit/write writer (transit/read reader))
(let [bais (ByteArrayInputStream. (.toByteArray baos))
(let [bais (ByteArrayInputStream. (.toByteArray baos))
reader (transit/reader bais :json
{:handlers {"incognito"
(incognito-read-handler (atom {'incognito.transit_test.Bar map->Bar}))}})]
(incognito-read-handler (atom {'incognito.transit_test.Bar map->Bar})))]
(transit/read reader))))))))))))