-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Implement mapOf setting type #12703
Implement mapOf setting type #12703
Conversation
@@ -0,0 +1,5 @@ | |||
// Jest Snapshot v1, https://goo.gl/fbAQLP | |||
|
|||
exports[`fails when not receiving expected key type 1`] = `"[name]: expected value of type [number] but got [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.
These errors aren't fantastic at giving you context yet (e.g. this doesn't separate between if the key failed or the value failed), but I think that's a separate PR cleaning it up across the Setting stuff.
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 it's good enough for now. I still need to learn what these jest snapshot are about :)
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 great, just few nits. I will battle test it with my logging settings :)
@@ -436,6 +436,65 @@ export class ObjectSetting<P extends Props> extends Setting< | |||
} | |||
} | |||
|
|||
function isMap<K, V>(o: any): o is Map<K, V> { |
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.
question: what is stopping us from using something like this?
function isMap<K, V>(o: Map<K, V>) {
return o instanceof 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.
🙈 "search internet, copy first answer" didn't work :partyparrot: <--- github needs to add this asap
}; | ||
const expected = new Map([['name', 'foo']]); | ||
|
||
expect(setting.validate(value)).toEqual(expected); |
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.
note: not for this PR, but maybe for the future discussion, from my experience validate
usually has one of the two behaviours:
- Returns
bool
(is validated or not); - Throws if can't validate.
And never modifies anything.
In this case it validates and transforms value somehow, so validate
name is a bit misleading, but maybe that's just me.
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 agree, I just haven't found a better name 🙈
platform/lib/schema/index.ts
Outdated
obj[key], | ||
toContext(context, key) | ||
); | ||
return toTuple(validatedKey, validatedValue); |
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.
nit: I actually find[validatedKey, validatedValue] as [K, V]
more readable and it requires less code, but up to you :)
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.
Yeah, I did that at first, but added toTuple
to have a place to add a comment. But I agree, I'll change it. 👍
platform/lib/schema/index.ts
Outdated
} | ||
|
||
if (isMap(obj)) { | ||
const res = [...obj.entries()].map(([key, value]) => { |
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.
nit: Hmm, these two pieces look so similar, maybe we can do something like this?
let keyValuePairs;
if (isPlainObject(obj)) {
keyValuePairs = Object.keys(obj).map((key) => [key, obj[key]]);
} else if (isMap(obj)) {
keyValuePairs = Array.from(obj);
} else {
throw new SettingError(
`expected value of type [Map] or [object] but got [${typeDetect(obj)}]`,
context
);
}
const res = keyValuePairs.map(([key, value]) => {
// You see, I don't like local variables if it's easy to read and TS doesn't complain :D
// But it's personal preference, so up to you
return [
this.keySetting.validate(key, toContext(context, key)),
this.valueSetting.validate(value, toContext(context, key))
] as [K, V];
});
return new Map(res);
Although I think additional map
call is not a big deal in this code (+ we'll get rid of it once we get Object.entries
), we can go further and iterate lazily if you have performance concerns:
let keyValuePairs;
if (isPlainObject(obj)) {
keyValuePairs = function* iterator() {
for (const key of Object.keys(obj)) {
yield [key, obj[key]];
}
}();
} else if (isMap(obj)) {
keyValuePairs = obj;
} else {
throw new SettingError(
`expected value of type [Map] or [object] but got [${typeDetect(obj)}]`,
context
);
}
const resultMap = new Map();
for (const [key, value] of keyValuePairs) {
resultMap.set(
this.keySetting.validate(key, toContext(context, key)),
this.valueSetting.validate(value, toContext(context, key))
);
}
return resultMap;
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 extracted a function instead. It felt a tad more readable in the end after playing around with it.
@@ -483,6 +542,14 @@ export function arrayOf<T>( | |||
return new ArraySetting(itemSetting, options); | |||
} | |||
|
|||
export function mapOf<K, V>( |
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.
nit: it would suuuuuuuper-awesome if you can add proper JSDoc
for this exported function (with tiny example maybe) :)
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'll create a separate PR where I add a jsdoc for all of these
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.
Nice!
@@ -0,0 +1,5 @@ | |||
// Jest Snapshot v1, https://goo.gl/fbAQLP | |||
|
|||
exports[`fails when not receiving expected key type 1`] = `"[name]: expected value of type [number] but got [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.
I think it's good enough for now. I still need to learn what these jest snapshot are about :)
@azasypkin This should help with the logging stuff, I expect ;)