-
Notifications
You must be signed in to change notification settings - Fork 93
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
Transformers #116
Transformers #116
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good, I really like the idea of defining an explicit interface for the encoding/decoding (the Transformer
protocol), makes the whole process clearer 👍
CHANGELOG.md
Outdated
(st/encode ::spec $)) | ||
; :clojure.spec.alpha/invalid | ||
|
||
(as-> "KiKka" $ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep here the comment about bijections? (first time I read it I thought it was a nice disclaimer)
src/spec_tools/transformer.cljc
Outdated
(catch #?(:clj Exception, :cljs js/Error) _ x)) | ||
x)) | ||
|
||
(defn date->string [_ x] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function and keyword->string
above are missing tests. Also, it would be very nice to have tests that do the roundtrip, that would help showing/uncover cases in which the "bijection" does not hold.
new tests, fixes and docs |
I also like it, I was wondering if I could address the json schema to spec round trip with this new addition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it in general. Glad to move away from calling it "conforming".
CHANGELOG.md
Outdated
|
||
(as-> "KiKka" $ | ||
(st/decode ::spec $ st/string-transformer) | ||
(st/encode ::spec $)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what is supposed to happen here. If (st/encode ::spec $)
won't ever do anything useful, should it have a 3-ary version at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true. 2-arity encode doesn't do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I meant 2, not 3
I also like the look of this in general, although I've not used the underlying "confiorming" libraries directly, outside of debugging stuff. a (very minor) naming nit-pick: |
I'll merge this anyway. Will not freeze before name is resolved. |
st/IntoSpec
protocol to convert non-recursively vanillaclojure.spec
Specs intost/Spec
:s. Used inst/encode
,st/decode
,st/explain
,st/explain-data
,st/conform
andst/conform!
.st/type-conforming
,st/json-conforming
,st/string-conforming
st/Transformer
protocol to drive spec-driven value transformationsst/encode
) & decoded (st/decode
) using a transformer, fixes #96.spec-tools.conform
intospec-tools.transform
, covering both encoding & decoding of valuesst/type-transformer
, supporting both:type
andSpec
level transformationsencode
anddecode
namespaces.st/encode
,st/decode
,st/explain
,st/explain-data
,st/conform
andst/conform!
take the transformer instance an optional third argumentst/json-transformer
andst/string-transformer
are shipped out-of-the-boxTransformer
Spec-driven transformations
:encode/*
and:decode/*
keys from Spec instances to declare how the values should be transformedSpec Bijections?
no, as there can be multiple valid representations for a encoded value. But it's quaranteed that a decoded values X is always encoded into Y, which can be decoded back into X,
y -> X -> Y -> X
Type-driven transformations
:type
information from Specs (mostly resolved automatically):type
gives you encoders & decoders (and docs) for free, like Data.Unjson: