Skip to content

Commit

Permalink
Remove defn-curried
Browse files Browse the repository at this point in the history
Fixes #255.
  • Loading branch information
bfontaine authored and JulianBirch committed Mar 1, 2021
1 parent 39396ea commit a646036
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 103 deletions.
43 changes: 22 additions & 21 deletions src/ajax/formats.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
url, json and transit are found in their own files."
(:require [ajax.interceptors :as i]
[ajax.util :as u]
[ajax.protocols :as pr]
#? (:clj [ajax.macros :as m]))
[ajax.protocols :as pr])
#? (:clj (:import [java.io InputStream]
[java.util Scanner])
:cljs (:require-macros [ajax.macros :as m])))
[java.util Scanner])))

(defn raw-response-format
"This will literally return whatever the underlying implementation
Expand Down Expand Up @@ -47,7 +45,7 @@

;;; Detect Response Format

(m/defn-curried get-format [request format-entry]
(defn get-format [request format-entry]
"Converts one of a number of types to a response format.
Note that it processes `[text format]` the same as `format`,
which makes it easier to work with detection vectors such as
Expand All @@ -65,41 +63,44 @@
;;; Must be a format generating function
:else (format-entry request)))

(m/defn-curried get-accept-entries [request format-entry]
(defn get-accept-entries [request format-entry]
(let [fe (if (vector? format-entry)
(first format-entry)
(:content-type (get-format request format-entry)))]
(cond (nil? fe) ["*/*"]
(string? fe) [fe]
:else fe)))

(m/defn-curried content-type-matches
(defn content-type-matches
[^String content-type ^String accept]
(or (= accept "*/*")
(>= (.indexOf content-type accept) 0)))

(m/defn-curried detect-content-type
(defn detect-content-type
[content-type request format-entry]
(let [accept (get-accept-entries request format-entry)]
(some (content-type-matches content-type) accept)))
(some #(content-type-matches content-type %) accept)))

(defn get-default-format
[response {:keys [response-format] :as request}]
(let [f (detect-content-type (u/get-content-type response) request)]
(->> response-format
(filter f)
first
(get-format request))))
(let [content-type (u/get-content-type response)]
(letfn [(accepted-format?
[format-entry]
(detect-content-type content-type request format-entry))]
(->> response-format
(filter accepted-format?)
first
(get-format request)))))

(m/defn-curried detect-response-format-read
[request response]
(let [format (get-default-format response request)]
((:read format) response)))
(defn detect-response-format-read
[request]
(fn detect-response-format [response]
(let [format (get-default-format response request)]
((:read format) response))))

(defn accept-header [{:keys [response-format] :as request}]
(if (vector? response-format)
(mapcat (get-accept-entries request) response-format)
(get-accept-entries request response-format)))
(let [formats (if (vector? response-format) response-format [response-format])]
(mapcat #(get-accept-entries request %) formats)))

(defn detect-response-format [opts]
"NB This version of the response format doesn't have a zero
Expand Down
10 changes: 6 additions & 4 deletions src/ajax/interceptors.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
headers))))
(-process-response [_ xhrio] xhrio))

(m/defn-curried ^:internal uri-with-params [{:keys [vec-strategy params method url-params]} uri]
(defn ^:internal uri-with-params [{:keys [vec-strategy params method url-params]} uri]
"Internal function. Takes a uri and appends the interpretation of the query string to it
matching the behaviour of `url-request-format`."
(if-let [final-url-params (if (and (= method "GET") (nil? url-params))
Expand All @@ -189,9 +189,11 @@
(defrecord ProcessUrlParameters []
Interceptor
(-process-request [_ {:keys [method] :as request}]
(let [if-get-reduce (if (= method "GET") reduced identity)]
(if-get-reduce (update request :uri
(uri-with-params request)))))
(cond->
(update request :uri
(partial uri-with-params request))
(= method "GET")
reduced))
(-process-response [_ response] response))

;;; DirectSubmission is one of the default interceptors.
Expand Down
38 changes: 0 additions & 38 deletions src/ajax/macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,3 @@
(if (keyword? f#)
(apply hash-map ~opts)
f#))))))

(defn- curry
[[params1 params2] body]
(cons (vec params1)
(if (empty? params2)
body
(list (apply list 'fn (vec params2) body)))))

(defn- do-curried [symbol to-fn params]
(let [result (split-with (complement vector?) params)
[[name doc meta] [args & body]] result
[doc meta] (if (string? doc) [doc meta] [nil doc])
body (if meta (cons meta body) body)
arity-for-n #(-> % inc (split-at args) (to-fn body))
arities (->>
(range 0 (count args))
(map arity-for-n)
reverse)
before (keep identity [symbol name doc])]
(concat before arities)))

(defmacro defn-curried
"Builds a multiple arity function similar that returns closures
for the missing parameters, similar to ML's behaviour."
[& params]
(do-curried 'defn curry params))

(defmacro defn-curried-
"Builds a multiple arity function similar that returns closures
for the missing parameters, similar to ML's behaviour."
[& params]
(do-curried 'defn- curry params))

(defmacro fn-curried
"Builds a multiple arity function similar that returns closures
for the missing parameters, similar to ML's behaviour."
[& params]
(do-curried 'fn curry params))
25 changes: 13 additions & 12 deletions src/ajax/simple.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
(defn process-response [response interceptor]
(pr/-process-response interceptor response))

(m/defn-curried js-handler [handler interceptors response]
(let [processed (reduce process-response response interceptors)]
;;; This requires a bit of explanation: if we return a closeable,
;;; it should be wrapping the original response, so we _don't_
;;; close the original response stream
;;; If you're writing a weird interceptor that doesn't do this,
;;; remember to close the original stream yourself
#? (:clj (if (and response
(instance? Closeable (second processed)))
(.close ^Closeable (pr/-body response))))
(handler processed)))
(defn make-js-handler [handler interceptors]
(fn js-handler [response]
(let [processed (reduce process-response response interceptors)]
;;; This requires a bit of explanation: if we return a closeable,
;;; it should be wrapping the original response, so we _don't_
;;; close the original response stream
;;; If you're writing a weird interceptor that doesn't do this,
;;; remember to close the original stream yourself
#?(:clj (if (and response
(instance? Closeable (second processed)))
(.close ^Closeable (pr/-body response))))
(handler processed))))

(defn base-handler [interceptors {:keys [handler]}]
(if handler
(js-handler handler interceptors)
(make-js-handler handler interceptors)
(u/throw-error "No ajax handler provided.")))

(def default-interceptors (atom []))
Expand Down
28 changes: 14 additions & 14 deletions src/ajax/transit.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
(:require [cognitect.transit :as t]
[ajax.interceptors :as i]
[ajax.protocols :as pr]
[ajax.util :as u]
#? (:clj [ajax.macros :as m])))
; Surprisingly, only the clj version needs m/defn-curried
[ajax.util :as u]))

(defn transit-type [{:keys [type]}]
(or type #? (:cljs :json :clj :msgpack)))
Expand All @@ -15,10 +13,11 @@
(t/writer type opts))]
(fn transit-write-params [params]
(t/write writer params))))
:clj (m/defn-curried transit-write-fn
[type opts stream params]
(let [writer (t/writer stream type opts)]
(t/write writer params))))
:clj (defn transit-write-fn
[type opts]
(fn transit-write-params [stream params]
(let [writer (t/writer stream type opts)]
(t/write writer params)))))

(defn transit-request-format
"Returns a Transit request format.
Expand All @@ -41,13 +40,14 @@
(t/reader :json opts))]
(fn transit-read-response [response]
(t/read reader (pr/-body response)))))
:clj (m/defn-curried transit-read-fn [request response]
(let [content-type (u/get-content-type response)
type (if (.contains content-type "msgpack")
:msgpack :json)
stream (pr/-body response)
reader (t/reader stream type request)]
(t/read reader))))
:clj (defn transit-read-fn [request]
(fn transit-read-response [response]
(let [content-type (u/get-content-type response)
type (if (.contains content-type "msgpack")
:msgpack :json)
stream (pr/-body response)
reader (t/reader stream type request)]
(t/read reader)))))

(defn transit-response-format
"Returns a Transit request format.
Expand Down
26 changes: 12 additions & 14 deletions src/ajax/url.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@
of cljs-ajax. Now we have ~50 SLOCs achieving much the same result.
"

#?@ (:clj ((:require
[ajax.macros :as m]
[ajax.util :as u]
[clojure.string :as str]))
:cljs ((:require
[clojure.string :as str]
[ajax.util :as u])
(:require-macros [ajax.macros :as m]))))
#? (:clj (:require
[ajax.util :as u]
[clojure.string :as str])
:cljs (:require
[clojure.string :as str]
[ajax.util :as u])))


(defn- key-encode [key]
Expand All @@ -66,18 +64,18 @@
(defn- key-value-pair-to-str [[k v]]
(str (key-encode k) "=" (value-encode v)))

(m/defn-curried- vec-key-transform-fn [vec-key-encode k v]
(defn- vec-key-transform-fn [vec-key-encode k v]
[(vec-key-encode k) v])

(defn- to-vec-key-transform [vec-strategy]
(let [vec-key-encode (case (or vec-strategy :java)
:java (fn [k] nil) ; no subscript
:rails (fn [k] "") ; [] subscript
:indexed identity)] ; [1] subscript
(vec-key-transform-fn vec-key-encode)))
(partial vec-key-transform-fn vec-key-encode)))


(m/defn-curried- param-to-key-value-pairs [vec-key-transform prefix [key value]]
(defn- param-to-key-value-pairs [vec-key-transform prefix [key value]]
"Takes a parameter and turns it into a sequence of key-value pairs suitable
for passing to `key-value-pair-to-str`. Since we can have nested maps and
vectors, we need a vec-key-transform function and the current query key
Expand All @@ -89,7 +87,7 @@
(str prefix "[" k1 "]")
prefix)
k1)
recurse (param-to-key-value-pairs vec-key-transform new-key)]
recurse (partial param-to-key-value-pairs vec-key-transform new-key)]
(cond
(string? value) ; string is sequential so we have to handle it separately
[[new-key value]] ; ("a" 1) should be ["a" 1]
Expand All @@ -107,7 +105,7 @@

:else [[new-key value]])))

(m/defn-curried params-to-str [vec-strategy params]
(defn params-to-str [vec-strategy params]
"vec-strategy is one of :rails (a[]=3&a[]=4)
:java (a=3&a=4) (this is the correct behaviour and the default)
:indexed (a[3]=1&a[4]=1)
Expand All @@ -121,5 +119,5 @@
"The request format for simple POST and GET."
([] (url-request-format {}))
([{:keys [vec-strategy]}]
{:write (u/to-utf8-writer (params-to-str vec-strategy))
{:write (u/to-utf8-writer (partial params-to-str vec-strategy))
:content-type "application/x-www-form-urlencoded; charset=utf-8"}))

0 comments on commit a646036

Please sign in to comment.