From ec39de9b901f875ad7eb786b4e749b157e0e26b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tibor=20Kranj=C4=8Dec?= Date: Tue, 8 Feb 2022 19:43:07 +0100 Subject: [PATCH 1/3] Update core, ctrl, and transit --- .../verybigthings/funicular/controller.cljs | 282 +++++++++++------- src/com/verybigthings/funicular/core.clj | 191 ++++++------ src/com/verybigthings/funicular/transit.cljc | 8 +- 3 files changed, 281 insertions(+), 200 deletions(-) diff --git a/src/com/verybigthings/funicular/controller.cljs b/src/com/verybigthings/funicular/controller.cljs index 63bdb17..c0d17d6 100644 --- a/src/com/verybigthings/funicular/controller.cljs +++ b/src/com/verybigthings/funicular/controller.cljs @@ -4,14 +4,17 @@ [keechma.next.protocols :as keechma-pt] [promesa.core :as p] [cljs.core.async :refer [chan timeout ids id->aliases]}] (->> (fetch/post url (assoc fetch-opts :body {:queries queries})) - (p/map extract-result) - (p/map (fn [{:keys [queries]}] - (doseq [[deferred ids] deferred->ids] - (let [deferred-id->aliases (get id->aliases deferred) - deferred-res (reduce - (fn [acc id] - (let [r (get queries id) - aliases (get deferred-id->aliases id)] - (reduce #(assoc %1 %2 r) acc aliases))) - {} - ids)] - (p/resolve! deferred {:queries deferred-res}))))) - (p/catch (fn [err] - (doseq [deferred (vals deferred->ids)] - (p/reject! deferred err)))))) - -(defn deferred-result-handler [{:keys [command queries] :as res}] + (p/map extract-result) + (p/map (fn [{:keys [queries]}] + (doseq [[deferred ids] deferred->ids] + (let [deferred-id->aliases (get id->aliases deferred) + deferred-res (reduce + (fn [acc id] + (let [r (get queries id) + aliases (get deferred-id->aliases id)] + (reduce #(assoc %1 %2 r) acc aliases))) + {} + ids)] + (p/resolve! deferred {:queries deferred-res}))))) + (p/error (fn [err] + (doseq [deferred (vals deferred->ids)] + (p/reject! deferred err)))))) + +(defn deferred-result-handler [{:keys [queries command] :as res} log-success log-error] (let [[_ command-result] command + + ;; This will be set by the `get-reject-pipeline` function + command-error (::command-error res) + ;; This will be set by the `get-resolve-pipeline` function + command-res (::command-res res) + errored-query-result (reduce-kv (fn [_ _ [_ query-result]] (when (contains? query-result :funicular.anomaly/category) (reduced query-result))) nil - queries)] + queries) + errored-command (contains? command-result :funicular.anomaly/category)] + + (when debug-enabled? + (cond + (or errored-query-result errored-command) + (log-error res) + + command-error + (log-error command-error) + + command-res + (log-success command-res) + + :else + (log-success res))) (cond - (contains? command-result :funicular.anomaly/category) + errored-command (p/rejected (ex-info (:funicular.anomaly/message command-result) command-result)) errored-query-result @@ -132,9 +156,10 @@ :else res))) -(defn deferred-with-result-handler [deferred] +(defn deferred-with-result-handler [deferred log-success log-error] (->> deferred - (p/map deferred-result-handler))) + (p/map (fn [res] + (deferred-result-handler res log-success log-error))))) (def set-conj (fnil conj #{})) @@ -143,10 +168,10 @@ (fn [acc query-alias query] (let [query-id (or (get-in acc [:query->id query]) (keyword (gensym "req-")))] (-> acc - (assoc-in [:query->id query] query-id) - (update-in [:id->aliases deferred query-id] set-conj query-alias) - (update-in [:deferred->ids deferred] set-conj query-id) - (assoc-in [:queries query-id] query)))) + (assoc-in [:query->id query] query-id) + (update-in [:id->aliases deferred query-id] set-conj query-alias) + (update-in [:deferred->ids deferred] set-conj query-id) + (assoc-in [:queries query-id] query)))) queued (:queries payload))) @@ -185,19 +210,23 @@ val (recur (merge-queries queries val)))))) -(defn get-reject-pipeline [command-name err] - (pipeline! [_ ctrl] - (ctrl/broadcast ctrl [:funicular/error command-name] err) - (throw err))) +(defn get-reject-pipeline + ([command-name err] (get-reject-pipeline command-name err {:command [command-name err]})) + ([command-name err payload] + (-> (pipeline! [_ ctrl] + (ctrl/broadcast ctrl [:funicular/error command-name] err) + (throw err)) + (assoc ::command-error payload)))) (defn get-resolve-pipeline [command-name {:keys [command] :as payload}] (let [[_ command-result] command] (if (contains? command-result :funicular.anomaly/category) (let [err (p/rejected (ex-info (:funicular.anomaly/message command-result) command-result))] - (get-reject-pipeline command-name err)) - (pipeline! [_ ctrl] - (ctrl/broadcast ctrl [:funicular/after command-name] {:command (:command payload)}) - payload)))) + (get-reject-pipeline command-name err payload)) + (-> (pipeline! [_ ctrl] + (ctrl/broadcast ctrl [:funicular/after command-name] {:command (:command payload)}) + payload) + (assoc ::command-res payload))))) (defn request-command! [{:funicular/keys [url] :as ctrl} is-called-from-pipeline {:keys [command] :as payload} deferred] (let [[command-name command-payload] command @@ -207,64 +236,64 @@ query-attacher (reify IQueryAttacher (-get-command [_] command-payload) - (attach! [_ payload] + (attach! [_ payload log-success log-error] (when (contains? payload :command) (throw (ex-info "Commands can't be attached to another command" {:payload payload}))) (let [deferred (p/deferred)] (put! query-collector-chan {:payload payload :deferred deferred}) - (deferred-with-result-handler deferred))))] + (deferred-with-result-handler deferred log-success log-error))))] (put! query-collector-chan {:payload payload :deferred deferred}) (ctrl/broadcast ctrl [:funicular/before command-name] query-attacher) (go - (let [{:keys [queries deferred->ids id->aliases] :as collected} (ids id->aliases]} (> (fetch/post url (assoc fetch-opts :body {:queries (or queries {}) :command command})) - (p/map extract-result) - (p/map (fn [{:keys [queries command]}] - (when-not command-has-queries - (if is-called-from-pipeline - (p/resolve! deferred (get-resolve-pipeline command-name {:command command})) - (p/resolve! deferred {:command command}))) - (doseq [[d ids] deferred->ids] - (let [d-id->aliases (get id->aliases d) - d-res (reduce - (fn [acc id] - (let [r (get queries id) - aliases (get d-id->aliases id)] - (reduce #(assoc %1 %2 r) acc aliases))) - {} - ids) - payload {:queries d-res :command command}] - (if (and (= d deferred) is-called-from-pipeline) - (p/resolve! d (get-resolve-pipeline command-name payload)) - (p/resolve! d payload)))))) - (p/catch (fn [err] - (doseq [d (vals deferred->ids)] - (if (and (= d deferred) is-called-from-pipeline) - (p/reject! d (get-reject-pipeline command-name err)) - (p/resolve! d err)))))))))) + (p/map extract-result) + (p/map (fn [{:keys [queries command]}] + (when-not command-has-queries + (if is-called-from-pipeline + (p/resolve! deferred (get-resolve-pipeline command-name {:command command})) + (p/resolve! deferred {:command command}))) + (doseq [[d ids] deferred->ids] + (let [d-id->aliases (get id->aliases d) + d-res (reduce + (fn [acc id] + (let [r (get queries id) + aliases (get d-id->aliases id)] + (reduce #(assoc %1 %2 r) acc aliases))) + {} + ids) + payload {:queries d-res :command command}] + (if (and (= d deferred) is-called-from-pipeline) + (p/resolve! d (get-resolve-pipeline command-name payload)) + (p/resolve! d payload)))))) + (p/error (fn [err] + (doseq [d (vals deferred->ids)] + (if (and (= d deferred) is-called-from-pipeline) + (p/reject! d (get-reject-pipeline command-name err)) + (p/resolve! d err)))))))))) (defmethod ctrl/api ::controller [{:funicular/keys [url] :as ctrl}] (let [query-requester (::query-requester ctrl)] (reify IApi - (-req! [_ payload] + (-req! [_ payload log-success log-error] (if (:command payload) (let [deferred (p/deferred) - is-called-from-pipeline (in-pipeline?) ] + is-called-from-pipeline (in-pipeline?)] (request-command! ctrl is-called-from-pipeline payload deferred) (if is-called-from-pipeline - (deferred-with-result-handler deferred) + (deferred-with-result-handler deferred log-success log-error) (let [deferred-with-hijacked (->> deferred - deferred-with-result-handler - (hijack-command-deferred-then-and-catch ctrl payload))] + (deferred-with-result-handler log-success log-error) + (hijack-command-deferred-then-and-catch ctrl payload))] ;; Force at least one on-fulfilled and on-rejected handlers so we're ;; sure code in `hijack-command-deferred-then-and-catch` is called (->> deferred-with-hijacked - (p/map identity) - (p/catch identity)) + (p/map identity) + (p/error identity)) deferred-with-hijacked))) (let [deferred (p/deferred)] (queue-request query-requester payload deferred) - (deferred-with-result-handler deferred))))))) + (deferred-with-result-handler deferred log-success log-error))))))) (defmethod ctrl/init ::controller [ctrl] (let [query-requester (make-query-requester ctrl)] @@ -278,7 +307,7 @@ (install app {})) ([app config] (assoc-in app [:keechma/controllers ::controller] - (merge default-config config)))) + (merge default-config config)))) (defn command ([command-name payload] @@ -313,36 +342,79 @@ (let [[_ res] (get queries query-alias)] (or res default)))) -(defn req! [{:keechma/keys [app]} payload] +(defn -internal-req! [{:keechma/keys [app]} payload log-success log-error] (let [api* (keechma-pt/-get-api* app ::controller)] (if-let [api @api*] - (-req! api payload) + (-req! api payload log-success log-error) ;; Handle the case where this controller is started after the calling controller ;; because both might be without deps, which means that the order is nondeterministic (let [deferred (p/deferred)] - (js/setTimeout #(p/resolve! deferred (-req! @api* payload)) 1) + (js/setTimeout #(p/resolve! deferred (-req! @api* payload log-success log-error)) 1) deferred)))) +(defn req! [ctrl payload] + (let [log-success (fn [res] + (l/group-collapsed "%c[REQ]" "color: green" payload) + (l/log "[RES]" res) + (l/group-end)) + log-error (fn [err] + (l/group-collapsed "%c[REQ]" "color: red" payload) + (l/log "[REQ]" payload) + (l/log "[ERR]" err) + (l/group-end))] + (-internal-req! ctrl payload log-success log-error))) + (defn query! ([ctrl query-name payload] (query! ctrl query-name payload nil)) ([ctrl query-name payload default] - (->> (req! ctrl (query query-name payload)) - (p/map #(get-query % query-name default))))) + (let [log-success (fn [res] + (l/group-collapsed (str "%c[QUERY] " query-name) "color: green") + (l/log "[REQ]" payload) + (l/log "[RES]" (get-query res query-name default)) + (l/group-end)) + log-error (fn [err] + (l/group-collapsed (str "%c[QUERY] " query-name) "color: red") + (l/log "[REQ]" payload) + (l/log "[ERR]" err) + (l/group-end))] + (->> (-internal-req! ctrl (query query-name payload) log-success log-error) + (p/map #(get-query % query-name default)))))) (defn command! ([ctrl command-name payload] (command! ctrl command-name payload nil)) ([ctrl command-name payload default] - (->> (req! ctrl (command command-name payload)) - (p/map (fn [res] - (if (pipeline? res) - (update-in res [:pipeline :begin] conj (fn [value _] (get-command value default))) - (get-command res default))))))) + (let [log-success (fn [res] + (l/group-collapsed (str "%c[COMMAND] " command-name) "color: green") + (l/log "[REQ]" payload) + (l/log "[RES]" (get-command res default)) + (l/group-end)) + log-error (fn [err] + (l/group-collapsed (str "%c[COMMAND] " command-name) "color: red") + (l/log "[REQ]" payload) + (l/log "[ERR]" err) + (l/group-end)) + req (-internal-req! ctrl (command command-name payload) log-success log-error)] + (->> req + (p/map (fn [res] + (if (pipeline? res) + (update-in res [:pipeline :begin] conj (fn [value _] (get-command value default))) + (get-command res default)))))))) (defn attach-query! ([value query-name payload] (attach-query! value query-name payload nil)) ([value query-name payload default] - (->> (attach! value (query query-name payload)) - (p/map #(get-query % query-name default))))) + (let [log-success (fn [res] + (l/group-collapsed (str "%c[ATTACHED QUERY] " query-name) "color: green") + (l/log "[REQ]" payload) + (l/log "[RES]" (get-query res query-name default)) + (l/group-end)) + log-error (fn [err] + (l/group-collapsed (str "%c[ATTACHED QUERY] " query-name) "color: red") + (l/log "[REQ]" payload) + (l/log "[ERR]" err) + (l/group-end))] + (->> (attach! value (query query-name payload) log-success log-error) + (p/map #(get-query % query-name default)))))) \ No newline at end of file diff --git a/src/com/verybigthings/funicular/core.clj b/src/com/verybigthings/funicular/core.clj index a2cb57b..6cbdcd1 100644 --- a/src/com/verybigthings/funicular/core.clj +++ b/src/com/verybigthings/funicular/core.clj @@ -8,22 +8,23 @@ [sieppari.context :as sic] [clojure.string :as str] [com.verybigthings.funicular.anomalies :as anom] - [clojure.pprint])) + [clojure.pprint] + [duct.logger :refer [log]])) (defn deep-merge-malli-errors [a b] (merge-with (fn [x y] (cond (map? y) (deep-merge-malli-errors x y) (vector? y) (concat x y) :else y)) - a b)) + a b)) (defn interceptor-map? [val] (and map? (seq (set/intersection - (-> val keys set) - #{:enter - :leave - :error})))) + (-> val keys set) + #{:enter + :leave + :error})))) (s/def :com.verybigthings.funicular.core.interceptor/enter fn?) (s/def :com.verybigthings.funicular.core.interceptor/leave fn?) @@ -44,24 +45,24 @@ (s/and map? (s/keys - :opt-un [::rules - ::interceptors] - :req-un [::handler - ::input-schema - ::output-schema]))) + :opt-un [::rules + ::interceptors] + :req-un [::handler + ::input-schema + ::output-schema]))) (s/def ::rule (s/or - :fn fn? - :and (s/cat - :op #(= % :and) - :rules (s/+ ::rule)) - :or (s/cat - :op #(= % :or) - :rules (s/+ ::rule)) - :not (s/cat - :op #(= % :not) - :rule ::rule))) + :fn fn? + :and (s/cat + :op #(= % :and) + :rules (s/+ ::rule)) + :or (s/cat + :op #(= % :or) + :rules (s/+ ::rule)) + :not (s/cat + :op #(= % :not) + :rule ::rule))) (s/def ::rules ::rule) @@ -78,26 +79,26 @@ (s/and map? (s/keys - :opt-un [::interceptors - ::input-schema - ::rules - ::queries - ::commands]))) + :opt-un [::interceptors + ::input-schema + ::rules + ::queries + ::commands]))) (s/def ::api-context-name (s/and simple-keyword? (s/or - :anon #{:<>} - :named any?))) + :anon #{:<>} + :named any?))) (s/def ::api (s/and vector? (s/cat - :name ::api-context-name - :props (s/? ::context-props) - :api-subcontexts (s/* ::api)))) + :name ::api-context-name + :props (s/? ::context-props) + :api-subcontexts (s/* ::api)))) (s/def ::context map?) @@ -112,15 +113,15 @@ (s/and map? (s/keys - :req-un [::api] - :opt-un [::pipes]))) + :req-un [::api] + :opt-un [::pipes]))) (s/def :com.verybigthings.funicular.request/query (s/tuple keyword? any?)) (s/def :com.verybigthings.funicular.request/queries (s/map-of - keyword? :com.verybigthings.funicular.request/query)) + keyword? :com.verybigthings.funicular.request/query)) (s/def :com.verybigthings.funicular.request/command (s/tuple keyword? any?)) @@ -129,8 +130,8 @@ (s/and map? (s/keys - :opt-un [:com.verybigthings.funicular.request/command - :com.verybigthings.funicular.request/queries]))) + :opt-un [:com.verybigthings.funicular.request/command + :com.verybigthings.funicular.request/queries]))) (def FunicularAnomaly [:map @@ -148,15 +149,20 @@ error) error) -(def root-error-interceptor - {:error (fn [{:keys [error] :as ctx}] - (let [data (ex-data error) +(defn make-root-error-interceptor [{:keys [logger]}] + {:error (fn [{:keys [request error] :as ctx}] + (let [data (ex-data error) response (if (contains? data :funicular.anomaly/category) data (anom/internal-error (ex-message error)))] + (log logger :error :funicular.request/error error) (-> ctx - (dissoc :error) - (assoc :response (sanitize-error-keys response)))))}) + (dissoc :error) + (assoc :response (sanitize-error-keys response)))))}) + +(defn make-root-nil-interceptor [namespaced-resolver-name] + {:leave (fn [ctx] + (update ctx :response #(or % (anom/unavailable (str namespaced-resolver-name " returned no data")))))}) (defmulti enforce-rule (fn [_ [op & _]] op)) @@ -186,11 +192,6 @@ (recur (rest rules)))) false))) - -(defn log [arg] - (clojure.pprint/pprint arg) - arg) - (defn with-context-name [acc {[context-type context-name] :name}] (if (= :named context-type) (update acc :path conj context-name) @@ -206,10 +207,10 @@ (humanize acc explanation nil)) ([acc {:keys [value errors]} {f :wrap :or {f :message} :as options}] (if errors - (if (coll? value) + (when (coll? value) (reduce (fn [acc error] - (let [error-path (me/error-path error options) + (let [error-path (me/error-path error options) error-path' (if (seq error-path) error-path :funicular/errors)] (update acc error-path' set-conj (f (me/with-error-message error options))))) acc @@ -222,22 +223,22 @@ (defn schemas->validator-explainer [schemas opts] (let [ves (mapv - (fn [s] {:explainer (m/explainer s opts) - :validator (m/validator s opts)}) - schemas)] + (fn [s] {:explainer (m/explainer s opts) + :validator (m/validator s opts)}) + schemas)] (fn [data] (let [errors (reduce (fn [acc {:keys [explainer validator]}] (if (validator data) acc (->> data - explainer - (humanize acc)))) + explainer + (humanize acc)))) {} ves)] (->> errors - (mapv (fn [[k v]] [k (-> v sort vec)])) - (into {})))))) + (mapv (fn [[k v]] [k (-> v sort vec)])) + (into {})))))) (defn with-input-schema-interceptor [{:keys [input-schemas] :as acc} {:malli/keys [registry]}] (if (seq input-schemas) @@ -246,7 +247,7 @@ interceptor {:enter (fn [ctx] - (let [data (get-in ctx [:request :data]) + (let [data (get-in ctx [:request :data]) errors (validator-explainer data)] (if (empty? errors) ctx @@ -265,7 +266,7 @@ {:leave (fn [{:keys [response] :as ctx}] (if (funiculary-anomaly-validator response) ctx - (let [data (get-in ctx [:response]) + (let [data (get-in ctx [:response]) errors (validator-explainer data)] (if (empty? errors) ctx @@ -275,8 +276,7 @@ (update acc :interceptors conj interceptor)) acc)) - -(defn with-schema [acc {:keys [input-schema output-schema]} opts] +(defn with-schema [acc {:keys [input-schema output-schema]} _] (cond-> acc input-schema (update :input-schemas conj input-schema) @@ -290,7 +290,7 @@ (keyword resolver-ns (name resolver-name))) resolver-name)) -(defn with-rules [acc {:keys [rules]} opts] +(defn with-rules [acc {:keys [rules]} _] (let [interceptor (fn [{:keys [request] :as ctx}] (if rules (if (enforce-rule request rules) @@ -304,18 +304,20 @@ (fn [acc' resolver-name {:keys [handler] :as resolver}] (let [{:keys [interceptors input-schemas output-schemas]} (-> acc - (with-rules resolver opts) - (with-interceptors resolver) - (with-schema resolver opts) - (with-input-schema-interceptor opts) - (update :interceptors conj handler) - (with-output-schema-interceptor opts)) + (with-rules resolver opts) + (with-interceptors resolver) + (with-schema resolver opts) + (with-input-schema-interceptor opts) + (update :interceptors conj handler) + (with-output-schema-interceptor opts)) namespaced-resolver-name (make-namespaced-resolver-name (:path acc) resolver-name)] (when (get-in acc' [:resolvers namespaced-resolver-name]) (throw (ex-info "Duplicate resolver" {:error ::duplicate-resolver :resolver namespaced-resolver-name}))) - (assoc-in acc' [:resolvers namespaced-resolver-name] {:chain (into [root-error-interceptor] interceptors) + (assoc-in acc' [:resolvers namespaced-resolver-name] {:chain (into [(make-root-nil-interceptor namespaced-resolver-name) + (make-root-error-interceptor opts)] + interceptors) :input-schemas input-schemas :output-schemas output-schemas :input-schema (last input-schemas) @@ -341,15 +343,15 @@ ([context opts] (compile-api {:path [] :interceptors [] :input-schemas [] :output-schemas []} context opts)) ([acc {:keys [props] :as context} opts] (-> acc - (with-context-name context) - (with-rules props opts) - (with-schema props opts) - (with-interceptors props) - (with-resolvers :commands props opts) - (with-resolvers :queries props opts) - (with-api-subcontexts context opts)))) - -(defn compile-pipes [pipes resolvers opts] + (with-context-name context) + (with-rules props opts) + (with-schema props opts) + (with-interceptors props) + (with-resolvers :commands props opts) + (with-resolvers :queries props opts) + (with-api-subcontexts context opts)))) + +(defn compile-pipes [pipes resolvers _] (reduce-kv (fn [acc source->target pipe] (let [[source target] source->target] @@ -381,7 +383,8 @@ (defn execute-command [acc compiled context {:keys [command]}] (if command (let [[command-name command-data] command - chain (get-in compiled [:resolvers command-name :chain]) + resolver (get-in compiled [:resolvers command-name]) + chain (when (= :command (:type resolver)) (:chain resolver)) res (if chain (si/execute chain (assoc context :data command-data :command command-name)) (anom/not-found "Command not found" (make-missing-command-error command-name command-data)))] @@ -393,21 +396,23 @@ :data query-data :funicular.anomaly/subcategory :funicular.anomaly.category.not-found/query}) -;; TODO queries should return errors when command returns error (defn execute-queries [{:keys [command] :as acc} compiled context {:keys [queries]}] (let [[command-name command-res] command] (reduce-kv (fn [acc' query-alias [query-name query-data]] - (let [chain (get-in compiled [:resolvers query-name :chain])] + (let [resolver (get-in compiled [:resolvers query-name]) + chain (when (= :query (:type resolver)) (:chain resolver))] (if chain - (let [pipe (get-in compiled [:pipes [command-name query-name]]) - context' (-> (if command - (assoc context :command {:name command-name :response command-res}) - context) - (assoc :query query-name)) - chain' (if pipe (into [pipe] chain) chain) - res (si/execute chain' (assoc context' :data query-data))] - (assoc-in acc' [:queries query-alias] [query-name res])) + (if (contains? command-res :funicular.anomaly/category) + (assoc-in acc' [:queries query-alias] [query-name (anom/internal-error (str "Command " command-name " failed"))]) + (let [pipe (get-in compiled [:pipes [command-name query-name]]) + context' (-> (if command + (assoc context :command {:name command-name :response command-res}) + context) + (assoc :query query-name)) + chain' (if pipe (into [pipe] chain) chain) + res (si/execute chain' (assoc context' :data query-data))] + (assoc-in acc' [:queries query-alias] [query-name res]))) (assoc-in acc' [:queries query-alias] [query-name (anom/not-found "Query not found" (make-missing-query-error query-name query-data))])))) acc queries))) @@ -415,12 +420,12 @@ (defn execute [compiled context request] (s/assert :com.verybigthings.funicular/request request) (-> {} - (execute-command compiled context request) - (execute-queries compiled context request))) + (execute-command compiled context request) + (execute-queries compiled context request))) (defn inspect [compiled] (->> compiled - :resolvers - (map (fn [[k v]] [k (select-keys v [:input-schemas :input-schema :output-schemas :output-schema :type])])) - (sort-by (fn [[k _]] (str k))) - vec)) \ No newline at end of file + :resolvers + (map (fn [[k v]] [k (select-keys v [:input-schemas :input-schema :output-schemas :output-schema :type])])) + (sort-by (fn [[k _]] (str k))) + vec)) \ No newline at end of file diff --git a/src/com/verybigthings/funicular/transit.cljc b/src/com/verybigthings/funicular/transit.cljc index 2fc4bfe..0bcff16 100644 --- a/src/com/verybigthings/funicular/transit.cljc +++ b/src/com/verybigthings/funicular/transit.cljc @@ -2,6 +2,7 @@ "Connect time-literals to transit." (:require [time-literals.read-write] [cognitect.transit :as transit] + #?(:cljs [com.cognitect.transit.types :as ty]) #?(:cljs [java.time :refer [Period LocalDate LocalDateTime @@ -31,6 +32,9 @@ Duration Year YearMonth)))) +#?(:cljs + (extend-type ty/UUID + IUUID)) (def time-classes {'period Period @@ -51,8 +55,8 @@ (def write-handlers {:handlers (into {} - (for [[tick-class host-class] time-classes] - [host-class (transit/write-handler (constantly (name tick-class)) str)]))}) + (for [[tick-class host-class] time-classes] + [host-class (transit/write-handler (constantly (name tick-class)) str)]))}) (def read-handlers {:handlers From 2bbfef3327a27f1a1538da11078e90ad2df4bdd3 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 21 Jul 2022 16:10:52 +0200 Subject: [PATCH 2/3] Fix formating --- .../verybigthings/funicular/anomalies.cljc | 2 +- .../verybigthings/funicular/controller.cljs | 74 +++++++++---------- src/com/verybigthings/funicular/transit.cljc | 2 +- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/com/verybigthings/funicular/anomalies.cljc b/src/com/verybigthings/funicular/anomalies.cljc index aad0f59..d768a28 100644 --- a/src/com/verybigthings/funicular/anomalies.cljc +++ b/src/com/verybigthings/funicular/anomalies.cljc @@ -23,4 +23,4 @@ (defn ->ex-info ([anomaly] (->ex-info anomaly nil)) ([anomaly cause] - (ex-info (:funicular.anomaly/message anomaly) anomaly cause))) \ No newline at end of file + (ex-info (:funicular.anomaly/message anomaly) anomaly cause))) diff --git a/src/com/verybigthings/funicular/controller.cljs b/src/com/verybigthings/funicular/controller.cljs index c0d17d6..3d1891e 100644 --- a/src/com/verybigthings/funicular/controller.cljs +++ b/src/com/verybigthings/funicular/controller.cljs @@ -34,9 +34,9 @@ ctrl (fn [] (let [fulfilled (reduce - (fn [m f] (assoc m f (f payload))) - {} - (:on-fulfilled state))] + (fn [m f] (assoc m f (f payload))) + {} + (:on-fulfilled state))] (vswap! state* assoc :committed? true :fulfilled fulfilled) (ctrl/broadcast ctrl [:funicular/after command-name] {:command command}) (get fulfilled on-fulfilled)))))))))) @@ -51,9 +51,9 @@ ctrl (fn [] (let [rejected (reduce - (fn [m f] (assoc m f (f payload))) - {} - (:on-rejected state))] + (fn [m f] (assoc m f (f payload))) + {} + (:on-rejected state))] (vswap! state* assoc :committed? true :rejected rejected) (ctrl/broadcast ctrl [:funicular/error command-name] payload) (get rejected on-rejected))))))))) @@ -106,12 +106,12 @@ (doseq [[deferred ids] deferred->ids] (let [deferred-id->aliases (get id->aliases deferred) deferred-res (reduce - (fn [acc id] - (let [r (get queries id) - aliases (get deferred-id->aliases id)] - (reduce #(assoc %1 %2 r) acc aliases))) - {} - ids)] + (fn [acc id] + (let [r (get queries id) + aliases (get deferred-id->aliases id)] + (reduce #(assoc %1 %2 r) acc aliases))) + {} + ids)] (p/resolve! deferred {:queries deferred-res}))))) (p/error (fn [err] (doseq [deferred (vals deferred->ids)] @@ -127,11 +127,11 @@ errored-query-result (reduce-kv - (fn [_ _ [_ query-result]] - (when (contains? query-result :funicular.anomaly/category) - (reduced query-result))) - nil - queries) + (fn [_ _ [_ query-result]] + (when (contains? query-result :funicular.anomaly/category) + (reduced query-result))) + nil + queries) errored-command (contains? command-result :funicular.anomaly/category)] (when debug-enabled? @@ -165,15 +165,15 @@ (defn merge-queries [queued {:keys [payload deferred]}] (reduce-kv - (fn [acc query-alias query] - (let [query-id (or (get-in acc [:query->id query]) (keyword (gensym "req-")))] - (-> acc - (assoc-in [:query->id query] query-id) - (update-in [:id->aliases deferred query-id] set-conj query-alias) - (update-in [:deferred->ids deferred] set-conj query-id) - (assoc-in [:queries query-id] query)))) - queued - (:queries payload))) + (fn [acc query-alias query] + (let [query-id (or (get-in acc [:query->id query]) (keyword (gensym "req-")))] + (-> acc + (assoc-in [:query->id query] query-id) + (update-in [:id->aliases deferred query-id] set-conj query-alias) + (update-in [:deferred->ids deferred] set-conj query-id) + (assoc-in [:queries query-id] query)))) + queued + (:queries payload))) (defn make-query-requester [ctrl] (let [in-chan (chan)] @@ -214,8 +214,8 @@ ([command-name err] (get-reject-pipeline command-name err {:command [command-name err]})) ([command-name err payload] (-> (pipeline! [_ ctrl] - (ctrl/broadcast ctrl [:funicular/error command-name] err) - (throw err)) + (ctrl/broadcast ctrl [:funicular/error command-name] err) + (throw err)) (assoc ::command-error payload)))) (defn get-resolve-pipeline [command-name {:keys [command] :as payload}] @@ -224,8 +224,8 @@ (let [err (p/rejected (ex-info (:funicular.anomaly/message command-result) command-result))] (get-reject-pipeline command-name err payload)) (-> (pipeline! [_ ctrl] - (ctrl/broadcast ctrl [:funicular/after command-name] {:command (:command payload)}) - payload) + (ctrl/broadcast ctrl [:funicular/after command-name] {:command (:command payload)}) + payload) (assoc ::command-res payload))))) (defn request-command! [{:funicular/keys [url] :as ctrl} is-called-from-pipeline {:keys [command] :as payload} deferred] @@ -256,12 +256,12 @@ (doseq [[d ids] deferred->ids] (let [d-id->aliases (get id->aliases d) d-res (reduce - (fn [acc id] - (let [r (get queries id) - aliases (get d-id->aliases id)] - (reduce #(assoc %1 %2 r) acc aliases))) - {} - ids) + (fn [acc id] + (let [r (get queries id) + aliases (get d-id->aliases id)] + (reduce #(assoc %1 %2 r) acc aliases))) + {} + ids) payload {:queries d-res :command command}] (if (and (= d deferred) is-called-from-pipeline) (p/resolve! d (get-resolve-pipeline command-name payload)) @@ -417,4 +417,4 @@ (l/log "[ERR]" err) (l/group-end))] (->> (attach! value (query query-name payload) log-success log-error) - (p/map #(get-query % query-name default)))))) \ No newline at end of file + (p/map #(get-query % query-name default)))))) diff --git a/src/com/verybigthings/funicular/transit.cljc b/src/com/verybigthings/funicular/transit.cljc index 0bcff16..e1352b1 100644 --- a/src/com/verybigthings/funicular/transit.cljc +++ b/src/com/verybigthings/funicular/transit.cljc @@ -76,4 +76,4 @@ #?(:clj (let [in (ByteArrayInputStream. (.getBytes json)) reader (transit/reader in :json read-handlers)] (transit/read reader)) - :cljs (transit/read (transit/reader :json read-handlers) json))) \ No newline at end of file + :cljs (transit/read (transit/reader :json read-handlers) json))) From ca0cb32586d512a37da8d82a8caa9064deeb5188 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 21 Jul 2022 16:22:32 +0200 Subject: [PATCH 3/3] Remove dependency on duct logger --- src/com/verybigthings/funicular/core.clj | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/com/verybigthings/funicular/core.clj b/src/com/verybigthings/funicular/core.clj index 252a920..1f5824e 100644 --- a/src/com/verybigthings/funicular/core.clj +++ b/src/com/verybigthings/funicular/core.clj @@ -8,8 +8,7 @@ [sieppari.context :as sic] [clojure.string :as str] [com.verybigthings.funicular.anomalies :as anom] - [clojure.pprint] - [duct.logger :refer [log]])) + [clojure.pprint])) (defn deep-merge-malli-errors [a b] (merge-with (fn [x y] @@ -149,13 +148,12 @@ error) error) -(defn make-root-error-interceptor [{:keys [logger]}] - {:error (fn [{:keys [request error] :as ctx}] +(defn make-root-error-interceptor [_] + {:error (fn [{:keys [_request error] :as ctx}] (let [data (ex-data error) response (if (contains? data :funicular.anomaly/category) data (anom/internal-error (ex-message error)))] - (log logger :error :funicular.request/error error) (-> ctx (dissoc :error) (assoc :response (sanitize-error-keys response)))))})