From 8921d0502e194c04a76ebba80116d258eb9dfe20 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 16:46:10 +1000 Subject: [PATCH 1/5] Added support for nested configs via env --- lib/nconf/stores/env.js | 21 ++++++++++++++++----- lib/nconf/stores/memory.js | 4 ++-- test/stores/env-test.js | 7 ++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index 936d45a..f5e541c 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -6,6 +6,7 @@ */ var util = require('util'), + common = require('../common'), Memory = require('./memory').Memory; // @@ -17,9 +18,15 @@ var util = require('util'), var Env = exports.Env = function (options) { Memory.call(this, options); - this.type = 'env'; - this.readOnly = true; - this.options = options || []; + options = options || {}; + this.type = 'env'; + this.readOnly = true; + this.filter = options.filter || []; + this.separator = options.separator || ''; + // Backwards compatibility + if (options instanceof Array) { + this.filter = options; + } }; // Inherit from the Memory store @@ -43,9 +50,13 @@ Env.prototype.loadEnv = function () { this.readOnly = false; Object.keys(process.env).filter(function (key) { - return !self.options.length || self.options.indexOf(key) !== -1; + return !self.filter.length || self.filter.indexOf(key) !== -1; }).forEach(function (key) { - self.set(key, process.env[key]); + if (self.separator) { + self.set(common.key.apply(common, key.split(self.separator)), process.env[key]); + } else { + self.set(key, process.env[key]); + } }); this.readOnly = true; diff --git a/lib/nconf/stores/memory.js b/lib/nconf/stores/memory.js index e075d7f..fbab7af 100644 --- a/lib/nconf/stores/memory.js +++ b/lib/nconf/stores/memory.js @@ -183,7 +183,7 @@ Memory.prototype.merge = function (key, value) { } return Object.keys(value).every(function (nested) { - return self.merge(fullKey + ':' + nested, value[nested]); + return self.merge(common.key(fullKey, nested), value[nested]); }); }; @@ -207,4 +207,4 @@ Memory.prototype.reset = function () { // Memory.prototype.loadSync = function () { return this.store || {}; -}; \ No newline at end of file +}; diff --git a/test/stores/env-test.js b/test/stores/env-test.js index e96b05f..9da60b8 100644 --- a/test/stores/env-test.js +++ b/test/stores/env-test.js @@ -16,8 +16,9 @@ vows.describe('nconf/stores/env').addBatch({ "should have the correct methods defined": function (env) { assert.isFunction(env.loadSync); assert.isFunction(env.loadEnv); - assert.isArray(env.options); - assert.lengthOf(env.options, 0); + assert.isArray(env.filter); + assert.lengthOf(env.filter, 0); + assert.equal(env.separator, ''); } } -}).export(module); \ No newline at end of file +}).export(module); From 92d4e9ea14172ee2bdd418b24d271d04e9954c74 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 17:04:06 +1000 Subject: [PATCH 2/5] Added test and updated docs --- README.md | 8 ++++++++ test/fixtures/scripts/nconf-nested-env.js | 11 +++++++++++ test/provider-test.js | 4 ++++ 3 files changed, 23 insertions(+) create mode 100644 test/fixtures/scripts/nconf-nested-env.js diff --git a/README.md b/README.md index 5fa3348..f8b583a 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,14 @@ Responsible for loading the values parsed from `process.env` into the configurat // Can optionally also be an Array of values to limit process.env to. // nconf.env(['only', 'load', 'these', 'values', 'from', 'process.env']); + + // + // Can also specify a separator for nested keys (instead of the default ':') + // + nconf.env({ + separator: '__', + filter: ['database__host', 'only', 'load', 'these', 'values', 'from', 'process.env'] + }); ``` ### Literal diff --git a/test/fixtures/scripts/nconf-nested-env.js b/test/fixtures/scripts/nconf-nested-env.js new file mode 100644 index 0000000..2752c8c --- /dev/null +++ b/test/fixtures/scripts/nconf-nested-env.js @@ -0,0 +1,11 @@ +/* + * nconf-nested-env.js: Test fixture for env with nested keys. + * + * (C) 2012, Nodejitsu Inc. + * (C) 2012, Michael Hart + * + */ + +var nconf = require('../../../lib/nconf').env({separator: '_'}); + +process.stdout.write(nconf.get('SOME:THING')); diff --git a/test/provider-test.js b/test/provider-test.js index 158b40c..63cb6f1 100644 --- a/test/provider-test.js +++ b/test/provider-test.js @@ -60,6 +60,10 @@ vows.describe('nconf/provider').addBatch({ script: path.join(fixturesDir, 'scripts', 'nconf-hierarchical-file-argv.js'), argv: ['--something', 'foobar'], env: { SOMETHING: true } + }), + "when 'env' is set to true with a nested separator": helpers.assertSystemConf({ + script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'), + env: { SOME_THING: 'foobar' } }) } } From e15f787940c56fbc6601f1874b5e27d1cfd2ec16 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 17:18:22 +1000 Subject: [PATCH 3/5] Updated README and allowed a simpley syntax --- README.md | 12 ++++++++++-- lib/nconf/stores/env.js | 4 +++- test/fixtures/scripts/nconf-nested-env.js | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f8b583a..b9c8aaf 100644 --- a/README.md +++ b/README.md @@ -163,10 +163,18 @@ Responsible for loading the values parsed from `process.env` into the configurat // // Can also specify a separator for nested keys (instead of the default ':') // + nconf.env('__'); + // Get the value of the env variable 'database__host' + var dbHost = nconf.get('database:host'); + + // + // Or use both options + // nconf.env({ separator: '__', - filter: ['database__host', 'only', 'load', 'these', 'values', 'from', 'process.env'] - }); + filter: ['database__host', 'only', 'load', 'these', 'values'] + }); + var dbHost = nconf.get('database:host'); ``` ### Literal diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index f5e541c..1aacd47 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -23,10 +23,12 @@ var Env = exports.Env = function (options) { this.readOnly = true; this.filter = options.filter || []; this.separator = options.separator || ''; - // Backwards compatibility if (options instanceof Array) { this.filter = options; } + if (typeof(options) === 'string') { + this.separator = options; + } }; // Inherit from the Memory store diff --git a/test/fixtures/scripts/nconf-nested-env.js b/test/fixtures/scripts/nconf-nested-env.js index 2752c8c..cfc3365 100644 --- a/test/fixtures/scripts/nconf-nested-env.js +++ b/test/fixtures/scripts/nconf-nested-env.js @@ -6,6 +6,6 @@ * */ -var nconf = require('../../../lib/nconf').env({separator: '_'}); +var nconf = require('../../../lib/nconf').env('_'); process.stdout.write(nconf.get('SOME:THING')); From 3c08fad1c912f5f7d24bc25a45793804584c35fe Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 18:04:37 +1000 Subject: [PATCH 4/5] Changed to as it's more accurate --- README.md | 4 ++-- lib/nconf/stores/env.js | 6 +++--- test/stores/env-test.js | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b9c8aaf..01a71f1 100644 --- a/README.md +++ b/README.md @@ -165,14 +165,14 @@ Responsible for loading the values parsed from `process.env` into the configurat // nconf.env('__'); // Get the value of the env variable 'database__host' - var dbHost = nconf.get('database:host'); + var dbHost = nconf.get('database:host'); // // Or use both options // nconf.env({ separator: '__', - filter: ['database__host', 'only', 'load', 'these', 'values'] + whitelist: ['database__host', 'only', 'load', 'these', 'values'] }); var dbHost = nconf.get('database:host'); ``` diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index 1aacd47..f4e94fc 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -21,10 +21,10 @@ var Env = exports.Env = function (options) { options = options || {}; this.type = 'env'; this.readOnly = true; - this.filter = options.filter || []; + this.whitelist = options.whitelist || []; this.separator = options.separator || ''; if (options instanceof Array) { - this.filter = options; + this.whitelist = options; } if (typeof(options) === 'string') { this.separator = options; @@ -52,7 +52,7 @@ Env.prototype.loadEnv = function () { this.readOnly = false; Object.keys(process.env).filter(function (key) { - return !self.filter.length || self.filter.indexOf(key) !== -1; + return !self.whitelist.length || self.whitelist.indexOf(key) !== -1; }).forEach(function (key) { if (self.separator) { self.set(common.key.apply(common, key.split(self.separator)), process.env[key]); diff --git a/test/stores/env-test.js b/test/stores/env-test.js index 9da60b8..6ae4cb0 100644 --- a/test/stores/env-test.js +++ b/test/stores/env-test.js @@ -16,8 +16,8 @@ vows.describe('nconf/stores/env').addBatch({ "should have the correct methods defined": function (env) { assert.isFunction(env.loadSync); assert.isFunction(env.loadEnv); - assert.isArray(env.filter); - assert.lengthOf(env.filter, 0); + assert.isArray(env.whitelist); + assert.lengthOf(env.whitelist, 0); assert.equal(env.separator, ''); } } From 9aaafc5a22b80fff13870f2357beb49857842b0c Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 18:05:52 +1000 Subject: [PATCH 5/5] Ugh, fixed whitespace --- lib/nconf/stores/env.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index f4e94fc..e73026e 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -21,7 +21,7 @@ var Env = exports.Env = function (options) { options = options || {}; this.type = 'env'; this.readOnly = true; - this.whitelist = options.whitelist || []; + this.whitelist = options.whitelist || []; this.separator = options.separator || ''; if (options instanceof Array) { this.whitelist = options;