Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(aggregate): automatically convert accumulator function options to…
… strings Fix #9364
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
module.exports = function stringifyAccumulatorOptions(pipeline) { | ||
if (!Array.isArray(pipeline)) { | ||
return; | ||
} | ||
|
||
for (const stage of pipeline) { | ||
if (stage == null) { | ||
continue; | ||
} | ||
|
||
const canHaveAccumulator = stage.$group || stage.$bucket || stage.$bucketAuto; | ||
if (canHaveAccumulator != null) { | ||
for (const key of Object.keys(canHaveAccumulator)) { | ||
handleAccumulator(canHaveAccumulator[key]); | ||
} | ||
} | ||
|
||
if (stage.$facet != null) { | ||
for (const key of Object.keys(stage.$facet)) { | ||
stringifyAccumulatorOptions(stage.$facet[key]); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
function handleAccumulator(operator) { | ||
if (operator == null || operator.$accumulator == null) { | ||
return; | ||
} | ||
|
||
for (const key of ['init', 'accumulate', 'merge', 'finalize']) { | ||
if (typeof operator.$accumulator[key] === 'function') { | ||
operator.$accumulator[key] = String(operator.$accumulator[key]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const stringifyAccumulatorOptions = require('../../lib/helpers/aggregate/stringifyAccumulatorOptions'); | ||
|
||
describe('stringifyAccumulatorOptions', function() { | ||
it('converts accumulator args to strings (gh-9364)', function() { | ||
const pipeline = [{ | ||
$group: { | ||
_id: '$author', | ||
avgCopies: { | ||
$accumulator: { | ||
init: function() { | ||
return { count: 0, sum: 0 }; | ||
}, | ||
accumulate: function(state, numCopies) { | ||
return { | ||
count: state.count + 1, | ||
sum: state.sum + numCopies | ||
}; | ||
}, | ||
accumulateArgs: ['$copies'], | ||
merge: function(state1, state2) { | ||
return { | ||
count: state1.count + state2.count, | ||
sum: state1.sum + state2.sum | ||
}; | ||
}, | ||
finalize: function(state) { | ||
return (state.sum / state.count); | ||
}, | ||
lang: 'js' | ||
} | ||
} | ||
} | ||
}]; | ||
|
||
stringifyAccumulatorOptions(pipeline); | ||
|
||
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.init, 'string'); | ||
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.accumulate, 'string'); | ||
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.merge, 'string'); | ||
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.finalize, 'string'); | ||
}); | ||
}); |