Skip to content

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
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft

Inspectable interceptors #1160

wants to merge 14 commits into from

Conversation

ikitommi
Copy link
Member

test how to inspect interceptor chain:

;; tune the chain so that we get data tap>'ed
((requiring-resolve 'malli.dev/-capture-interceptor))

;; add a tap
(add-tap (fn [x] (clojure.pprint/pprint (update x :schema m/type))))

run an interceptor chain:

(def Address
  [:map
   [:id string?]
   [:tags [:set keyword?]]
   [:address
    [:map
     [:street string?]
     [:city string?]
     [:zip int?]]]])

(m/decode
 Address
 {:id "Lillan",
  :tags ["coffee" "artesan" "garden"],
  :address {:street "Ahlmanintie 29"
            :city "Tampere"
            :zip 33100}}
 (mt/json-transformer))
;{:id "Lillan"
; :tags #{:coffee :artesan :garden}
; :address {:street "Ahlmanintie 29"
;           :city "Tampere"
;           :zip 33100}}

... and see steps tapped:

{:schema :set,
 :name :json,
 :phase :enter,
 :input ["coffee" "artesan" "garden"],
 :output #{"coffee" "garden" "artesan"}}

{:schema keyword?,
 :name :json,
 :phase :enter,
 :input "coffee",
 :output :coffee}

{:schema keyword?,
 :name :json,
 :phase :enter,
 :input "garden",
 :output :garden}

{:schema keyword?,
 :name :json,
 :phase :enter,
 :input "artesan",
 :output :artesan}

{:schema int?
 :name :json
 :phase :enter
 :input 33100
 :output 33100}

:output output
:transformer transformer})
output)))]
(cond-> (m/map->Interceptor interceptor)
Copy link
Member

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

(-transform-map-keys keyword-keys -string->keyword)))}))
(cond-> json-vectors (assoc :vector -sequential->vector)))
:encoders (-json-encoders)})))
(let [!this (atom nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this hack?

(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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some sort of recursion trick?

@opqdonut
Copy link
Member

discussed this with Tommi: needs docs & tests

@opqdonut opqdonut added the WIP work in progress, not ready yet label Apr 15, 2025
@opqdonut opqdonut marked this pull request as draft April 15, 2025 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WIP work in progress, not ready yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants