-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from edma2/develop
Version 1.1.0
- Loading branch information
Showing
7 changed files
with
240 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(ns msgpack.clojure-extensions | ||
"Extended types for Clojure-specific types" | ||
(:require [msgpack.core :as msg] | ||
[msgpack.macros :refer [extend-msgpack]])) | ||
|
||
(defn- keyword->str | ||
"Convert keyword to string with namespace preserved. | ||
Example: :A/A => \"A/A\"" | ||
[k] | ||
(subs (str k) 1)) | ||
|
||
(extend-msgpack | ||
clojure.lang.Keyword | ||
3 | ||
[k] (msg/pack (keyword->str k)) | ||
[bytes] (keyword (msg/unpack bytes))) | ||
|
||
(extend-msgpack | ||
clojure.lang.Symbol | ||
4 | ||
[s] (msg/pack (str s)) | ||
[bytes] (symbol (msg/unpack bytes))) | ||
|
||
(extend-msgpack | ||
java.lang.Character | ||
5 | ||
[c] (msg/pack (str c)) | ||
[bytes] (first (char-array (msg/unpack bytes)))) | ||
|
||
(extend-msgpack | ||
clojure.lang.Ratio | ||
6 | ||
[r] (msg/pack [(numerator r) (denominator r)]) | ||
[bytes] (let [[n d] (msg/unpack bytes)] | ||
(/ n d))) | ||
|
||
(extend-msgpack | ||
clojure.lang.IPersistentSet | ||
7 | ||
[s] (msg/pack (seq s)) | ||
[bytes] (set (msg/unpack bytes))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
(ns msgpack.macros | ||
"Macros for extending MessagePack with Extended types. | ||
See msgpack.clojure-extensions for examples." | ||
(:require [msgpack.core :refer :all])) | ||
|
||
(defmacro defext | ||
"Treat an existing class as a MessagePack extended type. | ||
As a side-effect, the class will extend the Packable protocol. | ||
Provide a type (non-negative signed integer) and a function that | ||
converts the class to a sequence of bytes. | ||
(defrecord Employee [name]) | ||
(defext Employee 1 #(.getBytes (:name %))) | ||
expands into: | ||
(extend-protocol Packable | ||
Employee | ||
(pack-stream [obj stream] | ||
(pack-stream (->Extension 1 (.getBytes (:name obj))) stream))) | ||
and this will work: | ||
(pack (Employee. name))" | ||
[class type f] | ||
(defmacro extend-msgpack | ||
[class type pack-args pack unpack-args unpack] | ||
`(let [type# ~type] | ||
(assert (<= 0 type# 127) | ||
"[-1, -128]: reserved for future pre-defined extensions.") | ||
(extend-protocol Packable | ||
~class | ||
(pack-stream [obj# stream#] | ||
(pack-stream (->Extension type# (~f obj#)) stream#))))) | ||
(do | ||
(extend-protocol Packable ~class | ||
(pack-stream [~@pack-args ^java.io.DataOutput s#] | ||
(pack-stream (->Ext type# ~pack) s#))) | ||
(defmethod refine-ext type# [ext#] | ||
(let [~@unpack-args (:data ext#)] | ||
~unpack))))) |
Oops, something went wrong.