Fork me on GitHub
#meander
<
2022-02-06
>
Richie03:02:28

I've got something else that I want to improve as well. I'm not blocked on this since I've got a workaround but I don't like the workaround.

(defn my-fun
  [x]
  (apply merge
         (m/rewrites x

                     {:thing {:map ?stuff}
                      :min ?length
                      :max ?length
                      :map-entries (m/some ?entries)}
                     {:entity {:stuff ?stuff
                               :entries ?entries}}

                     {:thing {:map ?stuff}
                      :map-entries (m/some ?entries)}
                     {:map {:stuff ?stuff
                            :entries ?entries}}

                     {:thing {:list ?stuff}
                      :list-entries (m/some ?entries)}
                     {:list {:stuff ?stuff
                             :entries ?entries}})))
(my-fun {:thing {:map 'stuff}
         :min 1
         :max 3
         :map-entries [1 2 3]})
;; {:map {:entries [1 2 3]}}
(my-fun {:thing {:map 'stuff}
         :min 1
         :max 1
         :map-entries [1 2 3]})
;; produces
;; {:entity {:entries [1 2 3]}, :map {:entries [1 2 3]}}
;; but i only want
;; {:map {:entries [1 2 3]}}

(my-fun {:thing {:map 'stuff
                 :list 'other-stuff}
         :min 1
         :max 3
         :map-entries [1 2 3]
         :list-entries 't})
;; {:map {:stuff stuff, :entries [1 2 3]}, :list {:stuff other-stuff, :entries t}}
(my-fun {:thing {:map 'stuff
                 :list 'other-stuff}
         :min 1
         :max 1
         :map-entries [1 2 3]
         :list-entries 't})
;; produces
;; {:entity {:stuff stuff, :entries [1 2 3]}, :map {:stuff stuff, :entries [1 2 3]}, :list {:stuff other-stuff, :entries t}}
;; but I only want
;; {:entity {:stuff stuff, :entries [1 2 3]}, :list {:stuff other-stuff, :entries t}}

;; should only return either :map or :entity, not both
I was trying to generate a schema from spec-provider stats output. I want rewrites to return multiple matches in case it's a map and a list but I don't want it to return "entity" and "map" together since "entity" trumps" map. I'm guessing it's an entity if the data is uniform and falling back to a map collection if it's not. I still want to know if it's a map and a list so that I can wrap them both in an "or". Another example: "map" and "nil" makes a nilable map.