Repository: ViacomInc/data-point
Pull request #77 (2017-12-22 20:21:55)

Factories and resolvers living together
<!--
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!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: The directories for each reducer type (and for transform expressions) now contain the resolve method for that type

<!-- Why are these changes necessary? -->
**Why**: #76 

<!-- How were these changes implemented? -->
**How**: Reorganizing some code

<!-- 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" -->
- [ ] Documentation **N/A**
- [ ] Tests **N/A**
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->

<!-- feel free to add additional comments -->


raingerber: This PR will have a conflict after #59 is merged.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/14781861/badge)](https://coveralls.io/builds/14781861)

Coverage remained the same at 100.0% when pulling **78a15b3d38e1ce708a41c6ba5d7bd61775ff9848 on raingerber:factories-and-resolvers-living-together** into **336b6ade7e85d7d4581c12a660493e9516409ee8 on ViacomInc:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14786417/badge)](https://coveralls.io/builds/14786417)

Coverage remained the same at 100.0% when pulling **403bf785cfa933c6f6296f4a55369e04398c2668 on raingerber:factories-and-resolvers-living-together** into **3caa57255b3cfbf1410d09a237d5ad501a02ab72 on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/core/transform.js b/packages/data-point/lib/core/transform.js
index f9c6c49..e51781f 100644
--- a/packages/data-point/lib/core/transform.js
+++ b/packages/data-point/lib/core/transform.js
@@ -5,7 +5,7 @@ const Promise = require('bluebird')
 
 const AccumulatorFactory = require('../accumulator/factory')
 const TransformExpression = require('../transform-expression')
-const reducer = require('../resolve/reducer')
+const resolveTransform = require('../transform-expression').resolve
 
 function getOptions (spec) {
   return _.defaults({}, spec, {
@@ -24,7 +24,7 @@ function resolve (manager, transformSource, value, options) {
 
   const transform = TransformExpression.create(transformSource)
 
-  return reducer.resolve(manager, context, transform)
+  return resolveTransform(manager, context, transform)
 }
 
 function transform (manager, transformSource, value, options, done) {
diff --git a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js b/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js
index 101d384..14779c4 100644
--- a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js
+++ b/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js
@@ -3,7 +3,7 @@
 const _ = require('lodash')
 const Promise = require('bluebird')
 
-const middleware = require('../../resolve/middleware')
+const middleware = require('../../middleware')
 
 const utils = require('../../utils')
 
diff --git a/packages/data-point/lib/helpers/helpers.js b/packages/data-point/lib/helpers/helpers.js
index 5f68657..d1b2024 100644
--- a/packages/data-point/lib/helpers/helpers.js
+++ b/packages/data-point/lib/helpers/helpers.js
@@ -2,7 +2,7 @@
 
 const _ = require('lodash')
 const Promise = require('bluebird')
-const resolve = require('../resolve/reducer').resolve
+const resolveTransform = require('../transform-expression').resolve
 const AccumulatorFactory = require('../accumulator/factory')
 const deepFreeze = require('deep-freeze')
 
@@ -78,7 +78,7 @@ function createAccumulator (value, options) {
 module.exports.createAccumulator = createAccumulator
 
 function createResolveTransform (dataPoint) {
-  return resolve.bind(null, dataPoint)
+  return resolveTransform.bind(null, dataPoint)
 }
 module.exports.createResolveTransform = createResolveTransform
 
diff --git a/packages/data-point/lib/middleware/index.js b/packages/data-point/lib/middleware/index.js
index 8dd1848..8de7fdf 100644
--- a/packages/data-point/lib/middleware/index.js
+++ b/packages/data-point/lib/middleware/index.js
@@ -1,5 +1,6 @@
 /* eslint global-require: 0 */
 
 module.exports = {
-  create: require('./factory').create
+  create: require('./factory').create,
+  resolve: require('./resolve').resolve
 }
diff --git a/packages/data-point/lib/resolve/middleware.js b/packages/data-point/lib/middleware/resolve.js
similarity index 100%
rename from packages/data-point/lib/resolve/middleware.js
rename to packages/data-point/lib/middleware/resolve.js
diff --git a/packages/data-point/lib/reducer-entity/index.js b/packages/data-point/lib/reducer-entity/index.js
index 9d90c13..7008c5a 100644
--- a/packages/data-point/lib/reducer-entity/index.js
+++ b/packages/data-point/lib/reducer-entity/index.js
@@ -1,9 +1,11 @@
 /* eslint global-require: 0 */
 
 const reducer = require('./factory')
+const resolve = require('./resolve').resolve
 
 module.exports = {
   create: reducer.create,
   type: reducer.type,
-  isType: reducer.isEntity
+  isType: reducer.isEntity,
+  resolve: resolve
 }
diff --git a/packages/data-point/lib/resolve/reducer-entity.js b/packages/data-point/lib/reducer-entity/resolve.js
similarity index 100%
rename from packages/data-point/lib/resolve/reducer-entity.js
rename to packages/data-point/lib/reducer-entity/resolve.js
diff --git a/packages/data-point/lib/reducer-function/index.js b/packages/data-point/lib/reducer-function/index.js
index 96bdf12..45bdb83 100644
--- a/packages/data-point/lib/reducer-function/index.js
+++ b/packages/data-point/lib/reducer-function/index.js
@@ -1,9 +1,11 @@
 /* eslint global-require: 0 */
 
 const reducer = require('./factory')
+const resolve = require('./resolve').resolve
 
 module.exports = {
   create: reducer.create,
   type: reducer.type,
-  isType: reducer.isFunction
+  isType: reducer.isFunction,
+  resolve: resolve
 }
diff --git a/packages/data-point/lib/reducer-path/index.js b/packages/data-point/lib/reducer-path/index.js
index 2aaf10b..0d2bbd2 100644
--- a/packages/data-point/lib/reducer-path/index.js
+++ b/packages/data-point/lib/reducer-path/index.js
@@ -1,9 +1,11 @@
 /* eslint global-require: 0 */
 
 const reducer = require('./factory')
+const resolve = require('./resolve').resolve
 
 module.exports = {
   create: reducer.create,
   type: reducer.type,
-  isType: reducer.isPath
+  isType: reducer.isPath,
+  resolve: resolve
 }
diff --git a/packages/data-point/lib/transform-expression/index.js b/packages/data-point/lib/transform-expression/index.js
index 8dd1848..8de7fdf 100644
--- a/packages/data-point/lib/transform-expression/index.js
+++ b/packages/data-point/lib/transform-expression/index.js
@@ -1,5 +1,6 @@
 /* eslint global-require: 0 */
 
 module.exports = {
-  create: require('./factory').create
+  create: require('./factory').create,
+  resolve: require('./resolve').resolve
 }
diff --git a/packages/data-point/lib/resolve/reducer.js b/packages/data-point/lib/transform-expression/resolve.js
similarity index 83%
rename from packages/data-point/lib/resolve/reducer.js
rename to packages/data-point/lib/transform-expression/resolve.js
index 302c261..a8b872b 100644
--- a/packages/data-point/lib/resolve/reducer.js
+++ b/packages/data-point/lib/transform-expression/resolve.js
@@ -2,9 +2,9 @@
 
 const _ = require('lodash')
 const Promise = require('bluebird')
-const resolveReducerPath = require('./reducer-path')
-const resolveReducerFunction = require('./reducer-function')
-const resolveEntity = require('./reducer-entity')
+const resolveReducerPath = require('../reducer-path').resolve
+const resolveReducerFunction = require('../reducer-function').resolve
+const resolveReducerEntity = require('../reducer-entity').resolve
 
 /**
  *
@@ -19,15 +19,15 @@ function getReducerFunction (store, reducerType) {
   /* eslint indent: ["error", 2, { "SwitchCase": 1 }] */
   switch (reducerType) {
     case 'ReducerPath':
-      reducerResolver = resolveReducerPath.resolve
+      reducerResolver = resolveReducerPath
       break
     case 'ReducerFunction':
-      reducerResolver = resolveReducerFunction.resolve
+      reducerResolver = resolveReducerFunction
       break
     case 'ReducerEntity':
       /* eslint no-use-before-define: "off" */
       // IMPORTANT: recursiveness here - watch out!
-      reducerResolver = _.partial(resolveEntity.resolve, store, resolve)
+      reducerResolver = _.partial(resolveReducerEntity, store, resolve)
       break
     default:
       throw new Error(`Reducer type '${reducerType}' was not recognized`)


Test patch:
diff --git a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js b/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js
index 8328935..dc0ebe5 100644
--- a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js
+++ b/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js
@@ -129,7 +129,7 @@ describe('ResolveEntity.resolveMiddleware', () => {
   })
 })
 
-const Resolve = require('../../resolve/reducer')
+const ResolveTransform = require('../../transform-expression/resolve')
 
 describe('ResolveEntity.resolveEntity', () => {
   const defaultResolver = (acc, resolveTransform) => Promise.resolve(acc)
@@ -206,7 +206,7 @@ describe('ResolveEntity.resolve', () => {
     const reducer = ReducerEntity.create(entityId)
     return ResolveEntity.resolve(
       dataPoint,
-      Resolve.resolve,
+      ResolveTransform.resolve,
       racc,
       reducer,
       resolver
diff --git a/packages/data-point/lib/resolve/reducer-function.js b/packages/data-point/lib/reducer-function/resolve.js
similarity index 100%
rename from packages/data-point/lib/resolve/reducer-function.js
rename to packages/data-point/lib/reducer-function/resolve.js
diff --git a/packages/data-point/lib/resolve/reducer-function.test.js b/packages/data-point/lib/reducer-function/resolve.test.js
similarity index 96%
rename from packages/data-point/lib/resolve/reducer-function.test.js
rename to packages/data-point/lib/reducer-function/resolve.test.js
index e294eb8..324193f 100644
--- a/packages/data-point/lib/resolve/reducer-function.test.js
+++ b/packages/data-point/lib/reducer-function/resolve.test.js
@@ -3,7 +3,7 @@
 
 const AccumulatorFactory = require('../accumulator/factory')
 const reducerFactory = require('../reducer/factory')
-const resolveFunction = require('./reducer-function')
+const resolveFunction = require('./resolve')
 
 describe('resolve#filter.resolve', () => {
   test('resolves node style callback', () => {
diff --git a/packages/data-point/lib/resolve/reducer-path.js b/packages/data-point/lib/reducer-path/resolve.js
similarity index 100%
rename from packages/data-point/lib/resolve/reducer-path.js
rename to packages/data-point/lib/reducer-path/resolve.js
diff --git a/packages/data-point/lib/resolve/reducer-path.test.js b/packages/data-point/lib/reducer-path/resolve.test.js
similarity index 95%
rename from packages/data-point/lib/resolve/reducer-path.test.js
rename to packages/data-point/lib/reducer-path/resolve.test.js
index 9503532..f57231f 100644
--- a/packages/data-point/lib/resolve/reducer-path.test.js
+++ b/packages/data-point/lib/reducer-path/resolve.test.js
@@ -3,7 +3,7 @@
 
 const AccumulatorFactory = require('../accumulator/factory')
 const reducerFactory = require('../reducer/factory')
-const reducerPath = require('./reducer-path')
+const reducerPath = require('../reducer-path')
 
 describe('resolve#transform.resolve', () => {
   function resolve (value, rawReducer) {
diff --git a/packages/data-point/lib/resolve/reducer.test.js b/packages/data-point/lib/transform-expression/resolve.test.js
similarity index 66%
rename from packages/data-point/lib/resolve/reducer.test.js
rename to packages/data-point/lib/transform-expression/resolve.test.js
index ac91d97..fd27551 100644
--- a/packages/data-point/lib/resolve/reducer.test.js
+++ b/packages/data-point/lib/transform-expression/resolve.test.js
@@ -9,9 +9,9 @@ const testData = require('../../test/data.json')
 
 const AccumulatorFactory = require('../accumulator/factory')
 const reducerFactory = require('../reducer/factory')
-const TransformExpression = require('../transform-expression')
+const TransformExpression = require('./factory')
 
-const resolveReducer = require('./reducer')
+const ResolveTransform = require('./resolve')
 
 let store
 
@@ -21,20 +21,23 @@ beforeAll(() => {
 
 describe('reducer.getReducerFunction', () => {
   test('resolve to ReducerPath', () => {
-    const resolver = resolveReducer.getReducerFunction(store, 'ReducerPath')
+    const resolver = ResolveTransform.getReducerFunction(store, 'ReducerPath')
     expect(resolver).toBeInstanceOf(Function)
   })
   test('resolve to ReducerFunction', () => {
-    const resolver = resolveReducer.getReducerFunction(store, 'ReducerFunction')
+    const resolver = ResolveTransform.getReducerFunction(
+      store,
+      'ReducerFunction'
+    )
     expect(resolver).toBeInstanceOf(Function)
   })
   test('resolve to ReducerEntity', () => {
-    const resolver = resolveReducer.getReducerFunction(store, 'ReducerEntity')
+    const resolver = ResolveTransform.getReducerFunction(store, 'ReducerEntity')
     expect(resolver).toBeInstanceOf(Function)
   })
   test('resolve to ReducerEntity', () => {
     expect(() => {
-      resolveReducer.getReducerFunction(store, 'INVALID TYPE')
+      ResolveTransform.getReducerFunction(store, 'INVALID TYPE')
     }).toThrow()
   })
 })
@@ -45,11 +48,11 @@ test('resolve#reducer.resolveReducer', () => {
   })
 
   const reducer = reducerFactory.create(reducers.addCollectionValues())
-  return resolveReducer
-    .resolveReducer(store, accumulator, reducer)
-    .then(result => {
+  return ResolveTransform.resolveReducer(store, accumulator, reducer).then(
+    result => {
       expect(result.value).toEqual(6)
-    })
+    }
+  )
 })
 
 test('resolve#reducer.resolve - reducer empty', () => {
@@ -59,9 +62,9 @@ test('resolve#reducer.resolve - reducer empty', () => {
 
   const transform = TransformExpression.create('')
 
-  return resolveReducer
-    .resolve(store, accumulator, transform)
-    .then(result => expect(result.value).toEqual(testData.a.g))
+  return ResolveTransform.resolve(store, accumulator, transform).then(result =>
+    expect(result.value).toEqual(testData.a.g)
+  )
 })
 
 describe('resolve#reducer.resolve - reducer transform', () => {
@@ -71,9 +74,9 @@ describe('resolve#reducer.resolve - reducer transform', () => {
     })
 
     const transform = TransformExpression.create('$a.g')
-    return resolveReducer
-      .resolve(store, accumulator, transform)
-      .then(result => expect(result.value).toEqual(testData.a.g))
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result => expect(result.value).toEqual(testData.a.g)
+    )
   })
 
   test('multiple transforms', () => {
@@ -83,9 +86,9 @@ describe('resolve#reducer.resolve - reducer transform', () => {
 
     const transform = TransformExpression.create('$a.g | $g1')
 
-    return resolveReducer
-      .resolve(store, accumulator, transform)
-      .then(result => expect(result.value).toBe(1))
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result => expect(result.value).toBe(1)
+    )
   })
 })
 
@@ -96,9 +99,9 @@ describe('resolve#reducer.resolve - reducer model', () => {
     })
 
     const transform = TransformExpression.create('hash:asIs')
-    return resolveReducer
-      .resolve(store, accumulator, transform)
-      .then(result => expect(result.value).toEqual(testData))
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result => expect(result.value).toEqual(testData)
+    )
   })
 
   test('multiple models', () => {
@@ -108,9 +111,9 @@ describe('resolve#reducer.resolve - reducer model', () => {
 
     const transform = TransformExpression.create('hash:asIs | hash:a.1')
 
-    return resolveReducer
-      .resolve(store, accumulator, transform)
-      .then(result => expect(result.value).toEqual(testData.a.h))
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result => expect(result.value).toEqual(testData.a.h)
+    )
   })
 })
 
@@ -127,10 +130,11 @@ describe('resolve#reducer.resolve - reducer request', () => {
     })
 
     const transform = TransformExpression.create('request:a1')
-    return resolveReducer.resolve(store, accumulator, transform).then(result =>
-      expect(result.value).toEqual({
-        ok: true
-      })
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result =>
+        expect(result.value).toEqual({
+          ok: true
+        })
     )
   })
 
@@ -153,10 +157,11 @@ describe('resolve#reducer.resolve - reducer request', () => {
 
     const transform = TransformExpression.create('request:a1 | request:a3')
 
-    return resolveReducer.resolve(store, accumulator, transform).then(result =>
-      expect(result.value).toEqual({
-        ok: true
-      })
+    return ResolveTransform.resolve(store, accumulator, transform).then(
+      result =>
+        expect(result.value).toEqual({
+          ok: true
+        })
     )
   })
 })