-
Notifications
You must be signed in to change notification settings - Fork 215
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
Decode map keys into keywords for [:map
schemas in json-transformer
#1135
base: master
Are you sure you want to change the base?
Changes from 3 commits
15b56d0
1abc8e3
7d44b1c
cb8e7f7
761d62f
5b1dbaf
6a1f515
3c9ab67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,6 +415,8 @@ | |
(-transform-if-valid key-schema) | ||
(-transform-map-keys)) | ||
(-transform-map-keys m/-keyword->string))))}) | ||
(assoc :map {:compile (fn [_ _] | ||
(-transform-map-keys -string->keyword))}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this might need similar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this should not be the default as it blows the perf (your JSON decoder most likely already emits keyword keys) + this will break maps like: [:map
["kikka" :string]
["kukka" :string]] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@ikitommi Yeah that's a good point – I've made this transformation schema-aware.
True, I've now made this functionality opt-in via It's a bit sad that we have to opt in in order to have "decoded value validates against schema" property, but I guess there's no other way (other than creating JSON deserialiser from Malli schemas directly to fuse away this step). |
||
(cond-> json-vectors (assoc :vector -sequential->vector))) | ||
:encoders (-json-encoders)}))) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -997,6 +997,18 @@ | |
(mt/key-transformer | ||
{:decode #(-> % name (str "_key") keyword)})))) | ||
|
||
(testing "JSON transformer decodes map schema keys" | ||
(let [schema [:map | ||
[:a :uuid] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a breaking change for users who want to keep keys as strings. Could you add a test case that demonstrates what happens with a schema with string keys? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an example with |
||
[:b [:enum :x :y :z]]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. used non-trivial schemas for leaves here just to convince myself that a layer of |
||
value {"a" "b699671c-d34d-b33f-1337-dbdbfd337e73" | ||
"b" "x"} | ||
decoded-value (m/decode schema value mt/json-transformer)] | ||
(is (= {:a #uuid "b699671c-d34d-b33f-1337-dbdbfd337e73" | ||
:b :x} | ||
decoded-value)) | ||
(is (m/validate schema decoded-value)))) | ||
|
||
(is (= {:x 32} | ||
(m/decode | ||
[:map {:decode/string '{:enter #(update % :x inc), :leave #(update % :x (partial * 2))}} | ||
|
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.
This line is now nested under the
:gen/fmap
change, even though it's not related. Please add it as a top-level bullet point at the top of the list.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.
Moved to top-level + no longer labeled as breaking.