/
pack.clj
263 lines (223 loc) · 8.14 KB
/
pack.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
(ns com.timezynk.domain.pack
(:require
[clojure.core.reducers :as r]
[clojure.edn :as edn]
[clojure.string :as s]
[clojure.walk :refer [postwalk-replace]]
[com.timezynk.domain.update-leafs :refer [update-leafs]]
[com.timezynk.useful.date :as ud]
[com.timezynk.useful.mongo :as um :refer [object-id? intersecting-query start-inside-period-query]]
[slingshot.slingshot :refer [throw+]]))
;pack query
(def mongo-operators {:_from_ :$gte
:_to_ :$lte
:_lt_ :$lt
:_lte_ :$lte
:_gt_ :$gt
:_gte_ :$gte
:_or_ :$or
:_not_ :$ne
:_in_ :$in
:_elem_ :$elemMatch})
(defn- replace-with-mongo-operators [q]
(postwalk-replace mongo-operators q))
(defn- operator? [key]
(or
(= [] key)
(->> key name (re-matches #"^_.*_$"))))
(defn- clean-property-path [path]
(remove operator? path))
(defn get-type-path
"From a path, created by update-leafs,
create a path to use with get-in"
[trail]
(into []
(loop [trail (into () trail)
path []]
(let [[head & tail] trail
[tail-head] tail]
(cond
(and (keyword? head)
(s/includes? (name head) "."))
(let [next-trail (-> head
(name)
(s/split #"\.")
(->> (r/map keyword)
(into []))
(concat tail))]
(recur next-trail
path))
(= [] tail-head) (let [tail (rest tail)]
(recur tail
(conj path head
:children
(if (seq tail) :properties :type))))
(and head tail-head) (recur tail
(conj path head :properties))
head (recur tail
(conj path head :type))
:else path)))))
(defn get-type [trail _ props]
(get-in props (get-type-path trail)))
(defmulti pack-property get-type)
(defn- pack-query-parameters [q properties]
(update-leafs q (fn [path value]
(pack-property (clean-property-path path)
value
properties))))
(defn interval-query [{:keys [start _start_ end _end_ criteria _match_]}]
(if (= "intersects" (or criteria _match_))
(intersecting-query (or start _start_) (or end _end_))
(start-inside-period-query (or start _start_) (or end _end_))))
(defn pack-interval-query [q]
(if (map? (:interval q))
(merge
(dissoc q :interval)
(interval-query (:interval q)))
q))
(defn pack-query [dom-type-collection request]
(let [{:keys [domain-query-params route-params]} request
{:keys [properties]} dom-type-collection]
(-> (merge domain-query-params route-params)
(pack-query-parameters properties)
(replace-with-mongo-operators)
(pack-interval-query))))
(defn pack-post-query [dom-type-collection request]
(let [{:keys [domain-query-params route-params body-params]} request
{:keys [properties]} dom-type-collection]
(-> (merge body-params domain-query-params route-params)
(pack-query-parameters properties)
(replace-with-mongo-operators))))
; pack collects
(defn- pack-collect [properties [property-name options]]
(let [{:keys [collection
domain-type?
unpack
vid
only-keys
target]
:as collect-def} (get-in properties [property-name :collect])]
[target
(-> options
(assoc :collection collection
:domain-type? (if-not (nil? domain-type?)
domain-type?
true)
:unpack unpack
:versioned? (if vid true false)
:only-keys (let [fields (:fields options)]
(if (= :all fields) [] fields))
:ref-property (or vid property-name))
(dissoc :fields))]))
(defn pack-collects [dom-type-collection request]
(let [{:keys [domain-collect-params]} request
{:keys [properties]} dom-type-collection]
(->> domain-collect-params
(map (partial pack-collect properties))
(into {}))))
(defn filter-params [params properties flag]
(apply dissoc params
(map (fn [[k p]] k)
(filter
(fn [[k p]]
(get p flag))
properties))))
; pack body
(defn pack-doc
"Converts the document from a document with values in \"client types\",
to a document with values in \"server types\"."
[dom-type-collection doc]
(update-leafs doc
pack-property
(:properties dom-type-collection)))
(defn pack-insert
"Filters and prepares parameters before insert"
[dom-type-collection req]
(filter-params (merge (:body-params req) (:route-params req))
(:properties dom-type-collection)
:remove-on-create?))
(defn pack-bulk-insert
"Filters and prepares parameters before bulk insert"
[dom-type-collection req]
(map
(fn [d]
(filter-params (merge d (:route-params req) (:domain-query-params req))
(:properties dom-type-collection)
:remove-on-create?))
(:body-params req)))
(defn pack-update
"Filters and prepares parameters before update"
[dom-type-collection req]
(filter-params (merge (:body-params req) (:route-params req))
(:properties dom-type-collection)
:remove-on-update?))
(defn pack-values
"Pack dynamic values so that they can be used without overwriting"
[values]
(r/reduce
(fn [acc k v]
(assoc acc (keyword (str "values." (name k))) v))
{} values))
(defmethod pack-property :any [_ v props] v)
(defmethod pack-property :string [_ v props]
(when v
(if (string? v)
(let [^String trimmed (s/trim v)]
(when-not (.isEmpty ^String trimmed) trimmed))
(.toString v))))
(defmethod pack-property :object-id [_ v props]
(cond-> v
(object-id? v) (um/object-id)))
(defmethod pack-property :date [trail v props]
(try
(ud/->local-date v)
(catch Exception e
(throw+
{:code 400
:message (str "Parse error at path [\"" (s/join "\", \"" (map name trail)) "\"]. " (.getMessage e))}))))
(defmethod pack-property :time [trail v props]
(try
(ud/->local-time v)
(catch Exception e
(throw+
{:code 400
:message (str "Parse error at path [\"" (s/join "\", \"" (map name trail)) "\"]. " (.getMessage e))}))))
(defmethod pack-property :date-time [trail v props]
(try
(ud/->local-datetime v)
(catch Exception e
(throw+
{:code 400
:message (str "Parse error at path [\"" (s/join "\", \"" (map name trail)) "\"]. " (.getMessage e))}))))
(defmethod pack-property :number [_ v props]
(if (string? v)
(edn/read-string v)
(if (ratio? v)
(double v)
v)))
(defmethod pack-property :integer [_ v props]
(if (string? v)
(edn/read-string v)
v))
(defmethod pack-property :duration [_ v props]
(if (string? v)
(edn/read-string v)
(if (ratio? v)
(double v)
v)))
(defmethod pack-property :timestamp [_ v props]
(let [parsed (if (string? v) (edn/read-string v) v)]
; Accept false and turn it into nil for backwards compatibility on the archived field
(if (not parsed) nil parsed)))
(defmethod pack-property :boolean [trail v props]
(if (string? v)
(edn/read-string v)
(if v true v)))
(defmethod pack-property :vector [trail v props]
(if (sequential? v)
v
(pack-property (conj trail []) v props)))
(defmethod pack-property :map [_ v props]
v)
(defmethod pack-property nil [trail v props]
v)