-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
47 lines (41 loc) · 1.29 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns datomic-type-extensions.core
(:require [clojure.walk :refer [postwalk]]
[datomic.api :as d]
[datomic-type-extensions.types :as types]))
(defn serialize-assertion-tx [form attr-types]
(if-let [[op e a v] (and (vector? form) form)]
(if-let [type (and (#{:db/add :db/retract} op)
(get attr-types a))]
(update form 3 #(types/serialize type %))
form)
form))
(defn apply-to-value [f val]
(cond
(set? val) (set (map f val))
(list? val) (map f val)
(vector? val) (mapv f val)
:else (f val)))
(defn- update-attr [f form [k type]]
(if (get form k)
(update form k #(apply-to-value (partial f type) %))
form))
(defn serialize-tx-data [attr-types tx-data]
(postwalk
(fn [form]
(cond
(map? form) (reduce #(update-attr types/serialize %1 %2) form attr-types)
(vector? form) (serialize-assertion-tx form attr-types)
:else form))
tx-data))
(defn deserialize [attr-types form]
(postwalk
(fn [form]
(if (map? form)
(reduce #(update-attr types/deserialize %1 %2) form attr-types)
form))
form))
(defn serialize-lookup-ref [attr-types eid]
(if-let [type (and (vector? eid)
(attr-types (first eid)))]
(update eid 1 #(types/serialize type %))
eid))