Fork me on GitHub
#malli
<
2021-11-11
>
Yehonathan Sharvit07:11:30

Is there a way to create a custom Malli tag that will allow me to write:

[:record
   [:user-id :string]
   [:num-of-purchases :int]]
And it would be the same as:
[:and
 [:map-of :keyword :any]
 [:map
   [:user-id :string]
   [:num-of-purchases :int]]
I need this because for the moment JSON decoder doesn't convert :map key strings to keywords https://github.com/metosin/malli/issues/568

Ben Sless07:11:57

Why not add your own transformer?

juhoteperi08:11:22

@U0L91U7A8 you could use the json-transformer version I posted on the issue. (You might want to combine with :map-of code from the Malli.transform version)

juhoteperi08:11:47

But yeah if you create your own registry, you could define :record as :map-of + :map

Yehonathan Sharvit09:11:41

How it would look like?

Nikolas Pafitis20:11:30

Does generate cache the generator?

respatialized21:11:51

mg/generator is what you're looking for I think

Nikolas Pafitis21:11:28

Yes but if i skip the mg/generator, generate will create a generator, is that generator cached?

ikitommi18:11:14

mg/generator is still bit faster, as there is no cache lookup. Caching is done on Schema instance level, so calling generate with just hiccup effectively bypasses the cache.

ikitommi18:11:46

merged, so generators are also cached by default (with the Schema instance):

(def schema
  (m/schema
    [:map
     [:x boolean?]
     [:y {:optional true} int?]
     [:z [:map
          [:x boolean?]
          [:y {:optional true} int?]]]]))

(comment
  ;; 119µs
  ;; 16µs (cache generator)
  (p/bench (mg/generate schema)))

😮 1
ikitommi18:11:46

merged, so generators are also cached by default (with the Schema instance):

(def schema
  (m/schema
    [:map
     [:x boolean?]
     [:y {:optional true} int?]
     [:z [:map
          [:x boolean?]
          [:y {:optional true} int?]]]]))

(comment
  ;; 119µs
  ;; 16µs (cache generator)
  (p/bench (mg/generate schema)))

😮 1