-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader.cljc
203 lines (179 loc) · 7.16 KB
/
reader.cljc
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(ns ^{:doc "Reads structured data from a graph."
:author "Paula Gearon"}
zuko.entity.reader
(:require [zuko.entity.general :as general :refer [tg-ns KeyValue EntityMap GraphType]]
[zuko.node :as node]
[schema.core :as s :refer [=>]]
[clojure.string :as string]
#?(:clj [cheshire.core :as j])))
(def MapOrList (s/cond-pre EntityMap [s/Any]))
(defn get-tg-first
"Finds the tg/first property in a map, and gets the value."
[struct]
(let [first-pair? (fn [[k v :as p]]
(and (= tg-ns (namespace k))
(string/starts-with? (name k) "first")
p))]
(some first-pair? struct)))
(s/defn property-values :- [KeyValue]
"Return all the property/value pairs for a given entity in the store.
Skips non-keyword properties, as these are not created by tg.entity"
[graph :- GraphType
entity :- s/Any]
(->> (node/find-triple graph [entity '?p '?o])
(filter (comp keyword? first))))
(s/defn check-structure :- (s/maybe [KeyValue])
"Determines if a value represents a structure. If so, return the property/values for it.
Otherwise, return nil."
[graph :- GraphType
prop :- s/Any
v :- s/Any]
(if (and (not (#{:db/ident :db/id} prop)) (node/node-type? graph prop v))
(let [data (property-values graph v)]
data)))
(declare pairs->struct recurse-node)
(s/defn build-list :- [s/Any]
"Takes property/value pairs and if they represent a list node, returns the list.
else, nil."
[graph :- GraphType
seen :- #{s/Any}
pairs :- [KeyValue]]
;; convert the data to a map
(let [st (into {} pairs)]
;; if the properties indicate a list, then process it
(if-let [first-prop-elt (get-tg-first st)]
(let [remaining (:tg/rest st)
[_ first-elt] (recurse-node graph seen first-prop-elt)]
(assert first-elt)
(let [head-elt (if (= :tg/nil first-elt) nil first-elt)]
;; recursively build the list
(if remaining
(cons head-elt (build-list graph seen (property-values graph remaining)))
(list head-elt))))
(when (= :tg/list (:tg/type st)) []))))
(s/defn vbuild-list :- [s/Any]
"Calls build-list, converting to a vector as the final step"
[graph :- GraphType
seen :- #{s/Any}
pairs :- [KeyValue]]
(let [l (build-list graph seen pairs)]
(if (seq? l) (vec l) l)))
(def ^:dynamic *nested-structs* false)
(s/defn recurse-node :- s/Any
"Determines if the val of a map entry is a node to be recursed on, and loads if necessary.
If referring directly to a top level node, then short circuit and return the ID"
[graph :- GraphType
seen :- #{s/Keyword}
[prop v :as prop-val] :- KeyValue]
(if-let [pairs (check-structure graph prop v)]
(if (and (not *nested-structs*) (some #(= :tg/entity (first %)) pairs))
[prop (if-let [ident (some (fn [[k v]] (if (= :db/ident k) v)) pairs)]
{:db/ident ident}
{:db/id v})]
[prop (or (vbuild-list graph seen pairs)
(pairs->struct graph pairs (conj seen v)))])
(if (= :tg/empty-list v)
[prop []]
prop-val)))
(s/defn into-multimap
"Takes key/value tuples and inserts them into a map. If there are duplicate keys then create a set for the values."
[kvs :- [[(s/one s/Any "Key") (s/one s/Any "Value")]]]
#?(:clj
(persistent!
(reduce (fn [m [k v]]
(assoc! m k (if-let [[km vm] (find m k)]
(if (set? vm) (conj vm v) #{vm v})
v)))
(transient {}) kvs))
:cljs
(persistent!
(reduce (fn [m [k v]]
(assoc! m k (let [vm (get m k ::null)]
(if-not (= ::null vm)
(if (set? vm) (conj vm v) #{vm v})
v))))
(transient {}) kvs))))
(s/defn pairs->struct :- EntityMap
"Uses a set of property-value pairs to load up a nested data structure from the graph"
([graph :- GraphType
prop-vals :- [KeyValue]] (pairs->struct graph prop-vals #{}))
([graph :- GraphType
prop-vals :- [KeyValue]
seen :- #{s/Keyword}]
(if (some (fn [[k _]] (= :tg/first k)) prop-vals)
(vbuild-list graph seen prop-vals)
(do
(->> prop-vals
(remove (comp #{:db/id :db/ident :tg/entity} first))
(remove (comp seen second))
(map (fn [[a v :as av]] (if (= :tg/nil v) [a nil] av)))
(map (partial recurse-node graph seen))
(map (fn [[a v :as av]] (if (seq? v) [a (vec v)] av)))
into-multimap)))))
(s/defn ref->entity :- EntityMap
"Uses an id node to load up a nested data structure from the graph.
Accepts a value that identifies the internal node."
([graph :- GraphType
entity-id :- s/Any]
(ref->entity graph entity-id false nil))
([graph :- GraphType
entity-id :- s/Any
nested? :- s/Bool]
(ref->entity graph entity-id nested? nil))
([graph :- GraphType
entity-id :- s/Any
nested? :- s/Bool
exclusions :- (s/maybe #{(s/cond-pre s/Keyword s/Str)})]
(binding [*nested-structs* nested?]
(let [prop-vals (property-values graph entity-id)
pvs (if (seq exclusions)
(remove (comp exclusions first) prop-vals)
prop-vals)]
(pairs->struct graph pvs)))))
(s/defn ident->entity :- EntityMap
"Converts data in a database to a data structure suitable for JSON encoding
Accepts an internal node identifier to identify the entity object"
([graph :- GraphType
ident :- s/Any]
(ident->entity graph ident false))
([graph :- GraphType
ident :- s/Any
nested? :- s/Bool]
;; find the entity by its ident. Some systems will make the id the entity id,
;; and the ident will be separate, so look for both.
(let [eid (or (ffirst (node/find-triple graph [ident '?a '?v]))
(ffirst (node/find-triple graph ['?eid :db/ident ident])))]
(ref->entity graph eid nested?))))
(s/defn graph->entities :- [EntityMap]
"Pulls all top level entities out of a store"
([graph :- GraphType]
(graph->entities graph false nil))
([graph :- GraphType
nested? :- s/Bool]
(graph->entities graph nested? nil))
([graph :- GraphType
nested? :- s/Bool
exclusions :- (s/maybe #{s/Keyword})]
(->> (node/find-triple graph '[?e :tg/entity true])
(map first)
(map #(ref->entity graph % nested? exclusions)))))
#?(:clj
(defn json-generate-string
([data] (j/generate-string data))
([data indent]
(j/generate-string
data
(assoc j/default-pretty-print-options
:indentation (apply str (repeat indent \space))))))
:cljs
(defn json-generate-string
([data] (.stringify js/JSON (clj->js data)))
([data indent] (.stringify js/JSON (clj->js data) nil indent))))
(s/defn graph->str :- s/Str
"Reads a store into JSON strings"
([graph :- GraphType]
(json-generate-string (graph->entities graph false)))
([graph :- GraphType, nested? :- s/Bool]
(json-generate-string (graph->entities graph nested?)))
([graph :- GraphType, nested? :- s/Bool, indent :- s/Num]
(json-generate-string (graph->entities graph nested?) indent)))