Skip to content

Commit

Permalink
Fix dot-paths in dynamic objects
Browse files Browse the repository at this point in the history
Previously nested constructs containing dot-paths as keys would lead to
a crash like "cannot find configuration param 'dot.path'". With some
careful hacking around paths we can at least enable this use-case for
dynamic objects - it is completely untested/unsupported for regular
overlaying.
  • Loading branch information
Sebmaster committed Aug 21, 2019
1 parent b6a7c52 commit d487f7d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/convict.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const ALLOWED_OPTION_STRICT = 'strict';
const ALLOWED_OPTION_WARN = 'warn';

function flatten(obj, useProperties) {
let stack = Object.keys(obj);
let stack = Object.keys(obj).map(k => [k]);
let key;

let entries = [];
Expand All @@ -111,9 +111,9 @@ function flatten(obj, useProperties) {
if (useProperties) {
if ('properties' in val) {
val = val.properties;
key = key + '.properties';
key.push('properties');
} else {
entries.push([key, val]);
entries.push([key.join('.'), val]);
continue;
}
}
Expand All @@ -122,12 +122,12 @@ function flatten(obj, useProperties) {
// Don't filter out empty objects
if (subkeys.length > 0) {
subkeys.forEach(function(subkey) {
stack.push(key + '.' + subkey);
stack.push(key.concat([subkey]));
});
continue;
}
}
entries.push([key, val]);
entries.push([key.join('.'), val]);
}

let flattened = {};
Expand Down Expand Up @@ -456,7 +456,7 @@ function loadFile(path) {

function walk(obj, path, initializeMissing) {
if (path) {
let ar = path.split('.');
let ar = Array.isArray(path) ? cloneDeep(path) : path.split('.');
while (ar.length) {
let k = ar.shift();
if (initializeMissing && obj[k] == null) {
Expand Down
5 changes: 5 additions & 0 deletions test/cases/nested_dotpath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.conf = {
dynamic: { format: Object, default: null }
};
5 changes: 5 additions & 0 deletions test/cases/nested_dotpath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dynamic": {
"dot.path": {}
}
}
5 changes: 5 additions & 0 deletions test/cases/nested_dotpath.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dynamic": {
"dot.path": {}
}
}

0 comments on commit d487f7d

Please sign in to comment.