From 0e144f99a98efd0e6714b77fce727cfeaf7d0c55 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Wed, 17 Apr 2024 09:21:42 +0200 Subject: [PATCH] apply format --- composer.lock | 34 +++++----- src/router.phel | 138 +++++++++++++++++++------------------- src/router/flatten.phel | 6 +- tests/router.phel | 104 ++++++++++++++-------------- tests/router/flatten.phel | 33 +++++---- 5 files changed, 155 insertions(+), 160 deletions(-) diff --git a/composer.lock b/composer.lock index 6126aeb..cf01a5d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c2168173446083caaaf41477aadc7cf7", + "content-hash": "e21096d604cdc8dff0c818c3e81919a6", "packages": [ { "name": "gacela-project/container", @@ -816,36 +816,34 @@ }, { "name": "symfony/routing", - "version": "v6.4.6", + "version": "v7.0.6", "source": { "type": "git", "url": "https://github.com/symfony/routing.git", - "reference": "f2591fd1f8c6e3734656b5d6b3829e8bf81f507c" + "reference": "cded64e5bbf9f31786f1055fcc76718fdd77519c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/routing/zipball/f2591fd1f8c6e3734656b5d6b3829e8bf81f507c", - "reference": "f2591fd1f8c6e3734656b5d6b3829e8bf81f507c", + "url": "https://api.github.com/repos/symfony/routing/zipball/cded64e5bbf9f31786f1055fcc76718fdd77519c", + "reference": "cded64e5bbf9f31786f1055fcc76718fdd77519c", "shasum": "" }, "require": { - "php": ">=8.1", + "php": ">=8.2", "symfony/deprecation-contracts": "^2.5|^3" }, "conflict": { - "doctrine/annotations": "<1.12", - "symfony/config": "<6.2", - "symfony/dependency-injection": "<5.4", - "symfony/yaml": "<5.4" + "symfony/config": "<6.4", + "symfony/dependency-injection": "<6.4", + "symfony/yaml": "<6.4" }, "require-dev": { - "doctrine/annotations": "^1.12|^2", "psr/log": "^1|^2|^3", - "symfony/config": "^6.2|^7.0", - "symfony/dependency-injection": "^5.4|^6.0|^7.0", - "symfony/expression-language": "^5.4|^6.0|^7.0", - "symfony/http-foundation": "^5.4|^6.0|^7.0", - "symfony/yaml": "^5.4|^6.0|^7.0" + "symfony/config": "^6.4|^7.0", + "symfony/dependency-injection": "^6.4|^7.0", + "symfony/expression-language": "^6.4|^7.0", + "symfony/http-foundation": "^6.4|^7.0", + "symfony/yaml": "^6.4|^7.0" }, "type": "library", "autoload": { @@ -879,7 +877,7 @@ "url" ], "support": { - "source": "https://github.com/symfony/routing/tree/v6.4.6" + "source": "https://github.com/symfony/routing/tree/v7.0.6" }, "funding": [ { @@ -895,7 +893,7 @@ "type": "tidelift" } ], - "time": "2024-03-28T13:28:49+00:00" + "time": "2024-03-28T21:02:11+00:00" }, { "name": "symfony/service-contracts", diff --git a/src/router.phel b/src/router.phel index d682b4c..d36cea3 100644 --- a/src/router.phel +++ b/src/router.phel @@ -15,9 +15,9 @@ (:use \Symfony\Component\Routing\Exception\ResourceNotFoundException)) (definterface Router - (match-by-path [this path] "Matches a route given a path. Returns nil if path doesn't match.") - (match-by-name [this route-name] "Matches a route given a route name. Returns nil if route can't be found.") - (generate [this route-name parameter] "Generate a url for a route")) + (match-by-path [this path] "Matches a route given a path. Returns nil if path doesn't match.") + (match-by-name [this route-name] "Matches a route given a route name. Returns nil if route can't be found.") + (generate [this route-name parameter] "Generate a url for a route")) (defn- build-route-collection [normalized-routes] (for [route :in normalized-routes @@ -29,39 +29,39 @@ coll)) (defstruct SymfonyRouter [route-collection] - Router - (match-by-path [this path] - (try - (let [context (php/new RequestContext) - matcher (php/new UrlMatcher route-collection context) - parameters (php/-> matcher (match path)) - params-map (for [[k v] :pairs parameters - :when (not (= k "_route")) - :reduce [acc {}]] - (put acc (keyword k) v)) - route-name (php/aget parameters "_route") - route (php/-> route-collection (get route-name))] - {:template (php/-> route (getOption "template")) - :data (php/-> route (getOption "data")) - :path path - :path-params params-map}) - (catch NoConfigurationException e nil) - (catch ResourceNotFoundException e nil) - (catch MethodNotAllowedException e nil))) - (match-by-name [this route-name] - (let [route (php/-> route-collection (get (full-name route-name)))] - (when route - {:template (php/-> route (getOption "template")) - :data (php/-> route (getOption "data"))}))) - (generate [this route-name parameters] - (let [context (php/new RequestContext) - generator (php/new UrlGenerator route-collection context) - arr (->> - parameters - (map-indexed (fn [k v] [(name k) v])) - flatten - (apply php-associative-array))] - (php/-> generator (generate (full-name route-name) arr))))) + Router + (match-by-path [this path] + (try + (let [context (php/new RequestContext) + matcher (php/new UrlMatcher route-collection context) + parameters (php/-> matcher (match path)) + params-map (for [[k v] :pairs parameters + :when (not (= k "_route")) + :reduce [acc {}]] + (put acc (keyword k) v)) + route-name (php/aget parameters "_route") + route (php/-> route-collection (get route-name))] + {:template (php/-> route (getOption "template")) + :data (php/-> route (getOption "data")) + :path path + :path-params params-map}) + (catch NoConfigurationException e nil) + (catch ResourceNotFoundException e nil) + (catch MethodNotAllowedException e nil))) + (match-by-name [this route-name] + (let [route (php/-> route-collection (get (full-name route-name)))] + (when route + {:template (php/-> route (getOption "template")) + :data (php/-> route (getOption "data"))}))) + (generate [this route-name parameters] + (let [context (php/new RequestContext) + generator (php/new UrlGenerator route-collection context) + arr (->> + parameters + (map-indexed (fn [k v] [(name k) v])) + flatten + (apply php-associative-array))] + (php/-> generator (generate (full-name route-name) arr))))) (defn router "Builds a router" @@ -73,39 +73,39 @@ (-> raw-routes (flatten/flatten-routes path data) build-route-collection)))) (defstruct CompiledSymfonyRouter [compiled-matcher-routes compiled-generator-routes indexed-routes] - Router - (match-by-path [this path] - (try - (let [context (php/new RequestContext) - matcher (php/new CompiledUrlMatcher compiled-matcher-routes context) - parameters (php/-> matcher (match path)) - params-map (for [[k v] :pairs parameters - :when (not (= k "_route")) - :reduce [acc {}]] - (put acc (keyword k) v)) - route-name (php/aget parameters "_route") - [template data] (get indexed-routes route-name)] - {:template template - :data data - :path path - :path-params params-map}) - (catch NoConfigurationException e nil) - (catch ResourceNotFoundException e nil) - (catch MethodNotAllowedException e nil))) - (match-by-name [this route-name] - (let [route (get indexed-routes (full-name route-name))] - (when route - {:template (first route) - :data (second route)}))) - (generate [this route-name parameters] - (let [context (php/new RequestContext) - generator (php/new CompiledUrlGenerator compiled-generator-routes context) - arr (->> - parameters - (map-indexed (fn [k v] [(name k) v])) - flatten - (apply php-associative-array))] - (php/-> generator (generate (full-name route-name) arr))))) + Router + (match-by-path [this path] + (try + (let [context (php/new RequestContext) + matcher (php/new CompiledUrlMatcher compiled-matcher-routes context) + parameters (php/-> matcher (match path)) + params-map (for [[k v] :pairs parameters + :when (not (= k "_route")) + :reduce [acc {}]] + (put acc (keyword k) v)) + route-name (php/aget parameters "_route") + [template data] (get indexed-routes route-name)] + {:template template + :data data + :path path + :path-params params-map}) + (catch NoConfigurationException e nil) + (catch ResourceNotFoundException e nil) + (catch MethodNotAllowedException e nil))) + (match-by-name [this route-name] + (let [route (get indexed-routes (full-name route-name))] + (when route + {:template (first route) + :data (second route)}))) + (generate [this route-name parameters] + (let [context (php/new RequestContext) + generator (php/new CompiledUrlGenerator compiled-generator-routes context) + arr (->> + parameters + (map-indexed (fn [k v] [(name k) v])) + flatten + (apply php-associative-array))] + (php/-> generator (generate (full-name route-name) arr))))) (defmacro compiled-router "Builds a compiled router" diff --git a/src/router/flatten.phel b/src/router/flatten.phel index 68a243e..d3c3630 100644 --- a/src/router/flatten.phel +++ b/src/router/flatten.phel @@ -4,8 +4,8 @@ (defn- walk-many [routes prefix-path prev-data] (apply concat [] - (for [r :in routes] - (walk-one r prefix-path prev-data)))) + (for [r :in routes] + (walk-one r prefix-path prev-data)))) (defn- walk-one [routes prefix-path prev-data] (if (vector? (first routes)) @@ -18,7 +18,7 @@ [maybe-arg (rest args)]) next-data (deep-merge prev-data data) child-routes (walk-many (keep identity childs) (str prefix-path path) next-data)] - (if (empty? childs) [[(str prefix-path path) next-data]] child-routes))))) + (if (empty? childs) [[(str prefix-path path) next-data]] child-routes))))) (defn flatten-routes "Flattens nested routes to a vector of tuples [path data]." diff --git a/tests/router.phel b/tests/router.phel index 2bfd938..09518f3 100644 --- a/tests/router.phel +++ b/tests/router.phel @@ -7,118 +7,118 @@ (deftest test-match-by-path (is (= - {:template "/ping" :data {} :path "/ping" :path-params {}} - (match-by-path (r/router ["/ping"]) "/ping")) + {:template "/ping" :data {} :path "/ping" :path-params {}} + (match-by-path (r/router ["/ping"]) "/ping")) "single static route") (is (= - nil - (match-by-path (r/router ["/ping"]) "/pong")) + nil + (match-by-path (r/router ["/ping"]) "/pong")) "no matching static route") (is (= - {:template "/pong" :data {} :path "/pong" :path-params {}} - (match-by-path (r/router [["/ping"] ["/pong"]]) "/pong")) + {:template "/pong" :data {} :path "/pong" :path-params {}} + (match-by-path (r/router [["/ping"] ["/pong"]]) "/pong")) "two static route") (is (= - {:template "/ping/{id<\\d+>}" :data {:name ::ping} :path "/ping/123" :path-params {:id "123"}} - (match-by-path (r/router [["/ping/{id<\\d+>}" {:name ::ping}]]) "/ping/123")) + {:template "/ping/{id<\\d+>}" :data {:name ::ping} :path "/ping/123" :path-params {:id "123"}} + (match-by-path (r/router [["/ping/{id<\\d+>}" {:name ::ping}]]) "/ping/123")) "route with path parameter") (is (= - nil - (match-by-path (r/router [["/ping/{id<\d+>}" {:name ::ping}]]) "/ping/abc")) + nil + (match-by-path (r/router [["/ping/{id<\d+>}" {:name ::ping}]]) "/ping/abc")) "no matching path requirement with path parameter")) (deftest test-match-by-path-compiled-router (is (= - {:template "/ping" :data {} :path "/ping" :path-params {}} - (match-by-path (r/compiled-router ["/ping"]) "/ping")) + {:template "/ping" :data {} :path "/ping" :path-params {}} + (match-by-path (r/compiled-router ["/ping"]) "/ping")) "single static route") (is (= - nil - (match-by-path (r/compiled-router ["/ping"]) "/pong")) + nil + (match-by-path (r/compiled-router ["/ping"]) "/pong")) "no matching static route") (is (= - {:template "/pong" :data {} :path "/pong" :path-params {}} - (match-by-path (r/compiled-router [["/ping"] ["/pong"]]) "/pong")) + {:template "/pong" :data {} :path "/pong" :path-params {}} + (match-by-path (r/compiled-router [["/ping"] ["/pong"]]) "/pong")) "two static route") (is (= - {:template "/ping/{id<\\d+>}" :data {:name ::ping} :path "/ping/123" :path-params {:id "123"}} - (match-by-path (r/compiled-router [["/ping/{id<\\d+>}" {:name ::ping}]]) "/ping/123")) + {:template "/ping/{id<\\d+>}" :data {:name ::ping} :path "/ping/123" :path-params {:id "123"}} + (match-by-path (r/compiled-router [["/ping/{id<\\d+>}" {:name ::ping}]]) "/ping/123")) "route with path parameter") (is (= - nil - (match-by-path (r/compiled-router [["/ping/{id<\d+>}" {:name ::ping}]]) "/ping/abc")) + nil + (match-by-path (r/compiled-router [["/ping/{id<\d+>}" {:name ::ping}]]) "/ping/abc")) "no matching path requirement with path parameter")) (deftest test-match-by-name (is (= - {:template "/ping" :data {:name ::ping}} - (match-by-name (r/router [["/ping" {:name ::ping}]]) ::ping)) + {:template "/ping" :data {:name ::ping}} + (match-by-name (r/router [["/ping" {:name ::ping}]]) ::ping)) "single static route") (is (= - {:template "/ping/{id}" :data {:name ::ping}} - (match-by-name (r/router [["/ping/{id}" {:name ::ping}]]) ::ping)) + {:template "/ping/{id}" :data {:name ::ping}} + (match-by-name (r/router [["/ping/{id}" {:name ::ping}]]) ::ping)) "route with path parameter") (is (= - nil - (match-by-name (r/router [["/ping/{id}" {:name ::ping}]]) ::pong)) + nil + (match-by-name (r/router [["/ping/{id}" {:name ::ping}]]) ::pong)) "route with name not found")) (deftest test-match-by-name-compiled-router (is (= - {:template "/ping" :data {:name ::ping}} - (match-by-name (r/compiled-router [["/ping" {:name ::ping}]]) ::ping)) + {:template "/ping" :data {:name ::ping}} + (match-by-name (r/compiled-router [["/ping" {:name ::ping}]]) ::ping)) "single static route") (is (= - {:template "/ping/{id}" :data {:name ::ping}} - (match-by-name (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping)) + {:template "/ping/{id}" :data {:name ::ping}} + (match-by-name (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping)) "route with path parameter") (is (= - nil - (match-by-name (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::pong)) + nil + (match-by-name (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::pong)) "route with name not found")) (deftest test-generate (is (= - "/ping" - (generate (r/router [["/ping" {:name ::ping}]]) ::ping {})) + "/ping" + (generate (r/router [["/ping" {:name ::ping}]]) ::ping {})) "single static route") (is (thrown? - RouteNotFoundException - (generate (r/router [["/ping" {:name ::ping}]]) ::pong {})) + RouteNotFoundException + (generate (r/router [["/ping" {:name ::ping}]]) ::pong {})) "unknown static route") (is (= - "/ping/10" - (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10})) + "/ping/10" + (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10})) "generate route with path parameter") (is (thrown? - MissingMandatoryParametersException - (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {})) + MissingMandatoryParametersException + (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {})) "generate route with missing path parameter") (is (= - "/ping/10?foo=bar" - (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10 :foo "bar"})) + "/ping/10?foo=bar" + (generate (r/router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10 :foo "bar"})) "generate route with additional query parameter")) (deftest test-generate-compiled-router (is (= - "/ping" - (generate (r/compiled-router [["/ping" {:name ::ping}]]) ::ping {})) + "/ping" + (generate (r/compiled-router [["/ping" {:name ::ping}]]) ::ping {})) "single static route") (is (thrown? - RouteNotFoundException - (generate (r/compiled-router [["/ping" {:name ::ping}]]) ::pong {})) + RouteNotFoundException + (generate (r/compiled-router [["/ping" {:name ::ping}]]) ::pong {})) "unknown static route") (is (= - "/ping/10" - (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10})) + "/ping/10" + (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10})) "generate route with path parameter") (is (thrown? - MissingMandatoryParametersException - (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {})) + MissingMandatoryParametersException + (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {})) "generate route with missing path parameter") (is (= - "/ping/10?foo=bar" - (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10 :foo "bar"})) + "/ping/10?foo=bar" + (generate (r/compiled-router [["/ping/{id}" {:name ::ping}]]) ::ping {:id 10 :foo "bar"})) "generate route with additional query parameter")) (defn fnil [f x] diff --git a/tests/router/flatten.phel b/tests/router/flatten.phel index ca63794..756aca9 100644 --- a/tests/router/flatten.phel +++ b/tests/router/flatten.phel @@ -4,28 +4,25 @@ (deftest flatten-routes-test (is (= - [["/ping" {}]] - (flatten-routes ["/ping"] "" {}) - ) + [["/ping" {}]] + (flatten-routes ["/ping"] "" {})) "simple route") (is (= - [["/ping" {}] ["/pong" {}]] - (flatten-routes [["/ping"] ["/pong"]] "" {})) + [["/ping" {}] ["/pong" {}]] + (flatten-routes [["/ping"] ["/pong"]] "" {})) "two routes") (is (= - [["/ping" {:name ::ping}] ["/pong" {:name ::pong}]] - (flatten-routes [["/ping" {:name ::ping}] ["/pong" {:name ::pong}]] "" {})) + [["/ping" {:name ::ping}] ["/pong" {:name ::pong}]] + (flatten-routes [["/ping" {:name ::ping}] ["/pong" {:name ::pong}]] "" {})) "route with arguments") (is (= - [ - ["/api/admin" {:middleware [::admin] :name ::admin}] - ["/api/admin/db" {:middleware [::admin ::connect] :name ::db}] - ["/api/ping" {:name ::ping}] - ] - (flatten-routes - ["/api" - ["/admin" {:middleware [::admin]} - ["" {:name ::admin}] - ["/db" {:name ::db :middleware [::connect]}]] - ["/ping" {:name ::ping}]] "" {})) + [["/api/admin" {:middleware [::admin] :name ::admin}] + ["/api/admin/db" {:middleware [::admin ::connect] :name ::db}] + ["/api/ping" {:name ::ping}]] + (flatten-routes + ["/api" + ["/admin" {:middleware [::admin]} + ["" {:name ::admin}] + ["/db" {:name ::db :middleware [::connect]}]] + ["/ping" {:name ::ping}]] "" {})) "nested routes"))