Skip to content

Commit

Permalink
msg
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Apr 22, 2024
1 parent 53224ff commit 3db883b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
[org.immutant/immutant "2.1.10"]
[http-kit "2.3.0"]
[criterium "0.4.5"]]
:jvm-opts ["-Dcompojure.api.meta.static-context-coach={:default :assert}"]
:test-paths ["test19"]
:ring {:handler examples.thingie/app
:reload-paths ["src" "examples/thingie/src"]}
Expand Down
51 changes: 38 additions & 13 deletions src/compojure/api/meta.clj
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,12 @@
(defn- route-args? [arg]
(not= arg []))

(def endpoint-vars (conj (into #{}
(mapcat (fn [n]
(map #(symbol (name %) (name n))
'[compojure.api.core
compojure.api.sweet])))
'[GET ANY HEAD PATCH DELETE OPTIONS POST PUT])
'compojure.api.sweet/resource
'compojure.api.resource/resource))
(def endpoint-vars (into #{}
(mapcat (fn [n]
(map #(symbol (name %) (name n))
'[compojure.api.core
compojure.api.sweet])))
'[GET ANY HEAD PATCH DELETE OPTIONS POST PUT]))

(def routes-vars #{'compojure.api.sweet/routes
'compojure.api.core/routes})
Expand All @@ -688,6 +686,26 @@
(and (routes-vars sym)
(static-body? &env (next form))))))))))))

(def resource-vars '#{compojure.api.sweet/resource
compojure.api.resource/resource})

(defn- static-resource? [&env form]
(and (seq? form)
(boolean
(let [sym (first form)]
(when (symbol? sym)
(when-some [v (resolve &env sym)]
(when (var? v)
(let [sym (symbol v)]
(when (and (resource-vars sym)
(= 1 (count form)))
(let [[_ data] form]
;; TODO only needs to be static in a few places.
;; is it enough to just test for map?.
(or (static-form? data)
(and (map? data)
()))))))))))))

(def context-vars (into #{}
(mapcat (fn [n]
(map #(symbol (name %) (name n))
Expand Down Expand Up @@ -898,8 +916,14 @@
(-> info :public :static)))
"Cannot be both a :dynamic and :static context.")

static? (not (or (-> info :public :dynamic)
(route-args? route-arg) (seq lets) (seq letks)))
bindings? (boolean (or (route-args? route-arg) (seq lets) (seq letks)))

_ (assert (not (and (-> info :public :static)
bindings?))
"A context cannot be :static and also provide bindings. Either push bindings into endpoints or remove :static.")

static? (not (or (-> info :public :dynamic) bindings?))

safely-static (or (-> info :public :static)
(try (static-body? &env body)
(catch Exception e
Expand Down Expand Up @@ -928,16 +952,17 @@
"use (context ... :static true ...)."
"\n\n"
"To suppress this message for this namespace use -Dcompojure.api.meta.static-context-coach="
"{" nsym :off "}"
"{" nsym " " :off "}"
(when coach
(str "\n\nCurrent coach config: " (pr-str coach))))]
(case coach
(case mode
:off nil
:print (println msg)
:assert (throw (ex-info msg
{:form &form
:meta (meta &form)}))
(throw (ex-info "compojure.api.meta.static-context-coach must be either :off, :print, or :assert" {:provided coach})))))))
(throw (ex-info "compojure.api.meta.static-context-coach mode must be either :off, :print, or :assert" {:coach coach
:provided mode})))))))

;; :dynamic by default
static-context? (and static? context? (boolean safely-static))
Expand Down
2 changes: 1 addition & 1 deletion test/compojure/api/swagger_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(fact "all compojure.api.core macros are interpreted"
(let [app (context "/a" []
(routes
(context "/b" []
(context "/b" a
(let-routes []
(GET "/c" [] identity)
(POST "/d" [] identity)
Expand Down

0 comments on commit 3db883b

Please sign in to comment.