Skip to content

Protocol function instrumentation #555

@piotr-yuxuan

Description

@piotr-yuxuan

Hello, thank you very much once again for this great tool.

The doc for function schema instrumentation (link) mentions defn and doesn't refer to protocol functions so I guess the behaviour below isn't a bug, but more a feature request.

(defprotocol Backend
  (upsert [this data])
  (delete [this id])
  (query [this f]))

(m/=> upsert
      [:=>
       [:cat Backend [:map [:id int?]]]
       [:map [:id int?]]])

(m/=> delete
      [:=>
       [:cat Backend int?]
       [:enum :done]])

(m/=> query
      [:=>
       [:cat Backend ifn?]
       [:sequential [:map [:id int?]]]])

(defrecord InMemoryBackend [state]
  Backend
  (upsert [_ {:keys [id] :as data}] (swap! state assoc id data))
  (delete [_ id] (swap! state dissoc id) :done)
  (query [_ f] (->> @state vals (filter f))))

(mi/instrument!)

(doto (InMemoryBackend. (atom {}))
  (upsert {:id 1}))
=> (atom {1 {:id 1}})

;; Not the expected outcome
(doto (InMemoryBackend. (atom {}))
  (upsert :should-fail))
=> (atom {nil :should-fail})

Would you consider and review a PR that would extend malli and add support for protocol function instrumentation? Any thoughts about it off the top of your mind?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Not right now

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions