Skip to content

Commit

Permalink
Merge objects if necessary when traversing stores on get()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart authored and pksunkara committed Jun 13, 2012
1 parent 6b1b019 commit 26d81e8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
33 changes: 31 additions & 2 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ Provider.prototype.get = function (key, callback) {
var current = 0,
names = Object.keys(this.stores),
self = this,
response;
response,
mergeObjs = [];

async.whilst(function () {
return typeof response === 'undefined' && current < names.length;
Expand All @@ -233,13 +234,30 @@ Provider.prototype.get = function (key, callback) {
}

response = value;

// Merge objects if necessary
if (typeof response === 'object' && !Array.isArray(response)) {
mergeObjs.push(response);
response = undefined;
}

next();
});
}

response = store.get(key);

// Merge objects if necessary
if (typeof response === 'object' && !Array.isArray(response)) {
mergeObjs.push(response);
response = undefined;
}

next();
}, function (err) {
if (!err && mergeObjs.length) {
response = common.merge(mergeObjs.reverse());
}
return err ? callback(err) : callback(null, response);
});
};
Expand Down Expand Up @@ -475,7 +493,8 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
callback = typeof args[args.length - 1] === 'function' && args.pop(),
destructive = ['set', 'clear', 'merge'].indexOf(action) !== -1,
self = this,
response;
response,
mergeObjs = [];

function runAction (name, next) {
var store = self.stores[name];
Expand Down Expand Up @@ -505,9 +524,19 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
}

response = store[action].apply(store, args);

// Merge objects if necessary
if (action === 'get' && typeof response === 'object' && !Array.isArray(response)) {
mergeObjs.push(response);
response = undefined;
}
}
});

if (mergeObjs.length) {
response = common.merge(mergeObjs.reverse());
}

return response;
}

Expand Down
2 changes: 1 addition & 1 deletion test/complete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vows.describe('nconf').addBatch({
"literal vars": {
"are present": function () {
Object.keys(data).forEach(function (key) {
assert.equal(nconf.get(key), data[key]);
assert.deepEqual(nconf.get(key), data[key]);
});
}
},
Expand Down
8 changes: 6 additions & 2 deletions test/fixtures/merge/file1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"candy": {
"something": "file1",
"something1": true,
"something2": true
"something2": true,
"something5": {
"first": 1,
"second": 2
}
}
}
}
18 changes: 18 additions & 0 deletions test/fixtures/scripts/nconf-hierarchical-load-merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* nconf-hierarchical-load-merge.js: Test fixture for loading and merging nested objects across stores.
*
* (C) 2012, Nodejitsu Inc.
* (C) 2012, Michael Hart
*
*/

var path = require('path'),
nconf = require('../../../lib/nconf');

nconf.argv()
.file(path.join(__dirname, '..', 'merge', 'file1.json'));

process.stdout.write(JSON.stringify({
apples: nconf.get('apples'),
candy: nconf.get('candy')
}));
35 changes: 34 additions & 1 deletion test/hierarchy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ vows.describe('nconf/hierarchy').addBatch({
}
});
}
},
"configured with .argv(), .file() and invoked with nested command line options": {
topic: function () {
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge.js'),
argv = ['--candy:something', 'foo', '--candy:something5:second', 'bar'],
that = this,
data = '',
child;

child = spawn('node', [script].concat(argv));

child.stdout.on('data', function (d) {
data += d;
});

child.on('exit', function() {
that.callback(null, data);
});
},
"should merge nested objects ": function (err, data) {
assert.deepEqual(JSON.parse(data), {
apples: true,
candy: {
something: 'foo',
something1: true,
something2: true,
something5: {
first: 1,
second: 'bar'
}
}
});
}
}
}
}).export(module);
}).export(module);

0 comments on commit 26d81e8

Please sign in to comment.