-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathTypeMapping.js
181 lines (147 loc) · 5.18 KB
/
TypeMapping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const _ = require('lodash');
const loadFromElasticsearch = require('./type_mapping_discovery');
var TypeMapping = function(){
// A list of all sources
this.sources = [];
// A list of alternate names for sources, mostly used to save typing
this.source_aliases = {};
// A list of all layers
this.layers = [];
/*
* A list of all layers in each source. This is used for convenience elsewhere
* to determine when a combination of source and layer parameters is
* not going to match any records and will return no results.
*/
this.layers_by_source = {};
/*
* A list of layer aliases that can be used to support specific use cases
* (like coarse geocoding) * or work around the fact that different sources
* may have layers that mean the same thing but have a different name
*/
this.layer_aliases = {};
/*
* A list of the canonical sources included in the default Pelias configuration
*/
this.canonical_sources = [];
/*
* An object that contains all sources or aliases. The key is the source or alias,
* the value is either that source, or the canonical name for that alias if it's an alias.
*/
this.source_mapping = {};
/*
* An object that has a key for each possible layer or alias,
* and returns either that layer, or all the layers in the alias
*/
this.layer_mapping = {};
};
TypeMapping.addStandardTargetsToAliases = function(standard, aliases) {
var combined = _.extend({}, aliases);
standard.forEach(function(target) {
if (combined[target] === undefined) {
combined[target] = [target];
}
});
return combined;
};
// source alias setter
TypeMapping.prototype.setSourceAliases = function( aliases ){
safeReplace(this.source_aliases, aliases);
};
// layers-by-source alias setter
TypeMapping.prototype.setLayersBySource = function( lbs ){
safeReplace(this.layers_by_source, lbs);
};
// layer alias setter
TypeMapping.prototype.setLayerAliases = function( aliases ){
safeReplace(this.layer_aliases, aliases);
};
// canonical sources setter
TypeMapping.prototype.setCanonicalSources = function( sources ){
safeReplace(this.canonical_sources, sources);
};
// generate mappings after setters have been run
TypeMapping.prototype.generateMappings = function(){
safeReplace(this.sources, Object.keys(this.layers_by_source) );
safeReplace(
this.source_mapping,
TypeMapping.addStandardTargetsToAliases(this.sources, this.source_aliases)
);
safeReplace(
this.layers,
_.uniq(Object.keys(this.layers_by_source).reduce(function (acc, key) {
return acc.concat(this.layers_by_source[key]);
}.bind(this), []))
);
safeReplace(
this.layer_mapping,
TypeMapping.addStandardTargetsToAliases(this.layers, this.layer_aliases)
);
};
// generate a list of all layers which are part of the canonical Pelias configuration
TypeMapping.prototype.getCanonicalLayers = function(){
var canonicalLayers = [];
for( var source in this.layers_by_source ){
if( _.includes( this.canonical_sources, source ) ){
canonicalLayers = _.uniq( canonicalLayers.concat( this.layers_by_source[source] ) );
}
}
return canonicalLayers;
};
// load values from targets block
TypeMapping.prototype.loadTargets = function( targetsBlock ){
if( !_.isPlainObject(targetsBlock) ){ return; }
// set values from targets block
this.setSourceAliases( targetsBlock.source_aliases || {} );
this.setLayersBySource( targetsBlock.layers_by_source || {} );
this.setLayerAliases( targetsBlock.layer_aliases || {} );
this.setCanonicalSources( targetsBlock.canonical_sources || [] );
// generate the mappings
this.generateMappings();
};
// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.load = function( done ){
// load targets from config file
this.loadFromConfig();
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);
// do not load values from elasticsearch
if( true !== peliasConfigTargets.auto_discover ){
if( 'function' === typeof done ){ done(); }
return;
}
// load values from elasticsearch
loadFromElasticsearch(this, done);
};
// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.loadFromConfig = function( done ){
// load pelias config
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);
// load targets from config file
this.loadTargets( peliasConfigTargets );
};
// replace the contents of an object or array
// while maintaining the original pointer reference
function safeReplace(reference, replacement){
if (_.isPlainObject(reference) && _.isPlainObject(replacement) ){
for (let attr in reference) { delete reference[attr]; }
for (let attr in replacement) {
let el = replacement[attr];
// skip nully elements
if (_.isNil(el) || _.isNaN(el)) { continue; }
reference[attr] = el;
}
} else if (_.isArray(reference) && _.isArray(replacement)) {
reference.length = 0;
replacement.forEach(el => {
// skip nully elements
if (_.isNil(el) || _.isNaN(el)) { return; }
reference.push(el);
});
}
}
module.exports = TypeMapping;