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

Investigate a Protocol-based top-level API #236

Open
ikitommi opened this issue Jul 25, 2020 · 1 comment
Open

Investigate a Protocol-based top-level API #236

ikitommi opened this issue Jul 25, 2020 · 1 comment
Labels
for discussion Discussion is main focus of PR hammock need a lot of thinking. might take a while.

Comments

@ikitommi
Copy link
Member

ikitommi commented Jul 25, 2020

currently

Options is an optional parameter to all/most public apis. Would be cleaner if there was a SchemaContext Record to hold all configuration and state for working with Schemas. Should have at least :registry and :evaluator fields.

It would implement Protocols that would cover the current public APIs and would be passed as FIRST argument to apis. Would make top-level configuration explicit:

;; using the default context
(m/schema [:and int?])

;; using explicit context
(m/schema ctx [:and int?])
 
;; explicit contexts
(def ctx (m/default-context))

(def ctx (-> (m/default-context) (assoc :evaluator (malli.sci/evaluator))))

(def ctx (m/context {:registry ..., :evaluator ...}))

why?

  • Protocols yield coherent public apis, where the (optional) context is always the first argument
  • Records & Protocols give type errors (cosmetic)
  • Easy to extend
  • Clear separation between one-time/use-case options and shared/context options:
;; ctx has things like :registry, options something for this invocation
(mg/generate ctx [:maybe int?] {:size 10, :seed 10})

draft core Protocol

(defprotocol SchemaContext
  (-schema [this ?schema])
  (-into-schema [this type properties children])
  (-registry [this])
  (-evaluator [this]))
@ikitommi
Copy link
Member Author

ikitommi commented Jan 6, 2022

relates to #235.

This was referenced Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for discussion Discussion is main focus of PR hammock need a lot of thinking. might take a while.
Projects
None yet
Development

No branches or pull requests

1 participant