Fork me on GitHub
#malli
<
2022-10-08
>
skynet19:10:41

how do I use the function generated from :malli/gen true in function metadata <https://github.com/metosin/malli/blob/master/docs/function-schemas.md#function-schema-metadata>? the doc says "Setting :malli/gen to true while function body generation is enabled with mi/instrument! allows body to be generated, to return valid generated data." for example

(require '[malli.core :as m])
(defn pow 
  {:malli/schema [:=> [:cat :int] :int]
   :malli/gen true} ; how do I find/use this generated function body?
  [x] 
  (* x x))

(require 'malli.dev)
(malli.dev/start!)
(pow 1)
;1

dvingo13:10:54

You have to pass in malli.generator/generate to the start! call. there is an example under this section: https://github.com/metosin/malli/blob/master/docs/function-schemas.md#instrumentation ("With :gen we can omit the function body"..)

(defn pow {:malli/schema [:=> [:cat :int] :int] :malli/gen true} [x] (* x x))
(defn pow2 {:malli/schema [:=> [:cat :int] :int] :malli/gen false} [x] (* x x))
(md/start! {:gen malli.generator/generate})
(comment
  (pow 45)
  ; => random int

  (pow2 45)
  ; => 2025
  )

skynet16:10:24

@U051V5LLP perfect! just what I needed. didn't put it together to use the same argument to start!, thanks

skynet17:10:13

oh and one missing piece: I now need to add :malli/gen false to the defn metadata in order to disable gen for specific functions when doing this, but this might work out

dvingo20:10:58

oh yea, I didn't realize that you have to opt out - another idea is to use the :filters option to target only those that you want to gen

👍 1