0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -05:00

Merge pull request #5050 from penpot/hiru-ordered-maps

🔧 Add serializable ordered collections
This commit is contained in:
Andrey Antukh 2024-09-04 12:26:21 +02:00 committed by GitHub
commit 873c9b1903
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 4 deletions

View file

@ -6,6 +6,8 @@
(ns user (ns user
(:require (:require
[app.common.data :as d]
[app.common.fressian :as fres]
[app.common.json :as json] [app.common.json :as json]
[app.common.pprint :as pp] [app.common.pprint :as pp]
[app.common.schema :as sm] [app.common.schema :as sm]

View file

@ -44,8 +44,8 @@
(defn ordered-map (defn ordered-map
([] lkm/empty-linked-map) ([] lkm/empty-linked-map)
([a] (conj lkm/empty-linked-map a)) ([k a] (assoc lkm/empty-linked-map k a))
([a & xs] (apply conj lkm/empty-linked-map a xs))) ([k a & xs] (apply assoc lkm/empty-linked-map k a xs)))
(defn ordered-set? (defn ordered-set?
[o] [o]
@ -564,6 +564,41 @@
new-elems new-elems
(remove p? after)))) (remove p? after))))
(defn addm-at-index
"Insert an element in an ordered map at an arbitrary index"
[coll index key element]
(assert (ordered-map? coll))
(-> (ordered-map)
(into (take index coll))
(assoc key element)
(into (drop index coll))))
(defn insertm-at-index
"Insert a map {k v} of elements in an ordered map at an arbitrary index"
[coll index new-elems]
(assert (ordered-map? coll))
(-> (ordered-map)
(into (take index coll))
(into new-elems)
(into (drop index coll))))
(defn adds-at-index
"Insert an element in an ordered set at an arbitrary index"
[coll index element]
(assert (ordered-set? coll))
(-> (ordered-set)
(into (take index coll))
(conj element)
(into (drop index coll))))
(defn inserts-at-index
"Insert a list of elements in an ordered set at an arbitrary index"
[coll index new-elems]
(assert (ordered-set? coll))
(-> (ordered-set)
(into (take index coll))
(into new-elems)
(into (drop index coll))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Parsing / Conversion ;; Data Parsing / Conversion

View file

@ -15,6 +15,7 @@
java.time.Instant java.time.Instant
java.time.OffsetDateTime java.time.OffsetDateTime
java.util.List java.util.List
linked.map.LinkedMap
org.fressian.Reader org.fressian.Reader
org.fressian.StreamingWriter org.fressian.StreamingWriter
org.fressian.Writer org.fressian.Writer
@ -109,6 +110,13 @@
(clojure.lang.PersistentArrayMap. (.toArray kvs)) (clojure.lang.PersistentArrayMap. (.toArray kvs))
(clojure.lang.PersistentHashMap/create (seq kvs))))) (clojure.lang.PersistentHashMap/create (seq kvs)))))
(defn read-ordered-map
[^Reader rdr]
(let [kvs ^java.util.List (read-object! rdr)]
(reduce #(assoc %1 (first %2) (second %2))
(d/ordered-map)
(partition-all 2 (seq kvs)))))
(def ^:dynamic *write-handler-lookup* nil) (def ^:dynamic *write-handler-lookup* nil)
(def ^:dynamic *read-handler-lookup* nil) (def ^:dynamic *read-handler-lookup* nil)
@ -225,6 +233,11 @@
:wfn write-map-like :wfn write-map-like
:rfn read-map-like} :rfn read-map-like}
{:name "linked/map"
:class LinkedMap
:wfn write-map-like
:rfn read-ordered-map}
{:name "clj/keyword" {:name "clj/keyword"
:class clojure.lang.Keyword :class clojure.lang.Keyword
:wfn write-named :wfn write-named

View file

@ -12,7 +12,7 @@
[app.common.uri :as uri] [app.common.uri :as uri]
[cognitect.transit :as t] [cognitect.transit :as t]
[lambdaisland.uri :as luri] [lambdaisland.uri :as luri]
[linked.core :as lk] [linked.map :as lkm]
[linked.set :as lks]) [linked.set :as lks])
#?(:clj #?(:clj
(:import (:import
@ -24,6 +24,7 @@
java.time.Instant java.time.Instant
java.time.OffsetDateTime java.time.OffsetDateTime
lambdaisland.uri.URI lambdaisland.uri.URI
linked.map.LinkedMap
linked.set.LinkedSet))) linked.set.LinkedSet)))
(def write-handlers (atom nil)) (def write-handlers (atom nil))
@ -118,10 +119,15 @@
{:id "u" {:id "u"
:rfn parse-uuid}) :rfn parse-uuid})
{:id "ordered-map"
:class #?(:clj LinkedMap :cljs lkm/LinkedMap)
:wfn vec
:rfn #(into lkm/empty-linked-map %)}
{:id "ordered-set" {:id "ordered-set"
:class #?(:clj LinkedSet :cljs lks/LinkedSet) :class #?(:clj LinkedSet :cljs lks/LinkedSet)
:wfn vec :wfn vec
:rfn #(into (lk/set) %)} :rfn #(into lks/empty-linked-set %)}
{:id "duration" {:id "duration"
:class #?(:clj Duration :cljs lxn/Duration) :class #?(:clj Duration :cljs lxn/Duration)