-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat(data-point): Normalize collection and hash type checking functionality #214
Conversation
…process arrays rename getTypeModifier to normalizeTypeCheckSource
*/ | ||
function normalizeTypeCheckSource (source) { | ||
if (Array.isArray(source)) { | ||
return source.map(r => normalizeTypeCheckSource(r)) |
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.
hash and collection entities now pass arrays to this function
@@ -146,17 +146,17 @@ describe('ResolveEntity.resolveEntity', () => { | |||
} | |||
|
|||
test('It should resolve entity', () => { | |||
return resolveEntity('hash:asIs', 'foo').then(acc => { | |||
return resolveEntity('model:asIs', 'foo').then(acc => { |
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.
using model:asIs
instead of hash:asIs
because these entities are not receiving objects
@@ -2,11 +2,14 @@ const Util = require('util') | |||
const _ = require('lodash') | |||
const utils = require('../utils') | |||
|
|||
function errorMessage (value, expectedType) { | |||
const entityId = _.get(value, 'reducer.spec.id', '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.
I'm not sure if this line was needed, but it looks like it expects value
to be an accumulator, which is not the case
I want to make a few changes to this, so I'm closing it for now. |
@@ -2045,7 +2046,7 @@ If `params.inspect` is a `function`, you may execute custom debugging code to be | |||
|
|||
A Hash entity transforms a _Hash_ like data structure. It enables you to manipulate the keys within a Hash. | |||
|
|||
To prevent unexpected results, **Hash** can only process **Plain Objects**, which are objects created by the Object constructor. If [Hash.value](#hash-value) does not resolve to a Plain Object it will **throw** an error. | |||
To prevent unexpected results, a **Hash** can only return **Plain Objects**, which are objects created by the Object constructor. If a hash resolves to a different type, it will throw an error. This type check occurs *before* the value is passed to the (optional) `outputType` reducer. |
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 rewording lost an important point: Hashes only process plain objects. Can we put that line back in somehow. (something like, "... a Hash can only accept and return Plain Objects...")
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 just double checked and hash can accept any type - it's only required to return an object, which is why I changed this wording.
with that said, do you think that hash should only accept objects as input? that might be a good idea
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.
Hmm, this test looks like it throws if the input to a hash is an array. Am I missing something?
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 that's throwing because the output is an array, not because of the input (I think the name of the test is a little misleading though, since it makes it sound like the input needs to be an array)
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.
Maybe we can clean up that test in this PR too then? I always thought the input needed to be an object. The README and the test both read that way for me.
👍 on the rewording as is.
Regarding ensuring the input is an object, that might be better off in a separate PR. This is mostly refactoring the outputType, and that would introduce changes to a different lifecycle method. 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.
sounds good, I do think we should make that change at some point though. I just checked and made sure the wording for the tests is updated in this PR (so it no longer has messages like entity.hash - only process Plain Objects
)
@@ -2383,7 +2382,7 @@ A Collection entity enables you to operate over an array. Its API provides basic | |||
|
|||
Collection entities expose a set of reducers that you may apply to them: [map](#collection-map), [find](#collection-find), [filter](#collection-filter). These reducers are executed in a [specific order](#collection-reducers-order). If you want to have more control over the order of execution, use the [compose](#compose-reducer) reducer. | |||
|
|||
To prevent unexpected results, a **Collection Entity** can only process **Arrays**, if Collection.value does not resolve to an Array it will **throw** an error. | |||
To prevent unexpected results, a **Collection** can only return arrays. If a collection resolves to a different type, it will throw an eror. This type check occurs *before* the value is passed to the (optional) `outputType` reducer. |
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.
Similar to my comment on Hash, Collections accept and return arrays.
expect(acc).toHaveProperty('value', 'foo') | ||
}) | ||
}) | ||
|
||
test('It should attach entityId to error', () => { | ||
const rejectResolver = () => Promise.reject(new Error('test')) | ||
return resolveEntity('hash:asIs', undefined, undefined, rejectResolver) | ||
return resolveEntity('model:asIs', undefined, undefined, rejectResolver) |
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.
Would this not work with the hash:asIs
?
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.
you're right, it works either way. changed it back to hash:asIs
… invalid outputTypes
…to type-check-functions.js
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.
LGTM! Didn’t realize some things about these entity types before the README updates. 😅
expect(result).toMatchSnapshot() | ||
}) | ||
} | ||
test('should throw error from default outputType reducer when input is not valid', () => { |
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.
Shouldn't this be should throw error from default outputType reducer when output is not valid
?
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 catch - fixed this along with a similar mistake in entity-collection/resolve.test.js
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! 👍
closes #189
What: always use
outputType
for type checkinghash
andcollection
entities (before, they had custom validate functions)Why: type checking is always done in the same place now, whether or not the user has provided a custom type check reducer
How:
getTypeModifier
to now process arrays (and renamed it tonormalizeTypeCheckSource
)hash#outputType
always uses theisObject
reducer, following by an optional custom reducercollection#outputType
always uses theisArray
reducer, following by an optional custom reduceroutputType
for hash or collection (likestring
,number
, etc)Checklist: