0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 23:49:45 -05:00

Minor reorganization of transit handlers (backend).

This commit is contained in:
Andrey Antukh 2021-03-05 15:21:58 +01:00
parent 48a094d22d
commit 34c4f23e49
2 changed files with 40 additions and 41 deletions

View file

@ -5,16 +5,14 @@
;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2020 UXBOX Labs SL
;; Copyright (c) 2020-2021 UXBOX Labs SL
(ns app.util.time
(:require
[app.common.exceptions :as ex]
[clojure.spec.alpha :as s]
[cognitect.transit :as t])
[clojure.spec.alpha :as s])
(:import
java.time.Instant
java.time.OffsetDateTime
java.time.Duration
java.util.Date
java.time.temporal.TemporalAmount
@ -251,30 +249,6 @@
;; Serialization
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(declare from-string)
(def ^:private instant-write-handler
(t/write-handler
(constantly "m")
(fn [v] (str (.toEpochMilli ^Instant v)))))
(def ^:private offset-datetime-write-handler
(t/write-handler
(constantly "m")
(fn [v] (str (.toEpochMilli (.toInstant ^OffsetDateTime v))))))
(def ^:private read-handler
(t/read-handler
(fn [v] (-> (Long/parseLong v)
(Instant/ofEpochMilli)))))
(def +read-handlers+
{"m" read-handler})
(def +write-handlers+
{Instant instant-write-handler
OffsetDateTime offset-datetime-write-handler})
(defmethod print-method Instant
[mv ^java.io.Writer writer]
(.write writer (str "#app/instant \"" (.toString ^Instant mv) "\"")))

View file

@ -11,16 +11,17 @@
(:require
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.util.time :as dt]
[cognitect.transit :as t]
[linked.core :as lk])
(:import
linked.set.LinkedSet
app.common.geom.matrix.Matrix
app.common.geom.point.Point
java.io.ByteArrayInputStream
java.io.ByteArrayOutputStream
java.io.File
app.common.geom.point.Point
app.common.geom.matrix.Matrix))
java.time.Instant
java.time.OffsetDateTime
linked.set.LinkedSet))
;; --- Handlers
@ -29,6 +30,8 @@
(constantly "file")
(fn [v] (str v))))
;; --- GEOM
(def point-write-handler
(t/write-handler
(constantly "point")
@ -45,6 +48,8 @@
(def matrix-read-handler
(t/read-handler gmt/map->Matrix))
;; --- Ordered Set
(def ordered-set-write-handler
(t/write-handler
(constantly "ordered-set")
@ -53,18 +58,38 @@
(def ordered-set-read-handler
(t/read-handler #(into (lk/set) %)))
;; --- TIME
(def ^:private instant-read-handler
(t/read-handler
(fn [v] (-> (Long/parseLong v)
(Instant/ofEpochMilli)))))
(def ^:private instant-write-handler
(t/write-handler
(constantly "m")
(fn [v] (str (.toEpochMilli ^Instant v)))))
(def ^:private offset-datetime-write-handler
(t/write-handler
(constantly "m")
(fn [v] (str (.toEpochMilli (.toInstant ^OffsetDateTime v))))))
(def +read-handlers+
(assoc dt/+read-handlers+
"matrix" matrix-read-handler
"ordered-set" ordered-set-read-handler
"point" point-read-handler))
{"matrix" matrix-read-handler
"ordered-set" ordered-set-read-handler
"point" point-read-handler
"m" instant-read-handler
"instant" instant-read-handler})
(def +write-handlers+
(assoc dt/+write-handlers+
File file-write-handler
LinkedSet ordered-set-write-handler
Matrix matrix-write-handler
Point point-write-handler))
{File file-write-handler
LinkedSet ordered-set-write-handler
Matrix matrix-write-handler
Point point-write-handler
Instant instant-write-handler
OffsetDateTime offset-datetime-write-handler})
;; --- Low-Level Api