Skip to content
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

Multi keyword dispatch gen #1095

Merged
merged 6 commits into from
Aug 28, 2024
Merged

Multi keyword dispatch gen #1095

merged 6 commits into from
Aug 28, 2024

Conversation

ikitommi
Copy link
Member

Before

(mg/sample
 [:multi {:dispatch :type}
  ["duck" :map]
  ["boss" :map]])
;({}
; {}
; {}
; {}
; {}
; {}
; {}
; {}
; {}
; {})

After

(mg/sample
 [:multi {:dispatch :type}
  ["duck" :map]
  ["boss" :map]])
;({:type "boss"}
; {:type "boss"}
; {:type "boss"}
; {:type "boss"}
; {:type "boss"}
; {:type "duck"}
; {:type "duck"}
; {:type "duck"}
; {:type "duck"}
; {:type "boss"})

... especially nice with new :multi merging:

(mg/sample
 [:merge
  [:map [:name :string]]
  [:multi {:dispatch :type}
   ["duck" [:map [:speed :int]]]
   ["boss" [:map [:level :int]]]]])
;({:name "", :level 0, :type "boss"}
; {:name "", :speed -1, :type "duck"}
; {:name "", :level -1, :type "boss"}
; {:name "24m", :level -1, :type "boss"}
; {:name "8ou9", :level 4, :type "boss"}
; {:name "o", :speed -3, :type "duck"}
; {:name "", :speed 2, :type "duck"}
; {:name "", :speed -1, :type "duck"}
; {:name "", :level -15, :type "boss"}
; {:name "HWw6", :level 1, :type "boss"})

@frenchy64
Copy link
Collaborator

frenchy64 commented Aug 27, 2024

I wonder if there's any advantage to baking this into :multi itself. Perhaps applicable to json schema generation?

(->
  [:multi {:dispatch :type}
   ["a" :map]]
  m/children
  first last)
;=> [:merge :map [:map [:type [:= "a"]]]]

@ikitommi
Copy link
Member Author

ikitommi commented Aug 27, 2024

Schema forms should not change on schema creation -> children can't be changed. This is a needed property to ensure schemas can be read back as-is from their (serialised) forms.

There should be explicit action like deref, expand or custom helper do merge this. deref is already used for this (e.g. with :merge) but it would mean that merge implementation needs to be moved from malli.util to malli.core making it even bigger and slower to load - and more code on cljs.

I think this is a good baby step, maybe move it later to malli.util - or to core if it makes most sense. Made it private so easy to move later.

src/malli/generator.cljc Outdated Show resolved Hide resolved
@ikitommi ikitommi merged commit c5e015f into master Aug 28, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants