Repository: ViacomInc/data-point
Pull request #296 (2018-06-19 18:29:46)

feat(data-point): Allow entityOverride in options object
re #281

<!--
Thanks for your interest in the project. We appreciate bugs filed and PRs submitted!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!

BREAKING CHANGES:

If your PR includes a breaking change, please submit it with a codemod under
data-point-codemods that will help users upgrade their codebase.

Breaking changes without a codemod will not be accepted unless a codemod is not
viable or does not apply to the specific situation.
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: Added support for entity overrides

<!-- Why are these changes necessary? -->
**Why**: In order to allow global override values 

<!-- How were these changes implemented? -->
**How**: Added an override mapping to params

<!-- Have you done all of these things?  -->
**Checklist**:
<!-- add "N/A" to the end of each line that's irrelevant to your changes -->
<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->
- [ ] Has Breaking changes
- [ ] Documentation
- [ ] Tests
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added username to **all-contributors** list

<!-- feel free to add additional comments -->


codecov[bot]: # [Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=h1) Report
> Merging [#296](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=desc) into [master](https://codecov.io/gh/ViacomInc/data-point/commit/d14483369a6cad1165c12c493be6df148930b6ad?src=pr&el=desc) will **decrease** coverage by `0.05%`.
> The diff coverage is `83.33%`.

[![Impacted file tree graph](https://codecov.io/gh/ViacomInc/data-point/pull/296/graphs/tree.svg?token=LYxItnrx4i&src=pr&height=150&width=650)](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #296      +/-   ##
==========================================
- Coverage     100%   99.94%   -0.06%     
==========================================
  Files         128      128              
  Lines        1810     1816       +6     
  Branches      169      170       +1     
==========================================
+ Hits         1810     1815       +5     
- Misses          0        1       +1
```


| [Impacted Files](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/data-point/lib/core/transform.js](https://codecov.io/gh/ViacomInc/data-point/pull/296/diff?src=pr&el=tree#diff-cGFja2FnZXMvZGF0YS1wb2ludC9saWIvY29yZS90cmFuc2Zvcm0uanM=) | `100% <ø> (ø)` | :arrow_up: |
| [packages/data-point/lib/accumulator/factory.js](https://codecov.io/gh/ViacomInc/data-point/pull/296/diff?src=pr&el=tree#diff-cGFja2FnZXMvZGF0YS1wb2ludC9saWIvYWNjdW11bGF0b3IvZmFjdG9yeS5qcw==) | `100% <100%> (ø)` | :arrow_up: |
| [...data-point/lib/entity-types/base-entity/resolve.js](https://codecov.io/gh/ViacomInc/data-point/pull/296/diff?src=pr&el=tree#diff-cGFja2FnZXMvZGF0YS1wb2ludC9saWIvZW50aXR5LXR5cGVzL2Jhc2UtZW50aXR5L3Jlc29sdmUuanM=) | `99.02% <80%> (-0.98%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=footer). Last update [d144833...3865daa](https://codecov.io/gh/ViacomInc/data-point/pull/296?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

acatl: @arik223 thanks for the changes, I just realized we are missing documentation changes for this feature. Would you mind adding that once you feel the code is settled as part of this PR?
arik223: @acatl would be happy to 

Patch:
diff --git a/packages/data-point/README.md b/packages/data-point/README.md
index 616cc87..ca0f84a 100644
--- a/packages/data-point/README.md
+++ b/packages/data-point/README.md
@@ -57,6 +57,7 @@ npm install --save data-point
   - [Entity type checking](#entity-type-check)
   - [Entity ComposeReducer](#entity-compose-reducer)
   - [Extending Entities](#extending-entities)
+  - [Global Entity Options](#global-entity-options)
 - [Middleware](#middleware)
   - [dataPoint.use](#api-data-point-use)
 - [Custom Entity Types](#custom-entity-types)
@@ -3324,6 +3325,29 @@ Example at: [examples/extend-entity-keys.js](examples/extend-entity-keys.js)
 
 Example at: [examples/extend-entity-reusability.js](examples/extend-entity-reusability.js)
 
+### <a name="global-entity-options">Globally setting options for entities</a>
+You may set global params for entry types, allowing you to set these options once and have them be consistent. We do this using `entityOverrides` within the options object.
+
+**EXAMPLE**
+
+```js
+const options = {
+  entityOverrides: {
+    'request': {
+      params: {
+        inspect: true
+      }
+    }
+  }
+}
+
+dataPoint.resolve('request:xyz', {}, options)
+```
+The preceding example will make it so every `'request'` entity will have the inspect param set to true. This can be done with any entity and its respective parameters.
+
+Example at: [examples/entity-request-options-override.js](examples/entity-request-options-override.js)
+
+
 ## <a name="middleware">Middleware</a>
 
 Middleware, in the DataPoint context, is a place to add logic that will execute `before` and `after` the resolution of specified entity types. The middleware layer adds a `resolve` method to the [Accumulator](#middleware-accumulator-object) object, which allows you to control the resolution of entities.
diff --git a/packages/data-point/examples/entity-request-options-override.js b/packages/data-point/examples/entity-request-options-override.js
new file mode 100644
index 0000000..52baddb
--- /dev/null
+++ b/packages/data-point/examples/entity-request-options-override.js
@@ -0,0 +1,29 @@
+const dataPoint = require('../').create()
+const mockRequest = require('./entity-request-basic.mock')
+
+dataPoint.addEntities({
+  'request:getLuke': {
+    url: 'https://swapi.co/api/people/1/'
+  },
+  params: {
+    inspect: false
+  }
+})
+
+// mock the remote service
+mockRequest()
+
+// The params defined here will override the params defined in the entity definition.
+const options = {
+  entityOverrides: {
+    request: {
+      params: {
+        inspect: true
+      }
+    }
+  }
+}
+
+dataPoint.resolve('request:getLuke', {}, options).then(output => {
+  console.dir(output, { colors: true })
+})
diff --git a/packages/data-point/lib/accumulator/factory.js b/packages/data-point/lib/accumulator/factory.js
index 497b043..48def6e 100644
--- a/packages/data-point/lib/accumulator/factory.js
+++ b/packages/data-point/lib/accumulator/factory.js
@@ -28,7 +28,7 @@ function create (spec) {
   accumulator.reducer = {
     spec: spec.context
   }
-
+  accumulator.entityOverrides = merge({}, spec.entityOverrides)
   accumulator.locals = merge({}, spec.locals)
   accumulator.values = spec.values
   accumulator.trace = spec.trace
diff --git a/packages/data-point/lib/core/transform.js b/packages/data-point/lib/core/transform.js
index cd85235..97af3d8 100644
--- a/packages/data-point/lib/core/transform.js
+++ b/packages/data-point/lib/core/transform.js
@@ -6,7 +6,8 @@ const AccumulatorFactory = require('../accumulator/factory')
 
 function getOptions (spec) {
   return _.defaults({}, spec, {
-    locals: {}
+    locals: {},
+    entityOverrides: {}
   })
 }
 
@@ -28,6 +29,7 @@ function reducerResolve (manager, reducerSource, value, options) {
   const context = AccumulatorFactory.create({
     value: value,
     locals: contextOptions.locals,
+    entityOverrides: contextOptions.entityOverrides,
     trace: contextOptions.trace,
     values: manager.values.getStore()
   })
diff --git a/packages/data-point/lib/entity-types/base-entity/resolve.js b/packages/data-point/lib/entity-types/base-entity/resolve.js
index b491d05..0acefb9 100644
--- a/packages/data-point/lib/entity-types/base-entity/resolve.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.js
@@ -3,6 +3,7 @@ const Promise = require('bluebird')
 const middleware = require('../../middleware')
 const utils = require('../../utils')
 const memoize = require('lodash/memoize')
+const merge = require('lodash/merge')
 
 /**
  * create a new debug scope
@@ -105,7 +106,7 @@ function createCurrentAccumulator (accumulator, reducer, entity) {
     context: entity,
     reducer: currentReducer,
     initialValue: accumulator.value,
-    params: entity.params,
+    params: assignParamsHelper(accumulator, entity),
     debug: debugEntity(reducer.entityType)
   })
 
@@ -114,6 +115,24 @@ function createCurrentAccumulator (accumulator, reducer, entity) {
 
 module.exports.createCurrentAccumulator = createCurrentAccumulator
 
+/**
+ * Incase there is an override present, assigns parameters to the correct entity.
+ * @param {*} accumulator
+ * @param {*} entity
+ */
+function assignParamsHelper (accumulator, entity) {
+  if (accumulator.entityOverrides[entity.entityType]) {
+    return merge(
+      {},
+      entity.params,
+      accumulator.entityOverrides[entity.entityType].params
+    )
+  }
+  return entity.params
+}
+
+module.exports.assignParamsHelper = assignParamsHelper
+
 /**
  * Resolves a middeware, this method contains a 'hack'
  * which consists in using an error to bypass the


Test patch:
diff --git a/packages/data-point/lib/entity-types/base-entity/resolve.test.js b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
index 4085cee..047433a 100644
--- a/packages/data-point/lib/entity-types/base-entity/resolve.test.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
@@ -94,6 +94,33 @@ describe('getCurrentReducer', () => {
     })
   })
 })
+describe('ResolveEntity.createCurrentAccumulatorWithOverride', () => {
+  let acc
+  let spyGetUID
+  let entity
+  let reducer
+  beforeEach(() => {
+    spyGetUID = jest.spyOn(utils, 'getUID')
+    spyGetUID.mockImplementationOnce(() => 10)
+    reducer = createReducerEntityId(createReducer, 'hash:base')
+    entity = dataPoint.entities.get('hash:base')
+    const accumulator = helpers.createAccumulator({
+      foo: 'bar'
+    })
+    accumulator.entityOverrides = {
+      hash: {
+        params: {
+          inspect: true
+        }
+      }
+    }
+    acc = ResolveEntity.createCurrentAccumulator(accumulator, reducer, entity)
+  })
+
+  test('It should have entityOverrides', () => {
+    expect(acc).toHaveProperty('params', { base: true, inspect: true })
+  })
+})
 
 describe('ResolveEntity.createCurrentAccumulator', () => {
   let acc