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

Feed :set into generator #3

Open
SevereOverfl0w opened this issue Jan 27, 2023 · 4 comments
Open

Feed :set into generator #3

SevereOverfl0w opened this issue Jan 27, 2023 · 4 comments

Comments

@SevereOverfl0w
Copy link

Hi, just playing around with this and our clojure spec generators.

It would be handy if :set could somehow be fed into the generator so I could do: (s/gen schema {[...] #(gen/return ...)}) for any overrides provided. This would allow generating off a multi-spec without needing a whole separate schema per multi-spec type(s).

e.g. I could do {:count 1 :set {:color :red}} and it would only generate things which must be red.

@flyingmachine
Copy link
Contributor

Sorry but I don't understand what you're suggesting. Would you be able to provide more context perhaps?

@SevereOverfl0w
Copy link
Author

Absolutely. With clojure.spec you can provide an override directly into the generator, the benefit of that is that if you have dependent keys within the map then the generator will factor that in, otherwise you can unintentionally produce invalid data.

(s/def :event/type keyword?)
(s/def :event/query string?)
(s/def :event/message string?)

(defmulti event-type :event/type)
(defmethod event-type :search [_] (s/keys :req [:event/type :event/query]))
(defmethod event-type :error [_] (s/keys :req [:event/type :event/message]))
(s/def :event/event (s/multi-spec event-type :event/type))

;; What I'd like data potato to do:
(gen/generate (s/gen :event/event {:event/type #(gen/return :error)})) ;;=> #:event{:type :event/error, :message "24zPSp"}

;; What data potato effectively does, but this creates an invalid event (it was a search that datapotato "changed")!
(merge (gen/generate (s/gen :event/event)) {:event/type :error}) ;; => #:event{:type :error, :query "F160GJ9nab4F24"}

So the :set override of {:event/type :error} needs to be known before generation takes place.

@raszi
Copy link

raszi commented Aug 27, 2024

This is a good idea, but I am unfamiliar with malli. Does that support the same feature? If not, then this would lead to a diversion of the implenentations.

@SevereOverfl0w
Copy link
Author

Indirectly, yes. You'd use something like [:and [:map [:event/type {:gen/elements [:error]} any?]] event-schema]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants