Merge pull request #269 from metosin/PolishFor032

Polish for 0.3.2
This commit is contained in:
Tommi Reiman 2019-05-11 14:29:58 +03:00 committed by GitHub
commit 3b4ed0061c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 36 deletions

View file

@ -1,18 +1,18 @@
(ns example.server (ns example.server
(:require [reitit.ring :as ring] (:require [reitit.ring :as ring]
[reitit.http :as http] [reitit.http :as http]
[reitit.coercion.spec]
[reitit.swagger :as swagger] [reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui] [reitit.swagger-ui :as swagger-ui]
[reitit.http.coercion :as coercion] [reitit.http.coercion :as coercion]
[reitit.coercion.spec :as spec-coercion] [reitit.dev.pretty :as pretty]
[reitit.interceptor.sieppari :as sieppari]
[reitit.http.interceptors.parameters :as parameters] [reitit.http.interceptors.parameters :as parameters]
[reitit.http.interceptors.muuntaja :as muuntaja] [reitit.http.interceptors.muuntaja :as muuntaja]
[reitit.http.interceptors.exception :as exception] [reitit.http.interceptors.exception :as exception]
[reitit.http.interceptors.multipart :as multipart] [reitit.http.interceptors.multipart :as multipart]
[reitit.http.spec :as spec]
[reitit.http.interceptors.dev :as dev] [reitit.http.interceptors.dev :as dev]
[reitit.interceptor.sieppari :as sieppari] [reitit.http.spec :as spec]
[reitit.dev.pretty :as pretty]
[spec-tools.spell :as spell] [spec-tools.spell :as spell]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[aleph.http :as client] [aleph.http :as client]
@ -113,10 +113,10 @@
:body {:total (- x y)}})}}]]] :body {:total (- x y)}})}}]]]
{;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs {;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
:validate spec/validate ;; enable spec validation for route data ;;:validate spec/validate ;; enable spec validation for route data
:reitit.spec/wrap spell/closed ;; strict top-level validation (alpha) ;;:reitit.spec/wrap spell/closed ;; strict top-level validation
:exception pretty/exception :exception pretty/exception
:data {:coercion spec-coercion/coercion :data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance :muuntaja m/instance
:interceptors [;; swagger feature :interceptors [;; swagger feature
swagger/swagger-feature swagger/swagger-feature
@ -139,7 +139,8 @@
(ring/routes (ring/routes
(swagger-ui/create-swagger-ui-handler (swagger-ui/create-swagger-ui-handler
{:path "/" {:path "/"
:config {:validatorUrl nil}}) :config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)) (ring/create-default-handler))
{:executor sieppari/executor})) {:executor sieppari/executor}))

View file

@ -1,16 +1,20 @@
(ns example.server (ns example.server
(:require [io.pedestal.http :as server] (:require [io.pedestal.http :as server]
[reitit.pedestal :as pedestal]
[reitit.ring :as ring] [reitit.ring :as ring]
[reitit.http :as http] [reitit.http :as http]
[reitit.coercion.spec]
[reitit.swagger :as swagger] [reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui] [reitit.swagger-ui :as swagger-ui]
[reitit.http.coercion :as coercion] [reitit.http.coercion :as coercion]
[reitit.coercion.spec :as spec-coercion]
[reitit.http.interceptors.parameters :as parameters] [reitit.http.interceptors.parameters :as parameters]
[reitit.http.interceptors.muuntaja :as muuntaja] [reitit.http.interceptors.muuntaja :as muuntaja]
[reitit.http.interceptors.exception :as exception]
[reitit.http.interceptors.multipart :as multipart] [reitit.http.interceptors.multipart :as multipart]
[reitit.http.interceptors.dev :as dev] [reitit.http.interceptors.dev :as dev]
[reitit.http.spec :as spec]
[spec-tools.spell :as spell]
[io.pedestal.http :as server]
[reitit.pedestal :as pedestal]
[clojure.core.async :as a] [clojure.core.async :as a]
[clojure.java.io :as io] [clojure.java.io :as io]
[muuntaja.core :as m])) [muuntaja.core :as m]))
@ -76,15 +80,22 @@
{:status 200 {:status 200
:body {:total (+ x y)}})}}]]] :body {:total (+ x y)}})}}]]]
{;;:reitit.interceptor/transform dev/print-context-diffs {;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
:data {:coercion spec-coercion/coercion ;;:validate spec/validate ;; enable spec validation for route data
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
:exception pretty/exception
:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance :muuntaja m/instance
:interceptors [;; query-params & form-params :interceptors [;; swagger feature
swagger/swagger-feature
;; query-params & form-params
(parameters/parameters-interceptor) (parameters/parameters-interceptor)
;; content-negotiation ;; content-negotiation
(muuntaja/format-negotiate-interceptor) (muuntaja/format-negotiate-interceptor)
;; encoding response body ;; encoding response body
(muuntaja/format-response-interceptor) (muuntaja/format-response-interceptor)
;; exception handling
(exception/exception-interceptor)
;; decoding request body ;; decoding request body
(muuntaja/format-request-interceptor) (muuntaja/format-request-interceptor)
;; coercing response bodys ;; coercing response bodys
@ -98,7 +109,8 @@
(ring/routes (ring/routes
(swagger-ui/create-swagger-ui-handler (swagger-ui/create-swagger-ui-handler
{:path "/" {:path "/"
:config {:validatorUrl nil}}) :config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-resource-handler) (ring/create-resource-handler)
(ring/create-default-handler)))) (ring/create-default-handler))))

View file

@ -1,13 +1,17 @@
(ns example.server (ns example.server
(:require [reitit.ring :as ring] (:require [reitit.ring :as ring]
[reitit.coercion.spec]
[reitit.swagger :as swagger] [reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui] [reitit.swagger-ui :as swagger-ui]
[reitit.ring.coercion :as coercion] [reitit.ring.coercion :as coercion]
[reitit.coercion.spec] [reitit.dev.pretty :as pretty]
[reitit.ring.middleware.muuntaja :as muuntaja] [reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.exception :as exception] [reitit.ring.middleware.exception :as exception]
[reitit.ring.middleware.multipart :as multipart] [reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.parameters :as parameters] [reitit.ring.middleware.parameters :as parameters]
[reitit.ring.middleware.dev :as dev]
[reitit.ring.spec :as spec]
[spec-tools.spell :as spell]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[muuntaja.core :as m] [muuntaja.core :as m]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
@ -72,9 +76,15 @@
{:status 200 {:status 200
:body {:total (+ x y)}})}}]]] :body {:total (+ x y)}})}}]]]
{:data {:coercion reitit.coercion.spec/coercion {;;:reitit.middleware/transform dev/print-request-diffs ;; pretty diffs
;;:validate spec/validate ;; enable spec validation for route data
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
:exception pretty/exception
:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance :muuntaja m/instance
:middleware [;; query-params & form-params :middleware [;; swagger feature
swagger/swagger-feature
;; query-params & form-params
parameters/parameters-middleware parameters/parameters-middleware
;; content-negotiation ;; content-negotiation
muuntaja/format-negotiate-middleware muuntaja/format-negotiate-middleware
@ -93,7 +103,8 @@
(ring/routes (ring/routes
(swagger-ui/create-swagger-ui-handler (swagger-ui/create-swagger-ui-handler
{:path "/" {:path "/"
:config {:validatorUrl nil}}) :config {:validatorUrl nil
:operationsSorter "alpha"}})
(ring/create-default-handler)))) (ring/create-default-handler))))
(defn start [] (defn start []

View file

@ -1,14 +1,17 @@
(ns example.server (ns example.server
(:require [reitit.ring :as ring] (:require [reitit.ring :as ring]
[reitit.coercion.spec]
[reitit.swagger :as swagger] [reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui] [reitit.swagger-ui :as swagger-ui]
[reitit.ring.coercion :as coercion] [reitit.ring.coercion :as coercion]
[reitit.coercion.spec] [reitit.dev.pretty :as pretty]
[reitit.ring.middleware.muuntaja :as muuntaja] [reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.exception :as exception] [reitit.ring.middleware.exception :as exception]
[reitit.ring.middleware.multipart :as multipart] [reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.parameters :as parameters] [reitit.ring.middleware.parameters :as parameters]
[reitit.ring.middleware.dev :as dev] [reitit.ring.middleware.dev :as dev]
[reitit.ring.spec :as spec]
[spec-tools.spell :as spell]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[muuntaja.core :as m] [muuntaja.core :as m]
[clojure.java.io :as io])) [clojure.java.io :as io]))
@ -61,10 +64,15 @@
{:status 200 {:status 200
:body {:total (+ x y)}})}}]]] :body {:total (+ x y)}})}}]]]
{;;:reitit.middleware/transform dev/print-request-diffs {;;:reitit.middleware/transform dev/print-request-diffs ;; pretty diffs
;;:validate spec/validate ;; enable spec validation for route data
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
:exception pretty/exception
:data {:coercion reitit.coercion.spec/coercion :data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance :muuntaja m/instance
:middleware [;; query-params & form-params :middleware [;; swagger feature
swagger/swagger-feature
;; query-params & form-params
parameters/parameters-middleware parameters/parameters-middleware
;; content-negotiation ;; content-negotiation
muuntaja/format-negotiate-middleware muuntaja/format-negotiate-middleware

View file

@ -4,18 +4,18 @@
[io.pedestal.http :as http] [io.pedestal.http :as http]
[reitit.interceptor] [reitit.interceptor]
[reitit.http]) [reitit.http])
(:import (reitit.interceptor Executor))) (:import (reitit.interceptor Executor)
(java.lang.reflect Method)))
(defn- arity [f] (defn- arities [f]
(->> (class f) (->> (class f)
.getDeclaredMethods .getDeclaredMethods
(filter #(= "invoke" (.getName %))) (filter #(= "invoke" (.getName %)))
first (map #(alength (.getParameterTypes ^Method %)))
.getParameterTypes (set)))
alength))
(defn- error-with-arity-1? [{error-fn :error}] (defn- error-without-arity-2? [{error-fn :error}]
(and error-fn (= 1 (arity error-fn)))) (and error-fn (not (contains? (arities error-fn) 2))))
(defn- error-arity-2->1 [error] (defn- error-arity-2->1 [error]
(fn [context ex] (fn [context ex]
@ -26,9 +26,23 @@
(dissoc :error)) (dissoc :error))
context)))) context))))
(defn wrap-error-arity-2->1 [interceptor] (defn- wrap-error-arity-2->1 [interceptor]
(update interceptor :error error-arity-2->1)) (update interceptor :error error-arity-2->1))
(defn ->interceptor [interceptor]
(cond
(interceptor/interceptor? interceptor)
interceptor
(seq (select-keys interceptor [:enter :leave :error]))
(interceptor/interceptor
(if (error-without-arity-2? interceptor)
(wrap-error-arity-2->1 interceptor)
interceptor))))
;;
;; Public API
;;
(def pedestal-executor (def pedestal-executor
(reify (reify
Executor Executor
@ -36,13 +50,7 @@
(->> interceptors (->> interceptors
(map (fn [{:keys [::interceptor/handler] :as interceptor}] (map (fn [{:keys [::interceptor/handler] :as interceptor}]
(or handler interceptor))) (or handler interceptor)))
(map (fn [interceptor] (keep ->interceptor)))
(if (interceptor/interceptor? interceptor)
interceptor
(interceptor/interceptor
(if (error-with-arity-1? interceptor)
(wrap-error-arity-2->1 interceptor)
interceptor)))))))
(enqueue [_ context interceptors] (enqueue [_ context interceptors]
(chain/enqueue context interceptors)))) (chain/enqueue context interceptors))))

View file

@ -0,0 +1,23 @@
(ns reitit.pedestal-test
(:require [clojure.test :refer [deftest testing is]]
[reitit.pedestal :as pedestal]))
(deftest arities-test
(is (= #{0} (#'pedestal/arities (fn []))))
(is (= #{1} (#'pedestal/arities (fn [_]))))
(is (= #{0 1 2} (#'pedestal/arities (fn ([]) ([_]) ([_ _]))))))
(deftest interceptor-test
(testing "wihtout :enter, :leave or :error are stripped"
(is (nil? (pedestal/->interceptor {:name ::kikka}))))
(testing ":error arities are wrapped"
(let [has-2-arity-error? (fn [interceptor]
(-> interceptor
(pedestal/->interceptor)
(:error)
(#'pedestal/arities)
(contains? 2)))]
(is (has-2-arity-error? {:error (fn [_])}))
(is (has-2-arity-error? {:error (fn [_ _])}))
(is (has-2-arity-error? {:error (fn [_ _ _])}))
(is (has-2-arity-error? {:error (fn ([_]) ([_ _]))})))))