0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Add point read/write handler to transit.

This commit is contained in:
Andrey Antukh 2016-04-10 18:05:32 +03:00
parent 9bd358a119
commit e791f49921
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

View file

@ -7,34 +7,44 @@
(ns uxbox.util.transit
"A lightweight abstraction for transit serialization."
(:require [cognitect.transit :as t]
[com.cognitect.transit :as tr]
[uxbox.util.data :refer (parse-int)]
[uxbox.util.geom.point :as gpt]
[uxbox.util.datetime :as dt]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Read/Write Transit handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; --- Transit Handlers
(def ^:private datetime-write-handler
(reify
Object
(tag [_ v] "m")
(rep [_ v] (dt/format v :offset))
(stringRep [this v] (str (dt/format v :offset)))))
(def datetime-write-handler
(t/write-handler (constantly "m")
#(str (dt/format % :offset))))
(defn- datetime-read-handler
[v]
(dt/datetime (parse-int v)))
(def datetime-read-handler
(t/read-handler
#(dt/datetime (parse-int %))))
(def point-write-handler
(t/write-handler
(constantly "point")
(fn [v]
(let [ret #js []]
(.push ret (:x v))
(.push ret (:y v))
ret))))
(def point-read-handler
(t/read-handler
#(gpt/point (js->clj %))))
(def ^:privare +read-handlers+
{"u" uuid
"m" datetime-read-handler})
"m" datetime-read-handler
"point" point-read-handler})
(def ^:privare +write-handlers+
{dt/DateTime datetime-write-handler})
{dt/DateTime datetime-write-handler
gpt/Point point-write-handler})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; --- Public Api
(defn decode
[data]