diff --git a/common/dev/user.clj b/common/dev/user.clj index 0e7787ae7..cb907fade 100644 --- a/common/dev/user.clj +++ b/common/dev/user.clj @@ -6,6 +6,8 @@ (ns user (:require + [app.common.data :as d] + [app.common.fressian :as fres] [app.common.json :as json] [app.common.pprint :as pp] [app.common.schema :as sm] diff --git a/common/src/app/common/data.cljc b/common/src/app/common/data.cljc index ecf55b115..77e9af51b 100644 --- a/common/src/app/common/data.cljc +++ b/common/src/app/common/data.cljc @@ -44,8 +44,8 @@ (defn ordered-map ([] lkm/empty-linked-map) - ([a] (conj lkm/empty-linked-map a)) - ([a & xs] (apply conj lkm/empty-linked-map a xs))) + ([k a] (assoc lkm/empty-linked-map k a)) + ([k a & xs] (apply assoc lkm/empty-linked-map k a xs))) (defn ordered-set? [o] @@ -564,6 +564,41 @@ new-elems (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 diff --git a/common/src/app/common/fressian.clj b/common/src/app/common/fressian.clj index 282620698..4a640cd8c 100644 --- a/common/src/app/common/fressian.clj +++ b/common/src/app/common/fressian.clj @@ -15,6 +15,7 @@ java.time.Instant java.time.OffsetDateTime java.util.List + linked.map.LinkedMap org.fressian.Reader org.fressian.StreamingWriter org.fressian.Writer @@ -109,6 +110,13 @@ (clojure.lang.PersistentArrayMap. (.toArray 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 *read-handler-lookup* nil) @@ -225,6 +233,11 @@ :wfn write-map-like :rfn read-map-like} + {:name "linked/map" + :class LinkedMap + :wfn write-map-like + :rfn read-ordered-map} + {:name "clj/keyword" :class clojure.lang.Keyword :wfn write-named diff --git a/common/src/app/common/transit.cljc b/common/src/app/common/transit.cljc index 93ab8d4b2..21673bdb4 100644 --- a/common/src/app/common/transit.cljc +++ b/common/src/app/common/transit.cljc @@ -12,7 +12,7 @@ [app.common.uri :as uri] [cognitect.transit :as t] [lambdaisland.uri :as luri] - [linked.core :as lk] + [linked.map :as lkm] [linked.set :as lks]) #?(:clj (:import @@ -24,6 +24,7 @@ java.time.Instant java.time.OffsetDateTime lambdaisland.uri.URI + linked.map.LinkedMap linked.set.LinkedSet))) (def write-handlers (atom nil)) @@ -118,10 +119,15 @@ {:id "u" :rfn parse-uuid}) + {:id "ordered-map" + :class #?(:clj LinkedMap :cljs lkm/LinkedMap) + :wfn vec + :rfn #(into lkm/empty-linked-map %)} + {:id "ordered-set" :class #?(:clj LinkedSet :cljs lks/LinkedSet) :wfn vec - :rfn #(into (lk/set) %)} + :rfn #(into lks/empty-linked-set %)} {:id "duration" :class #?(:clj Duration :cljs lxn/Duration)