File tree 2 files changed +37
-13
lines changed
2 files changed +37
-13
lines changed Original file line number Diff line number Diff line change 280
280
(silk/routes [[:id2 [[" foo" " bar" :baz ]]]])
281
281
[:id3 [nil {" a" :b }]]
282
282
[:id4 [[" search" ] {" q" (silk/? :q {:q " default" })}]]
283
- [:id5 [[" users" ] {" q" (silk/? :q )}]]]))
283
+ [:id5 [[" users" ] {" q" (silk/? :q )}]]
284
+ [:id6 [[" test" ] {" q" (silk/? (silk/int :q ))}]]]))
284
285
(spec/with-all clean-params
285
286
#(dissoc % :domkm.silk/routes :domkm.silk/url :domkm.silk/pattern :domkm.silk/name ))
286
287
(spec/it
303
304
(spec/it
304
305
" unmatches successfully"
305
306
(spec/should= (silk/map->URL {:query {" a" " c" }})
306
- (silk/unmatch @routes {:domkm.silk/name :id3 :b " c" })))
307
+ (silk/unmatch @routes {:domkm.silk/name :id3 :b " c" }))
308
+ (spec/should= (silk/map->URL {:path [" test" ] :query {" q" nil }})
309
+ (silk/unmatch @routes {:domkm.silk/name :id6 })))
307
310
(spec/it
308
311
" unmatches unsuccessfully"
309
312
(spec/should-throw AssertionError (silk/unmatch @routes {})))
326
329
(silk/arrive @routes " /users" )))
327
330
(spec/should= {:q " bar" }
328
331
(@clean-params
329
- (silk/arrive @routes " /users?q=bar" ))))
332
+ (silk/arrive @routes " /users?q=bar" )))
333
+ (spec/should= {:q nil }
334
+ (@clean-params
335
+ (silk/arrive @routes " /test" ))))
330
336
(spec/it
331
337
" departs"
332
338
(spec/should= " /foo/bar/bloop"
340
346
(spec/should= " /users"
341
347
(silk/depart @routes :id5 ))
342
348
(spec/should= " /users?q=bar"
343
- (silk/depart @routes :id5 {:q " bar" })))))
349
+ (silk/depart @routes :id5 {:q " bar" }))
350
+ (spec/should= " /test"
351
+ (silk/depart @routes :id6 )))))
Original file line number Diff line number Diff line change 95
95
(-match [this that])
96
96
(-unmatch [this params])
97
97
(-match-validator [this])
98
- (-unmatch-validators [this]))
98
+ (-unmatch-validators [this])
99
+ (-create-default [this v]))
99
100
100
101
(defn pattern? [x]
101
102
(satisfies? Pattern x))
102
103
104
+ (defn- optional-value-is-allowed [validator]
105
+ (fn [x]
106
+ (if (= x ::optional-key-has-no-value )
107
+ true
108
+ (validator x))))
109
+
103
110
(defn match-validator [ptrn]
104
111
{:pre [(pattern? ptrn)]
105
112
:post [(fn? %)]}
106
- (- match-validator ptrn))
113
+ (optional-value-is-allowed ( - match-validator ptrn) ))
107
114
108
115
(defn unmatch-validators [ptrn]
109
116
{:pre [(pattern? ptrn)]
134
141
:post [(match-valid? ptrn %)]}
135
142
(-unmatch ptrn params))
136
143
144
+ (defn create-default
145
+ ([ptrn] (create-default ptrn nil ))
146
+ ([ptrn v] (-create-default ptrn v)))
147
+
137
148
138
149
; ;;; Native Patterns ;;;;
139
150
158
169
(-match-validator [_]
159
170
some?)
160
171
(-unmatch-validators [this]
161
- {this some?}))
172
+ {this some?})
173
+ (-create-default [this v]
174
+ {this v}))
162
175
163
176
(defn ^:private match-coll [ks %1 s %2 s]
164
177
(loop [ks ks
229
242
deserialize
230
243
(hash-map param-key)))
231
244
(-unmatch [_ params]
232
- (->> param-key
233
- (get params)
234
- serialize))
245
+ (let [v (get params param-key)]
246
+ (if (= v ::optional-key-has-no-value )
247
+ v
248
+ (serialize v))))
235
249
(-match-validator [_]
236
250
string?)
237
251
(-unmatch-validators [_]
238
- {param-key validate}))
252
+ {param-key (optional-value-is-allowed validate)})
253
+ (-create-default [_ v]
254
+ (create-default param-key v)))
239
255
240
256
(defn regex
241
257
([k re]
316
332
Pattern
317
333
(-match [_ that]
318
334
(if (nil? that)
319
- (or default-params {ptrn nil } )
335
+ (or default-params ( create-default ptrn) )
320
336
(match ptrn that)))
321
337
(-unmatch [_ params]
322
338
(let [r (unmatch ptrn
325
341
dval
326
342
pval))
327
343
params
328
- (or default-params { ptrn ::optional-key-has-no-value } )))]
344
+ (or default-params ( create-default ptrn ::optional-key-has-no-value ) )))]
329
345
(if-not (= ::optional-key-has-no-value r)
330
346
r)))
331
347
(-match-validator [_]
You can’t perform that action at this time.
0 commit comments