Skip to content

Commit f56e955

Browse files
Fix dot-paths in dynamic objects
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. Previous PR that was not merged: mozilla#315
1 parent 95f4ab3 commit f56e955

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "node-convict",
3-
"private": true,
43
"scripts": {
54
"postinstall": "lerna link",
65
"test": "jest",

packages/convict/src/main.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const ALLOWED_OPTION_STRICT = 'strict'
7373
const ALLOWED_OPTION_WARN = 'warn'
7474

7575
function flatten(obj, useProperties) {
76-
const stack = Object.keys(obj)
76+
const stack = Object.keys(obj).map(k => [k])
7777
let key
7878

7979
const entries = []
@@ -85,9 +85,9 @@ function flatten(obj, useProperties) {
8585
if (useProperties) {
8686
if ('_cvtProperties' in val) {
8787
val = val._cvtProperties
88-
key = key + '._cvtProperties'
88+
key.push('_cvtProperties')
8989
} else {
90-
entries.push([key, val])
90+
entries.push([key.join('.'), val])
9191
continue
9292
}
9393
}
@@ -96,12 +96,12 @@ function flatten(obj, useProperties) {
9696
// Don't filter out empty objects
9797
if (subkeys.length > 0) {
9898
subkeys.forEach(function(subkey) {
99-
stack.push(key + '.' + subkey)
99+
stack.push(key.concat([subkey]))
100100
})
101101
continue
102102
}
103103
}
104-
entries.push([key, val])
104+
entries.push([key.join('.'), val])
105105
}
106106

107107
const flattened = {}
@@ -431,7 +431,7 @@ function loadFile(path) {
431431

432432
function walk(obj, path, initializeMissing) {
433433
if (path) {
434-
const ar = path.split('.')
434+
const ar = Array.isArray(path) ? cloneDeep(path) : path.split('.')
435435
while (ar.length) {
436436
const k = ar.shift()
437437
if (initializeMissing && obj[k] == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
exports.conf = {
4+
dynamic: {format: Object, default: null}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dynamic": {
3+
"dot.path": {}
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dynamic": {
3+
"dot.path": {}
4+
}
5+
}

0 commit comments

Comments
 (0)