Use mapValuesDeep vs reduceDeep

This commit is contained in:
Bryan Ashby 2020-06-11 21:38:33 -06:00
parent 9394730011
commit 43a7bd9ba4
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
1 changed files with 8 additions and 15 deletions

View File

@ -3,7 +3,7 @@ const paths = require('path');
const async = require('async'); const async = require('async');
const _ = require('lodash'); const _ = require('lodash');
const reduceDeep = require('deepdash/getReduceDeep')(_); const mapValuesDeep = require('deepdash/getMapValuesDeep')(_);
module.exports = class ConfigLoader { module.exports = class ConfigLoader {
constructor( constructor(
@ -218,26 +218,19 @@ module.exports = class ConfigLoader {
} }
_resolveAtSpecs(config) { _resolveAtSpecs(config) {
// :TODO: mapValuesDeep may be better here return mapValuesDeep(
return reduceDeep(
config, config,
(acc, value, key, parent, ctx) => { value => {
// resolve self references; there may be a better way...
if (_.isString(value) && '@' === value.charAt(0)) { if (_.isString(value) && '@' === value.charAt(0)) {
if (value.startsWith('@reference:')) { if (value.startsWith('@reference:')) {
value = value.slice(11); const refPath = value.slice(11);
const ref = _.get(acc, value); value = _.get(config, refPath, value);
if (ref) {
_.set(acc, ctx.path, ref);
}
} else if (value.startsWith('@environment:')) { } else if (value.startsWith('@environment:')) {
value = this._resolveEnvironmentVariable(value); value = this._resolveEnvironmentVariable(value) || value;
if (!_.isUndefined(value)) {
_.set(acc, ctx.path, value);
} }
} }
}
return acc; return value;
} }
); );
} }