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 23, 2020
1 parent 95f4ab3 commit 6dd05c8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "6.0.0"
"version": "6.0.1-alpha.1"
}
5 changes: 3 additions & 2 deletions packages/convict-format-with-moment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"timestamp",
"moment"
],
"version": "6.0.0",
"version": "6.0.1-alpha.1",
"license": "Apache-2.0",
"homepage": "https://github.com/mozilla/node-convict",
"repository": {
Expand All @@ -26,5 +26,6 @@
],
"dependencies": {
"moment": "^2.24.0"
}
},
"gitHead": "d345d374498df65397206426d3753fcf5f9701e2"
}
5 changes: 3 additions & 2 deletions packages/convict-format-with-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url",
"validator"
],
"version": "6.0.0",
"version": "6.0.1-alpha.1",
"license": "Apache-2.0",
"homepage": "https://github.com/mozilla/node-convict",
"repository": {
Expand All @@ -27,5 +27,6 @@
],
"dependencies": {
"validator": "^11.1.0"
}
},
"gitHead": "d345d374498df65397206426d3753fcf5f9701e2"
}
7 changes: 4 additions & 3 deletions packages/convict/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"author": "Lloyd Hilaiel <[email protected]> (http://lloyd.io)",
"name": "convict",
"name": "@atlassian/convict",
"description": "Featureful configuration management library for Node.js (nested structure, schema validation, etc.)",
"keywords": [
"configuration",
Expand All @@ -17,7 +17,7 @@
"yaml",
"toml"
],
"version": "6.0.0",
"version": "6.0.1-alpha.1",
"license": "Apache-2.0",
"homepage": "https://github.com/mozilla/node-convict",
"repository": {
Expand All @@ -35,5 +35,6 @@
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"yargs-parser": "^18.1.3"
}
},
"gitHead": "d345d374498df65397206426d3753fcf5f9701e2"
}
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 6dd05c8

Please sign in to comment.