-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(override): allow addendum data to override imported place data (#…
…1494) * proof of concept for having addendums that override indexed fields * feat(override): allow addendum data to override imported place data * use _.has
- Loading branch information
Showing
5 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const _ = require('lodash'); | ||
const logger = require('pelias-logger').get('api'); | ||
const codec = require('pelias-model').codec; | ||
|
||
function setup() { | ||
return applyOverrides; | ||
} | ||
|
||
function applyOverrides(req, res, next) { | ||
// do nothing if no result data set | ||
if (!res || !res.data) { | ||
return next(); | ||
} | ||
|
||
res.data = res.data.map(overrideOneRecord); | ||
|
||
next(); | ||
} | ||
|
||
/* | ||
* Rename the fields in one record | ||
*/ | ||
function overrideOneRecord(place) { | ||
if (_.has(place, 'addendum.override')) { | ||
try { | ||
const overrideData = codec.decode(place.addendum.override); | ||
place = _.merge(place, overrideData); | ||
} catch (err) { | ||
logger.error('Invalid addendum override json string:', place.addendum); | ||
} | ||
|
||
delete place.addendum.override; | ||
|
||
if (_.isEmpty(place.addendum)) { | ||
delete place.addendum; | ||
} | ||
} | ||
return place; | ||
} | ||
|
||
module.exports = setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const applyOverrides = require('../../../middleware/applyOverrides')(); | ||
const codec = require('pelias-model').codec; | ||
|
||
const proxyquire = require('proxyquire').noCallThru(); | ||
|
||
module.exports.tests = {}; | ||
|
||
module.exports.tests.applyOverrides = function(test, common) { | ||
test('valid override data with no other addendum', function(t) { | ||
const res = { | ||
data: [ | ||
{ | ||
name: 'aliased name to throw out', | ||
addendum: { | ||
override: codec.encode({ name: 'Override Name' }), | ||
} | ||
} | ||
] | ||
}; | ||
|
||
const expected = { | ||
data: [ | ||
{ | ||
name: 'Override Name' | ||
} | ||
] | ||
}; | ||
|
||
applyOverrides({}, res, function () { | ||
t.deepEquals(res, expected, 'valid override data'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('valid override data with other addendum data', function(t) { | ||
const res = { | ||
data: [ | ||
{ | ||
name: 'aliased name to throw out', | ||
addendum: { | ||
override: codec.encode({ name: 'Override Name' }), | ||
other_data: 1234 | ||
} | ||
} | ||
] | ||
}; | ||
|
||
const expected = { | ||
data: [ | ||
{ | ||
name: 'Override Name', | ||
addendum: { | ||
other_data: 1234 | ||
} | ||
} | ||
] | ||
}; | ||
|
||
applyOverrides({}, res, function () { | ||
t.deepEquals(res, expected, 'valid override data'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('invalid override data', function(t) { | ||
const res = { | ||
data: [ | ||
{ | ||
name: 'aliased name to throw out', | ||
addendum: { | ||
override: 'garbage json' | ||
} | ||
} | ||
] | ||
}; | ||
|
||
const expected = { | ||
data: [ | ||
{ | ||
name: 'aliased name to throw out' | ||
} | ||
] | ||
}; | ||
|
||
const proxiedApplyOverrides = proxyquire('../../../middleware/applyOverrides', { | ||
'pelias-logger': { | ||
get: () => { | ||
return { | ||
error: (msg1, msg2) => { | ||
t.equals(msg1, 'Invalid addendum override json string:'); | ||
t.deepEquals(msg2, { override: 'garbage json' }); | ||
} | ||
}; | ||
|
||
} | ||
} | ||
})(); | ||
|
||
proxiedApplyOverrides({}, res, function () { | ||
t.deepEquals(res, expected, 'invalid override data'); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
function test(name, testFunction) { | ||
return tape('[middleware] applyOverrides: ' + name, testFunction); | ||
} | ||
|
||
for( var testCase in module.exports.tests ){ | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters