-
Notifications
You must be signed in to change notification settings - Fork 222
Inspectable interceptors #1160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ikitommi
wants to merge
14
commits into
master
Choose a base branch
from
inspectable-interceptors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Inspectable interceptors #1160
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b5f4c4
.
ikitommi ca5f18b
.
ikitommi e4b977f
.
ikitommi 8c61b7f
.
ikitommi f2db311
.
ikitommi ac1e56d
.
ikitommi efa5d59
.
ikitommi 67d3f25
.
ikitommi 2a48bab
.
ikitommi 11f23ff
.
ikitommi 550b60f
fix bb
ikitommi f215011
.
ikitommi c5ad281
.
ikitommi 7ccc5af
.
ikitommi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,38 +16,39 @@ | |
(defn -interceptor | ||
"Utility function to convert input into an interceptor. Works with functions, | ||
map and sequence of those." | ||
[?interceptor schema options] | ||
[?interceptor schema name transformer options] | ||
(cond | ||
|
||
(m/-interceptor? ?interceptor) | ||
?interceptor | ||
|
||
(fn? ?interceptor) | ||
{:enter ?interceptor} | ||
(recur {:enter ?interceptor} schema name transformer options) | ||
|
||
(and (map? ?interceptor) (contains? ?interceptor :compile)) | ||
(let [compiled (::compiled options 0) | ||
options (assoc options ::compiled (inc ^long compiled))] | ||
(when (>= ^long compiled ^long *max-compile-depth*) | ||
(m/-fail! ::too-deep-compilation {:this ?interceptor, :schema schema, :options options})) | ||
(when-let [interceptor (-interceptor ((:compile ?interceptor) schema options) schema options)] | ||
(merge | ||
(dissoc ?interceptor :compile) | ||
interceptor))) | ||
(when-let [interceptor (-interceptor ((:compile ?interceptor) schema options) schema name transformer options)] | ||
(recur (merge (dissoc ?interceptor :compile) interceptor) schema name transformer options))) | ||
|
||
(and (map? ?interceptor) | ||
(or (contains? ?interceptor :enter) | ||
(contains? ?interceptor :leave))) ?interceptor | ||
(contains? ?interceptor :leave))) | ||
(-> ?interceptor (assoc :schema schema) (assoc :transformer transformer) (assoc :name name) (m/-interceptor)) | ||
|
||
(coll? ?interceptor) | ||
(reduce | ||
(fn [{:keys [enter leave]} {new-enter :enter new-leave :leave}] | ||
(let [enter (if (and enter new-enter) #(new-enter (enter %)) (or enter new-enter)) | ||
leave (if (and leave new-leave) #(leave (new-leave %)) (or leave new-leave))] | ||
{:enter enter :leave leave})) | ||
(keep #(-interceptor % schema options) ?interceptor)) | ||
(-interceptor {:enter enter :leave leave} schema name transformer options))) | ||
(keep #(-interceptor % schema name transformer options) ?interceptor)) | ||
|
||
(nil? ?interceptor) nil | ||
|
||
(ifn? ?interceptor) | ||
{:enter ?interceptor} | ||
(ifn? ?interceptor) (recur {:enter ?interceptor} schema name transformer options) | ||
|
||
:else (m/-fail! ::invalid-transformer {:value ?interceptor}))) | ||
|
||
|
@@ -380,56 +381,60 @@ | |
(defn transformer [& ?transformers] | ||
(let [->data (fn [ts default name key] {:transformers ts | ||
:default default | ||
:name name | ||
:keys (when name | ||
(cond-> [[(keyword key) name]] | ||
(not (qualified-keyword? name)) | ||
(conj [(keyword key (clojure.core/name name))])))}) | ||
->eval (fn [x options] (if (map? x) (reduce-kv (fn [x k v] (assoc x k (m/eval v options))) x x) (m/eval x))) | ||
->chain (m/-comp m/-transformer-chain m/-into-transformer) | ||
chain (->> ?transformers (keep identity) (mapcat #(if (map? %) [%] (->chain %))) (vec)) | ||
chain' (->> chain (mapv #(let [name (:name %)] | ||
{:decode (->data (:decoders %) (:default-decoder %) name "decode") | ||
:encode (->data (:encoders %) (:default-encoder %) name "encode")})))] | ||
(when (seq chain) | ||
chain' (->> chain (mapv (fn [{:keys [name decoders encoders default-decoder default-encoder]}] | ||
{:name name | ||
:decode (->data decoders default-decoder name "decode") | ||
:encode (->data encoders default-encoder name "encode")})))] | ||
(when (seq chain') | ||
(reify | ||
m/Transformer | ||
(-transformer-chain [_] chain) | ||
(-value-transformer [_ schema method options] | ||
(-value-transformer [this schema method options] | ||
(reduce | ||
(fn [acc {{:keys [keys default transformers]} method}] | ||
(fn [acc {{:keys [keys name default transformers]} method}] | ||
(let [options (or options (m/options schema)) | ||
from (fn [f] #(some-> (get-in (f schema) %) (->eval options))) | ||
from-properties (some-fn (from m/properties) (from m/type-properties))] | ||
(if-let [?interceptor (or (some from-properties keys) (get transformers (m/type schema)) default)] | ||
(let [interceptor (-interceptor ?interceptor schema options)] | ||
(if (nil? acc) interceptor (-interceptor [acc interceptor] schema options))) | ||
(let [interceptor (-interceptor ?interceptor schema name this options)] | ||
(if (nil? acc) interceptor (-interceptor [acc interceptor] schema [(:name acc) name] this options))) | ||
acc))) nil chain')))))) | ||
|
||
(defn json-transformer | ||
([] (json-transformer nil)) | ||
([{::keys [json-vectors | ||
keywordize-map-keys | ||
map-of-key-decoders] :or {map-of-key-decoders (-string-decoders)}}] | ||
(transformer | ||
{:name :json | ||
:decoders (-> (-json-decoders) | ||
(assoc :map-of {:compile (fn [schema _] | ||
(let [key-schema (some-> schema (m/children) (first))] | ||
(or (some-> key-schema (m/type) map-of-key-decoders | ||
(-interceptor schema {}) (m/-intercepting) | ||
(m/-comp m/-keyword->string) | ||
(-transform-if-valid key-schema) | ||
(-transform-map-keys)) | ||
(-transform-map-keys m/-keyword->string))))}) | ||
(cond-> keywordize-map-keys | ||
(assoc :map {:compile (fn [schema _] | ||
(let [keyword-keys (->> (mu/keys schema) | ||
(filter keyword?) | ||
(map name) | ||
set)] | ||
(-transform-map-keys keyword-keys -string->keyword)))})) | ||
(cond-> json-vectors (assoc :vector -sequential->vector))) | ||
:encoders (-json-encoders)}))) | ||
(let [!this (atom nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's this hack? |
||
this (transformer | ||
{:name :json | ||
:decoders (-> (-json-decoders) | ||
(assoc :map-of {:compile (fn [schema _] | ||
(let [key-schema (some-> schema (m/children) (first))] | ||
(or (some-> key-schema (m/type) map-of-key-decoders | ||
(-interceptor schema @!this :json nil) (m/-intercepting) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some sort of recursion trick? |
||
(m/-comp m/-keyword->string) | ||
(-transform-if-valid key-schema) | ||
(-transform-map-keys)) | ||
(-transform-map-keys m/-keyword->string))))}) | ||
(cond-> keywordize-map-keys | ||
(assoc :map {:compile (fn [schema _] | ||
(let [keyword-keys (->> (mu/keys schema) | ||
(filter keyword?) | ||
(map name) | ||
set)] | ||
(-transform-map-keys keyword-keys -string->keyword)))})) | ||
(cond-> json-vectors (assoc :vector -sequential->vector))) | ||
:encoders (-json-encoders)})] | ||
(reset! !this this)))) | ||
|
||
(defn string-transformer [] | ||
(transformer | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting: Interceptor is private, but map->Interceptor is not