Skip to content

Commit cc8b51a

Browse files
committed
Upstream pick to fix breaking gid management changes
1 parent 758f1f8 commit cc8b51a

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

helper/decode_gid.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const _ = require('lodash');
2+
3+
// helper method to decode a GID from a string
4+
// for additional validation see sanitizer/_ids.js
5+
6+
function decodeGID(gid) {
7+
const parts = gid.split(':');
8+
9+
if ( parts.length < 3 ) {
10+
return;
11+
}
12+
13+
// empty strings and other invalid values are expected to be handled by the caller
14+
return {
15+
source: parts[0],
16+
layer: parts[1],
17+
id: parts.slice(2).join(':'),
18+
};
19+
}
20+
21+
module.exports = decodeGID;

helper/geojsonify.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const logger = require('pelias-logger').get('geojsonify');
44
const collectDetails = require('./geojsonify_place_details');
55
const _ = require('lodash');
66
const Document = require('pelias-model').Document;
7+
const codec = require('pelias-model').codec;
78
const field = require('./fieldValue');
9+
const decode_gid = require('./decode_gid');
810

911
function geojsonifyPlaces( params, docs ){
1012

@@ -39,13 +41,14 @@ function geojsonifyPlaces( params, docs ){
3941
}
4042

4143
function geojsonifyPlace(params, place) {
44+
const gid_components = decode_gid(place._id);
4245
// setup the base doc
4346
const doc = {
44-
id: place._id,
45-
gid: new Document(place.source, place.layer, place._id).getGid(),
47+
id: gid_components.id,
48+
gid: new Document(place.source, place.layer, gid_components.id).getGid(),
4649
layer: place.layer,
4750
source: place.source,
48-
source_id: place.source_id,
51+
source_id: gid_components.id,
4952
bounding_box: place.bounding_box,
5053
lat: parseFloat(place.center_point.lat),
5154
lng: parseFloat(place.center_point.lon)
@@ -58,6 +61,22 @@ function geojsonifyPlace(params, place) {
5861
// assign all the details info into the doc
5962
Object.assign(doc, collectDetails(params, place));
6063

64+
// add addendum data if available
65+
// note: this should be the last assigned property, for aesthetic reasons.
66+
if (_.has(place, 'addendum')) {
67+
let addendum = {};
68+
for(let namespace in place.addendum){
69+
try {
70+
addendum[namespace] = codec.decode(place.addendum[namespace]);
71+
} catch( e ){
72+
logger.warn(`doc ${doc.gid} failed to decode addendum namespace ${namespace}`);
73+
}
74+
}
75+
if( Object.keys(addendum).length ){
76+
doc.addendum = addendum;
77+
}
78+
}
79+
6180
return doc;
6281
}
6382

0 commit comments

Comments
 (0)