From 4ffab49eb4964ae97fef1c233740a8e9c4b4af5b Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 25 Feb 2015 21:02:26 +0200 Subject: [PATCH 1/2] Fixed using regex patterns inside ?-pattern --- spec/domkm/silk_spec.cljx | 16 ++++++++++++---- src/domkm/silk.cljx | 28 +++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/spec/domkm/silk_spec.cljx b/spec/domkm/silk_spec.cljx index c3526a6..92d7af6 100644 --- a/spec/domkm/silk_spec.cljx +++ b/spec/domkm/silk_spec.cljx @@ -280,7 +280,8 @@ (silk/routes [[:id2 [["foo" "bar" :baz]]]]) [:id3 [nil {"a" :b}]] [:id4 [["search"] {"q" (silk/? :q {:q "default"})}]] - [:id5 [["users"] {"q" (silk/? :q)}]]])) + [:id5 [["users"] {"q" (silk/? :q)}]] + [:id6 [["test"] {"q" (silk/? (silk/int :q))}]]])) (spec/with-all clean-params #(dissoc % :domkm.silk/routes :domkm.silk/url :domkm.silk/pattern :domkm.silk/name)) (spec/it @@ -303,7 +304,9 @@ (spec/it "unmatches successfully" (spec/should= (silk/map->URL {:query {"a" "c"}}) - (silk/unmatch @routes {:domkm.silk/name :id3 :b "c"}))) + (silk/unmatch @routes {:domkm.silk/name :id3 :b "c"})) + (spec/should= (silk/map->URL {:path ["test"] :query {"q" nil}}) + (silk/unmatch @routes {:domkm.silk/name :id6}))) (spec/it "unmatches unsuccessfully" (spec/should-throw AssertionError (silk/unmatch @routes {}))) @@ -326,7 +329,10 @@ (silk/arrive @routes "/users"))) (spec/should= {:q "bar"} (@clean-params - (silk/arrive @routes "/users?q=bar")))) + (silk/arrive @routes "/users?q=bar"))) + (spec/should= {:q nil} + (@clean-params + (silk/arrive @routes "/test")))) (spec/it "departs" (spec/should= "/foo/bar/bloop" @@ -340,4 +346,6 @@ (spec/should= "/users" (silk/depart @routes :id5)) (spec/should= "/users?q=bar" - (silk/depart @routes :id5 {:q "bar"}))))) + (silk/depart @routes :id5 {:q "bar"})) + (spec/should= "/test" + (silk/depart @routes :id6))))) diff --git a/src/domkm/silk.cljx b/src/domkm/silk.cljx index 502a51e..acb7398 100644 --- a/src/domkm/silk.cljx +++ b/src/domkm/silk.cljx @@ -95,7 +95,8 @@ (-match [this that]) (-unmatch [this params]) (-match-validator [this]) - (-unmatch-validators [this])) + (-unmatch-validators [this]) + (-create-default [this v])) (defn pattern? [x] (satisfies? Pattern x)) @@ -134,6 +135,10 @@ :post [(match-valid? ptrn %)]} (-unmatch ptrn params)) +(defn create-default + ([ptrn] (create-default ptrn nil)) + ([ptrn v] (-create-default ptrn v))) + ;;;; Native Patterns ;;;; @@ -158,7 +163,9 @@ (-match-validator [_] some?) (-unmatch-validators [this] - {this some?})) + {this some?}) + (-create-default [this v] + {this v})) (defn ^:private match-coll [ks %1s %2s] (loop [ks ks @@ -229,13 +236,16 @@ deserialize (hash-map param-key))) (-unmatch [_ params] - (->> param-key - (get params) - serialize)) + (let [v (get params param-key)] + (if (= v ::optional-key-has-no-value) + v + (serialize v)))) (-match-validator [_] - string?) + #(or (= % ::optional-key-has-no-value) (string? %))) (-unmatch-validators [_] - {param-key validate})) + {param-key #(or (= % ::optional-key-has-no-value) (validate %))}) + (-create-default [_ v] + (create-default param-key v))) (defn regex ([k re] @@ -316,7 +326,7 @@ Pattern (-match [_ that] (if (nil? that) - (or default-params {ptrn nil}) + (or default-params (create-default ptrn)) (match ptrn that))) (-unmatch [_ params] (let [r (unmatch ptrn @@ -325,7 +335,7 @@ dval pval)) params - (or default-params {ptrn ::optional-key-has-no-value})))] + (or default-params (create-default ptrn ::optional-key-has-no-value))))] (if-not (= ::optional-key-has-no-value r) r))) (-match-validator [_] From 8509bd0e85ece099ca596af68a8353b4eaa8a3f2 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 25 Feb 2015 21:15:10 +0200 Subject: [PATCH 2/2] Created validator for optional values --- src/domkm/silk.cljx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/domkm/silk.cljx b/src/domkm/silk.cljx index acb7398..3bb29b4 100644 --- a/src/domkm/silk.cljx +++ b/src/domkm/silk.cljx @@ -101,10 +101,16 @@ (defn pattern? [x] (satisfies? Pattern x)) +(defn- optional-value-is-allowed [validator] + (fn [x] + (if (= x ::optional-key-has-no-value) + true + (validator x)))) + (defn match-validator [ptrn] {:pre [(pattern? ptrn)] :post [(fn? %)]} - (-match-validator ptrn)) + (optional-value-is-allowed (-match-validator ptrn))) (defn unmatch-validators [ptrn] {:pre [(pattern? ptrn)] @@ -241,9 +247,9 @@ v (serialize v)))) (-match-validator [_] - #(or (= % ::optional-key-has-no-value) (string? %))) + string?) (-unmatch-validators [_] - {param-key #(or (= % ::optional-key-has-no-value) (validate %))}) + {param-key (optional-value-is-allowed validate)}) (-create-default [_ v] (create-default param-key v)))