-
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?
Conversation
test/malli/core_test.cljc
Outdated
[:a :uuid] | ||
[:b [:enum :x :y :z]]] |
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.
used non-trivial schemas for leaves here just to convince myself that a layer of (-transform-map-keys -string->keyword)
doesn't mess with recursive decoding
CHANGELOG.md
Outdated
@@ -19,6 +19,7 @@ Malli is in well matured [alpha](README.md#alpha). | |||
* **BREAKING**: `:gen/fmap` property requires its schema to create a generator. | |||
* previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` | |||
* use `:gen/return nil` property to restore this behavior | |||
* Decode map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) |
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.
test/malli/core_test.cljc
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Added an example with "string"
key in [:map
in 761d62f.
src/malli/transform.cljc
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this might need similar map-of-key-decoders
logic as map-of
to work in the general case. Alternatively, this might need to be an optional feature like json-vectors
below.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
this will break maps like:
@ikitommi Yeah that's a good point – I've made this transformation schema-aware.
this should not be the default as it blows the perf (your JSON decoder most likely already emits keyword keys)
True, I've now made this functionality opt-in via ::keywordize-map-keys
option of the transformer.
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).
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 good to me, but I'll let @ikitommi give his opinion too
Fix #902