From 8921d0502e194c04a76ebba80116d258eb9dfe20 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Thu, 21 Jun 2012 16:46:10 +1000 Subject: [PATCH] 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);