Skip to content

Commit

Permalink
Upstream pick to fix breaking gid management changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed Feb 18, 2020
1 parent 758f1f8 commit cc8b51a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
21 changes: 21 additions & 0 deletions helper/decode_gid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const _ = require('lodash');

// helper method to decode a GID from a string
// for additional validation see sanitizer/_ids.js

function decodeGID(gid) {
const parts = gid.split(':');

if ( parts.length < 3 ) {
return;
}

// empty strings and other invalid values are expected to be handled by the caller
return {
source: parts[0],
layer: parts[1],
id: parts.slice(2).join(':'),
};
}

module.exports = decodeGID;
25 changes: 22 additions & 3 deletions helper/geojsonify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const logger = require('pelias-logger').get('geojsonify');
const collectDetails = require('./geojsonify_place_details');
const _ = require('lodash');
const Document = require('pelias-model').Document;
const codec = require('pelias-model').codec;
const field = require('./fieldValue');
const decode_gid = require('./decode_gid');

function geojsonifyPlaces( params, docs ){

Expand Down Expand Up @@ -39,13 +41,14 @@ function geojsonifyPlaces( params, docs ){
}

function geojsonifyPlace(params, place) {
const gid_components = decode_gid(place._id);
// setup the base doc
const doc = {
id: place._id,
gid: new Document(place.source, place.layer, place._id).getGid(),
id: gid_components.id,
gid: new Document(place.source, place.layer, gid_components.id).getGid(),
layer: place.layer,
source: place.source,
source_id: place.source_id,
source_id: gid_components.id,
bounding_box: place.bounding_box,
lat: parseFloat(place.center_point.lat),
lng: parseFloat(place.center_point.lon)
Expand All @@ -58,6 +61,22 @@ function geojsonifyPlace(params, place) {
// assign all the details info into the doc
Object.assign(doc, collectDetails(params, place));

// add addendum data if available
// note: this should be the last assigned property, for aesthetic reasons.
if (_.has(place, 'addendum')) {
let addendum = {};
for(let namespace in place.addendum){
try {
addendum[namespace] = codec.decode(place.addendum[namespace]);
} catch( e ){
logger.warn(`doc ${doc.gid} failed to decode addendum namespace ${namespace}`);
}
}
if( Object.keys(addendum).length ){
doc.addendum = addendum;
}
}

return doc;
}

Expand Down

0 comments on commit cc8b51a

Please sign in to comment.