plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.4k stars 256 forks source link

Babashka support #440

Closed frenchy64 closed 2 years ago

frenchy64 commented 2 years ago

The next version of babashka will be able to support schema with these changes.

The main thing to note here is that babashka cannot dynamically create classes (since it runs on graalvm), so cc/fn, cc/defrecord, cc/defprotocol have very different implementations than JVM Clojure.

Here, it mostly amounts to broadening class? checks to also account for sci.lang.Type, which is babashka's representation of dynamically generated classes.

We also use fn identity for s/fn-schema since bb keeps a pool of its own fn classes for each arity, and revert to less optimized CLJS approach in some other places.

Note: in bb, both :clj and :bb reader conditionals are active.

Note: in schema.macros, we can't use reader conditionals (.clj) so if-bb uses system props to determine platform.

Note: bb doesn't support extending print-method via a protocol, so macros/defrecord-schema is used to extend it for each schema primitive.

Bonus: adds pretty pr-str printing in CLJS.

The entire test suite remarkably runs in 0.5 seconds:

$ bash <(curl https://raw.githubusercontent.com/babashka/babashka/master/install) --version 0.8.3-SNAPSHOT --dir .
$ time ./bb test

Testing schema.core-test

Testing schema.macros-test

Testing schema.coerce-test

Testing schema.experimental.abstract-map-test

Testing schema.test-test

Testing schema.utils-test

Ran 121 tests containing 660 assertions.
0 failures, 0 errors.
./bb test  0.46s user 0.05s system 98% cpu 0.517 total