-
Notifications
You must be signed in to change notification settings - Fork 204
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
Merge map-of into map #52
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.
Thanks for the PR! Looking good, few comments to make it even better :)
src/malli/core.cljc
Outdated
[entry] | ||
(-> entry meta :pred-key?)) | ||
|
||
(defn- -only-map-of? |
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 less is more: map already has special syntax (in form of map-entries are represented as a vector), I think we could force all entries to be represented this way, so special syntax for only map-of. The code would be much simpler too, I believe.
;; illegal
[:map int? int?]
;; ok
[:map [int? int?]]
what do you think?
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.
Good idea!
(let [valid? (-validator value) | ||
default (boolean optional)] | ||
(fn [m] (if-let [map-entry (find m key)] (valid? (val map-entry)) default)))) | ||
(fn [m] |
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.
let's not push any code from compile time into validation time if possible. Hre, validators
vector could contain first the normal key validators and the the extra validators. Same goes for -explainer
and -transformer
methods, all on the hot perf path.
There is a small perf regression test suite if you want to peek how this would effect perf. My guess it's 2-5x times slower than before, see
https://github.com/metosin/malli/blob/master/perf/malli/perf_test.cljc#L244
[:map | ||
[:x string?] | ||
[[:opt :y] int?] | ||
[keyword? int?]]) |
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.
there should be a test that for multiple predicate keys too. Schema explicitly only allows one extra key per map, I think we might not need that restriction.
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.
What do you mean?
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.
My bad, was thinking Plumatic schema, which has the limitation - just one predicate key per map
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.
Allow something like that?
(m/schema
[:map
[keyword? int?]
[keyword? 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.
Let's not. That can already be defined as:
[:map
[keyword? [:or int? string?]]
... and this would cause trouble when later doing tools to merge maps and removing keyword?
key might or might not remove all keys. Same actually applies to normal keys, this should be illegal:
[:map [:x int?] [:x string?]]
Thanks! I'll merge this in and will check the perf & think about corner cases. |
perf regressed for validation of |
There is actually ambiguity rule here: Given a schema: [:map
[:a int?]
[:b string?]
[::id string?] One could have binded the (m/schema :tuple)
; :tuple => Will remove the changes and put the commits aside until resolved. Need to think this over. |
Master is reset back before this, and these changed are stored in branch https://github.com/metosin/malli/tree/map-of-with-map |
#43