mirror of
https://github.com/penpot/penpot.git
synced 2025-01-26 00:19:07 -05:00
✨ Minor reorganization of transit handlers (backend).
This commit is contained in:
parent
48a094d22d
commit
34c4f23e49
2 changed files with 40 additions and 41 deletions
|
@ -5,16 +5,14 @@
|
||||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
;; defined by the Mozilla Public License, v. 2.0.
|
;; 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
|
(ns app.util.time
|
||||||
(:require
|
(:require
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s])
|
||||||
[cognitect.transit :as t])
|
|
||||||
(:import
|
(:import
|
||||||
java.time.Instant
|
java.time.Instant
|
||||||
java.time.OffsetDateTime
|
|
||||||
java.time.Duration
|
java.time.Duration
|
||||||
java.util.Date
|
java.util.Date
|
||||||
java.time.temporal.TemporalAmount
|
java.time.temporal.TemporalAmount
|
||||||
|
@ -251,30 +249,6 @@
|
||||||
;; Serialization
|
;; 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
|
(defmethod print-method Instant
|
||||||
[mv ^java.io.Writer writer]
|
[mv ^java.io.Writer writer]
|
||||||
(.write writer (str "#app/instant \"" (.toString ^Instant mv) "\"")))
|
(.write writer (str "#app/instant \"" (.toString ^Instant mv) "\"")))
|
||||||
|
|
|
@ -11,16 +11,17 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.geom.matrix :as gmt]
|
[app.common.geom.matrix :as gmt]
|
||||||
[app.common.geom.point :as gpt]
|
[app.common.geom.point :as gpt]
|
||||||
[app.util.time :as dt]
|
|
||||||
[cognitect.transit :as t]
|
[cognitect.transit :as t]
|
||||||
[linked.core :as lk])
|
[linked.core :as lk])
|
||||||
(:import
|
(:import
|
||||||
linked.set.LinkedSet
|
app.common.geom.matrix.Matrix
|
||||||
|
app.common.geom.point.Point
|
||||||
java.io.ByteArrayInputStream
|
java.io.ByteArrayInputStream
|
||||||
java.io.ByteArrayOutputStream
|
java.io.ByteArrayOutputStream
|
||||||
java.io.File
|
java.io.File
|
||||||
app.common.geom.point.Point
|
java.time.Instant
|
||||||
app.common.geom.matrix.Matrix))
|
java.time.OffsetDateTime
|
||||||
|
linked.set.LinkedSet))
|
||||||
|
|
||||||
;; --- Handlers
|
;; --- Handlers
|
||||||
|
|
||||||
|
@ -29,6 +30,8 @@
|
||||||
(constantly "file")
|
(constantly "file")
|
||||||
(fn [v] (str v))))
|
(fn [v] (str v))))
|
||||||
|
|
||||||
|
;; --- GEOM
|
||||||
|
|
||||||
(def point-write-handler
|
(def point-write-handler
|
||||||
(t/write-handler
|
(t/write-handler
|
||||||
(constantly "point")
|
(constantly "point")
|
||||||
|
@ -45,6 +48,8 @@
|
||||||
(def matrix-read-handler
|
(def matrix-read-handler
|
||||||
(t/read-handler gmt/map->Matrix))
|
(t/read-handler gmt/map->Matrix))
|
||||||
|
|
||||||
|
;; --- Ordered Set
|
||||||
|
|
||||||
(def ordered-set-write-handler
|
(def ordered-set-write-handler
|
||||||
(t/write-handler
|
(t/write-handler
|
||||||
(constantly "ordered-set")
|
(constantly "ordered-set")
|
||||||
|
@ -53,18 +58,38 @@
|
||||||
(def ordered-set-read-handler
|
(def ordered-set-read-handler
|
||||||
(t/read-handler #(into (lk/set) %)))
|
(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+
|
(def +read-handlers+
|
||||||
(assoc dt/+read-handlers+
|
{"matrix" matrix-read-handler
|
||||||
"matrix" matrix-read-handler
|
"ordered-set" ordered-set-read-handler
|
||||||
"ordered-set" ordered-set-read-handler
|
"point" point-read-handler
|
||||||
"point" point-read-handler))
|
"m" instant-read-handler
|
||||||
|
"instant" instant-read-handler})
|
||||||
|
|
||||||
(def +write-handlers+
|
(def +write-handlers+
|
||||||
(assoc dt/+write-handlers+
|
{File file-write-handler
|
||||||
File file-write-handler
|
LinkedSet ordered-set-write-handler
|
||||||
LinkedSet ordered-set-write-handler
|
Matrix matrix-write-handler
|
||||||
Matrix matrix-write-handler
|
Point point-write-handler
|
||||||
Point point-write-handler))
|
Instant instant-write-handler
|
||||||
|
OffsetDateTime offset-datetime-write-handler})
|
||||||
|
|
||||||
;; --- Low-Level Api
|
;; --- Low-Level Api
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue