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.

Previous PR that was not merged:
mozilla#315
  • Loading branch information
bzawisza-atlassian committed Nov 18, 2020
1 parent 95f4ab3 commit 4f50d00
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/convict/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const ALLOWED_OPTION_STRICT = 'strict'
const ALLOWED_OPTION_WARN = 'warn'

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

const entries = []
Expand All @@ -85,9 +85,9 @@ function flatten(obj, useProperties) {
if (useProperties) {
if ('_cvtProperties' in val) {
val = val._cvtProperties
key = key + '._cvtProperties'
key.push('_cvtProperties')
} else {
entries.push([key, val])
entries.push([key.join('.'), val])
continue
}
}
Expand All @@ -96,12 +96,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])
}

const flattened = {}
Expand Down Expand Up @@ -431,7 +431,7 @@ function loadFile(path) {

function walk(obj, path, initializeMissing) {
if (path) {
const ar = path.split('.')
const ar = Array.isArray(path) ? cloneDeep(path) : path.split('.')
while (ar.length) {
const k = ar.shift()
if (initializeMissing && obj[k] == null) {
Expand Down
5 changes: 5 additions & 0 deletions packages/convict/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 packages/convict/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 packages/convict/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 4f50d00

Please sign in to comment.