Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.pid
*.swp
*.swo
*.iml
node_modules
checkstyle.xml
loopback-boot-*.tgz
Expand Down
24 changes: 21 additions & 3 deletions lib/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,28 @@ function mergeAppConfig(target, config, fileName) {
function applyCustomConfig(target, config) {
for (var key in config) {
var value = config[key];
if (typeof value === 'object') {
return 'override for the option `' + key + '` is not a value type.';
if (target[key]) {
if (Array.isArray(target[key]) && Array.isArray(value)) {
if (target[key].length == value.length) {
for (var valueIdx in value) {
if (typeof value[valueIdx] === 'object') {
applyCustomConfig(target[key][valueIdx], value[valueIdx]);
} else {
target[key][valueIdx] = value[valueIdx];
}
}
} else {
return 'override for the option `' + key +
'` is an array and lengths mismatch.';
}
} else if (typeof target[key] === 'object' && typeof value === 'object') {
applyCustomConfig(target[key], value);
} else {
target[key] = value;
}
} else {
target[key] = value;
}
target[key] = value;
}
return null; // no error
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"main": "index.js",
"browser": "browser.js",
"scripts": {
"pretest": "jshint .",
"pretest": "node_modules/.bin/jshint .",
"test": "mocha"
},
"license": {
Expand All @@ -31,6 +31,7 @@
"underscore": "^1.6.0"
},
"devDependencies": {
"jshint": "^2.5.2",
"loopback": "^1.5.0",
"mocha": "^1.19.0",
"must": "^0.12.0",
Expand Down
72 changes: 65 additions & 7 deletions test/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,82 @@ describe('compiler', function() {
expect(db).to.have.property('fromJs', true);
});

it('refuses to merge Object properties', function() {
it('merges Object properties', function() {
var nestedValue = { key: 'value' };
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('datasources.local.json', {
db: { nested: { key: 'value' } }
db: { nested: nestedValue }
});

expect(function() { boot.compile(appdir.PATH); })
.to.throw(/`nested` is not a value type/);
var instructions = boot.compile(appdir.PATH);

var db = instructions.dataSources.db;
expect(db).to.have.property('nested');
expect(db.nested).to.eql(nestedValue);
});

it('refuses to merge Array properties', function() {
it('merges nested Object properties', function() {
var nestedValue = 'http://api.test.com';
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('datasources.local.json', {
db: { nested: ['value'] }
rest: {
operations: [
{
template: {
url: nestedValue
}
}
]
}
});

var instructions = boot.compile(appdir.PATH);

var rest = instructions.dataSources.rest;
expect(rest).to.have.property('operations');
expect(rest.operations[0]).to.have.property('template');
expect(rest.operations[0].template).to.have.property('url');
expect(rest.operations[0].template.method).to.eql('POST');
expect(rest.operations[0].template.url).to.eql(nestedValue);
});

it('merges Array properties', function() {
var nestedValue = ['value'];
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('datasources.local.json', {
db: { nested: nestedValue }
});

var instructions = boot.compile(appdir.PATH);

var db = instructions.dataSources.db;
expect(db).to.have.property('nested');
expect(db.nested).to.eql(nestedValue);
});

it('errors on mismatched arrays', function() {
var nestedValue = 'http://api.test.com';
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('datasources.local.json', {
rest: {
operations: [
{
template: {
url: nestedValue
}
},
{
template: {
method: 'GET',
url: nestedValue
}
}
]
}
});

expect(function() { boot.compile(appdir.PATH); })
.to.throw(/`nested` is not a value type/);
.to.throw(/an array and lengths mismatch/);
});

it('merges app configs from multiple files', function() {
Expand Down
10 changes: 10 additions & 0 deletions test/helpers/appdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ appdir.createConfigFilesSync = function(appConfig, dataSources, models) {
db: {
connector: 'memory',
defaultForType: 'db'
},
rest: {
connector: 'rest',
operations: [
{
template: {
method: 'POST'
}
}
]
}
}, dataSources);
appdir.writeConfigFileSync ('datasources.json', dataSources);
Expand Down