Switch to dynamic custom-readers
This commit is contained in:
parent
a005a9d7fa
commit
82294f54f9
1 changed files with 12 additions and 8 deletions
|
|
@ -466,9 +466,9 @@
|
||||||
|
|
||||||
(def ^:private class-method-sig (into-array Class [IPersistentMap]))
|
(def ^:private class-method-sig (into-array Class [IPersistentMap]))
|
||||||
|
|
||||||
(declare ^:private custom-readers)
|
(declare ^:dynamic *custom-readers*)
|
||||||
(defn- read-custom! [type-id in]
|
(defn- read-custom! [type-id in]
|
||||||
(if-let [custom-reader (get @custom-readers type-id)]
|
(if-let [custom-reader (get *custom-readers* type-id)]
|
||||||
(try
|
(try
|
||||||
(custom-reader in)
|
(custom-reader in)
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
|
|
@ -756,7 +756,9 @@
|
||||||
(.writeShort ~out ~(coerce-custom-type-id custom-type-id))))
|
(.writeShort ~out ~(coerce-custom-type-id custom-type-id))))
|
||||||
~@body)))
|
~@body)))
|
||||||
|
|
||||||
(defonce custom-readers (atom {})) ; {<hash-or-byte-id> (fn [data-input]) ...}
|
(def ^:dynamic *custom-readers* "{<hash-or-byte-id> (fn [data-input])}" nil)
|
||||||
|
(defn swap-custom-readers! [f] (alter-var-root #'*custom-readers* f))
|
||||||
|
|
||||||
(defmacro extend-thaw
|
(defmacro extend-thaw
|
||||||
"Extends Nippy to support thawing of a custom type with given id:
|
"Extends Nippy to support thawing of a custom type with given id:
|
||||||
(extend-thaw :foo/my-type [data-input] ; Keyword id
|
(extend-thaw :foo/my-type [data-input] ; Keyword id
|
||||||
|
|
@ -766,13 +768,15 @@
|
||||||
(->MyType (.readUTF data-input)))"
|
(->MyType (.readUTF data-input)))"
|
||||||
[custom-type-id [in] & body]
|
[custom-type-id [in] & body]
|
||||||
(assert-custom-type-id custom-type-id)
|
(assert-custom-type-id custom-type-id)
|
||||||
(when (contains? @custom-readers (coerce-custom-type-id custom-type-id))
|
(when (contains? *custom-readers* (coerce-custom-type-id custom-type-id))
|
||||||
(println (format "Warning: resetting Nippy thaw for custom type with id: %s"
|
(println (format "Warning: resetting Nippy thaw for custom type with id: %s"
|
||||||
custom-type-id)))
|
custom-type-id)))
|
||||||
`(swap! custom-readers assoc
|
`(swap-custom-readers!
|
||||||
~(coerce-custom-type-id custom-type-id)
|
(fn [m#]
|
||||||
(fn [~(with-meta in {:tag 'java.io.DataInput})]
|
(assoc m#
|
||||||
~@body)))
|
~(coerce-custom-type-id custom-type-id)
|
||||||
|
(fn [~(with-meta in {:tag 'java.io.DataInput})]
|
||||||
|
~@body)))))
|
||||||
|
|
||||||
(comment (defrecord MyType [data])
|
(comment (defrecord MyType [data])
|
||||||
(extend-freeze MyType 1 [x out] (.writeUTF out (:data x)))
|
(extend-freeze MyType 1 [x out] (.writeUTF out (:data x)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue