Skip to content

Commit 056c81b

Browse files
jrgvfptaoussanis
authored andcommitted
[new] [#474] Add UUID support to Msgpack packer (@jrgvf)
1 parent 0b9b3b9 commit 056c81b

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/taoensso/msgpack/impl.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@
321321
(unpack [ba]
322322
(CachedKey. (get @c/*key-cache_* (bit-and 0xff (aget ^bytes ba 0))))))
323323

324+
(c/extend-packable 9 java.util.UUID
325+
(pack [u] (.getBytes (str u) StandardCharsets/UTF_8))
326+
(unpack [ba] (java.util.UUID/fromString (String. ^bytes ba StandardCharsets/UTF_8))))
327+
328+
#_ ; Unfortunately no simple equivalent for Cljs
329+
(c/extend-packable 9 java.util.UUID
330+
(pack [u]
331+
(with-out [out 64]
332+
(.writeLong out (.getMostSignificantBits u))
333+
(.writeLong out (.getLeastSignificantBits u))))
334+
335+
(unpack [ba] (with-in [in ba] (java.util.UUID. (.readLong in) (.readLong in)))))
336+
337+
(comment (unpack (pack (java.util.UUID/randomUUID))))
338+
324339
;;;;
325340

326341
(defn pack

src/taoensso/msgpack/impl.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@
452452
(unpack [u8s]
453453
(CachedKey. (get @c/*key-cache_* (aget ^js u8s 0)))))
454454

455+
(c/extend-packable 9 UUID
456+
(pack [u] (.encode text-encoder (str u)))
457+
(unpack [^js u8s] (uuid (.decode text-decoder ^js u8s))))
458+
455459
;;;;
456460

457461
(defn unpack [in] (unpack-1 (in-stream* in)))

test/taoensso/msgpack_tests.cljc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
#?(:clj (is (rt? "Timestamps" (java.time.Instant/now) (java.util.Date.) (java.util.Date. -1))))
119119
#?(:cljs (is (rt? "Timestamps" (js/Date.))))
120120

121+
(rt? "UUID" #uuid "66d96ab4-9834-40db-a3cd-42b361503ab9")
122+
121123
(testing "Key caching"
122124
[(rt? "Basic" [{:a :A} {:a :A}] {:foo {1 {:x :X} 2 {:y :Y}}})
123125
(rt? ">256 keys" (let [m (into {} (map (fn [n] [(str "key" n) n])) (range 512))] [m m {:a :A :b :B} m]))

0 commit comments

Comments
 (0)