From 4f50d00430b2a14d78b3ef8e4d947a27502ad162 Mon Sep 17 00:00:00 2001 From: Brian Zawisza Date: Tue, 17 Nov 2020 11:51:51 -0500 Subject: [PATCH] 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: https://github.com/mozilla/node-convict/pull/315 --- packages/convict/src/main.js | 12 ++++++------ packages/convict/test/cases/nested_dotpath.js | 5 +++++ packages/convict/test/cases/nested_dotpath.json | 5 +++++ packages/convict/test/cases/nested_dotpath.out | 5 +++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 packages/convict/test/cases/nested_dotpath.js create mode 100644 packages/convict/test/cases/nested_dotpath.json create mode 100644 packages/convict/test/cases/nested_dotpath.out diff --git a/packages/convict/src/main.js b/packages/convict/src/main.js index 5aad9325..32b0d385 100644 --- a/packages/convict/src/main.js +++ b/packages/convict/src/main.js @@ -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 = [] @@ -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 } } @@ -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 = {} @@ -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) { diff --git a/packages/convict/test/cases/nested_dotpath.js b/packages/convict/test/cases/nested_dotpath.js new file mode 100644 index 00000000..82eec5ba --- /dev/null +++ b/packages/convict/test/cases/nested_dotpath.js @@ -0,0 +1,5 @@ +'use strict' + +exports.conf = { + dynamic: {format: Object, default: null} +} diff --git a/packages/convict/test/cases/nested_dotpath.json b/packages/convict/test/cases/nested_dotpath.json new file mode 100644 index 00000000..8b94b4bb --- /dev/null +++ b/packages/convict/test/cases/nested_dotpath.json @@ -0,0 +1,5 @@ +{ + "dynamic": { + "dot.path": {} + } +} diff --git a/packages/convict/test/cases/nested_dotpath.out b/packages/convict/test/cases/nested_dotpath.out new file mode 100644 index 00000000..8b94b4bb --- /dev/null +++ b/packages/convict/test/cases/nested_dotpath.out @@ -0,0 +1,5 @@ +{ + "dynamic": { + "dot.path": {} + } +}