Skip to content

Commit

Permalink
infer more
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Apr 22, 2024
1 parent 62a12a3 commit eba3d6c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
61 changes: 57 additions & 4 deletions src/compojure/api/meta.clj
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@

(declare static-body?)

(defn- static-form? [&env form]
(static-body? &env [form]))

(defn- static-endpoint? [&env form]
(and (seq? form)
(boolean
Expand Down Expand Up @@ -785,21 +788,71 @@
(when (symbol? sym)
(let [v (resolve &env sym)]
(when (or (= #'when v)
(= #'cond v)
(= #'= v)
(= #'not= v)
(= #'boolean v)
(= sym 'if))
(static-body? &env (next form)))))))))

(defn- var-form? [&env form]
(boolean
(or (and (seq? form)
(= 2 (count form))
(= 'var (first form)))
(when (symbol? form)
(var? (resolve &env form))))))

(defn- static-expansion? [&env form]
(boolean
(when (and (seq? form)
(symbol? (first form))
(not (contains? &env (first form))))
(let [form' (macroexpand-1 form)]
(when-not (identical? form' form)
(static-form? &env form'))))))

(defn- constant-form? [&env form]
(or ((some-fn nil? keyword? number? boolean?) form)
(and (seq? form)
(= 2 (count form))
(= 'quote (first form)))))

(defn- static-binder? [&env bv]
(and (vector? bv)
(even? (count bv))
(every? (fn [[_ init]]
(static-body? &env init))
(partition 2 bv))))

(defn- static-let? [&env body]
(and (seq? body)
(symbol? (first body))
(or (= 'let* (first body))
(let [v (resolve &env (first body))]
(when (var? v)
(contains?
'#{clojure.core/let compojure.api.sweet/let-routes compojure.api.core/let-routes}
(symbol v)))))
(let [[_ bv & body] body]
(and (static-binder? &env bv)
(static-body? &env body)))))

(defn- static-vector? [&env body]
(and (vector? body)
(every? #(static-body? &env %) body)))

(defn- static-body? [&env body]
(every? #(or (static-endpoint? &env %)
(contains? &env %) ;;local
(when (symbol? %)
(var? (resolve &env %))) ;;var deref
((some-fn keyword? number? boolean?) %)
(var-form? &env %)
(constant-form? &env %)
(static-let? &env %)
(static-cond? &env %)
(static-context? &env %)
(static-middleware? &env %)
(static-route-middleware? &env %))
(static-route-middleware? &env %)
(static-expansion? &env %))
body))

(defn restructure [method [path route-arg & args] {:keys [context? &form &env]}]
Expand Down
4 changes: 0 additions & 4 deletions test/compojure/api/swagger_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

(fact "all compojure.api.core macros are interpreted"
(let [app (context "/a" []
:static true ;;FIXME nested static/dynamic?
(routes
(context "/b" []
:dynamic true
(let-routes []
(GET "/c" [] identity)
(POST "/d" [] identity)
Expand Down Expand Up @@ -50,14 +48,12 @@
(fact "route-macros are expanded"
(extract-paths
(context "/api" []
:static true
(optional-routes true (GET "/true" [] identity))
(optional-routes false (PUT "/false" [] identity)))) => {"/api/true" {:get {}}})

(fact "endpoint-macros are expanded"
(extract-paths
(context "/api" []
:static true
(GET+ "/true" [] identity))) => {"/api/xxx/true" {:get {}}})

(fact "Vanilla Compojure defroutes are NOT followed"
Expand Down

0 comments on commit eba3d6c

Please sign in to comment.