Skip to content
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

Merged
merged 3 commits into from
Jul 7, 2017
Merged

Conversation

kimjoar
Copy link
Contributor

@kimjoar kimjoar commented Jul 7, 2017

@azasypkin This should help with the logging stuff, I expect ;)

@@ -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]"`;
Copy link
Contributor Author

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.

Copy link
Member

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 :)

Copy link
Member

@azasypkin azasypkin left a 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> {
Copy link
Member

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;
}

Copy link
Contributor Author

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);
Copy link
Member

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.

Copy link
Contributor Author

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 🙈

obj[key],
toContext(context, key)
);
return toTuple(validatedKey, validatedValue);
Copy link
Member

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 :)

Copy link
Contributor Author

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. 👍

}

if (isMap(obj)) {
const res = [...obj.entries()].map(([key, value]) => {
Copy link
Member

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;

Copy link
Contributor Author

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>(
Copy link
Member

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) :)

Copy link
Contributor Author

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

Copy link
Member

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]"`;
Copy link
Member

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 :)

@kimjoar kimjoar merged commit 57951f6 into elastic:new-platform Jul 7, 2017
@kimjoar kimjoar deleted the platform/mapOf branch July 7, 2017 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants