Skip to content

Commit

Permalink
Merge pull request #5712 from openstreetmap/preset-field-inheritance
Browse files Browse the repository at this point in the history
Preset field inheritance
  • Loading branch information
quincylvania authored Jan 15, 2019
2 parents a9b8cfd + 4252c5c commit 03f3dd5
Show file tree
Hide file tree
Showing 149 changed files with 1,323 additions and 2,233 deletions.
44 changes: 22 additions & 22 deletions build_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const requireESM = require('esm')(module);
const _cloneDeep = requireESM('lodash-es/cloneDeep').default;
const _forEach = requireESM('lodash-es/forEach').default;
const _intersection = requireESM('lodash-es/intersection').default;
const _isEmpty = requireESM('lodash-es/isEmpty').default;
const _merge = requireESM('lodash-es/merge').default;
const _toPairs = requireESM('lodash-es/toPairs').default;
Expand Down Expand Up @@ -459,28 +458,29 @@ function validateCategoryPresets(categories, presets) {
}

function validatePresetFields(presets, fields) {
var betweenBracketsRegex = /([^{]*?)(?=\})/;
_forEach(presets, function(preset) {
if (preset.fields) {
preset.fields.forEach(function(field) {
if (fields[field] === undefined) {
console.error('Unknown preset field "' + field + '" in "fields" array of preset ' + preset.name);
process.exit(1);
}
});
}
if (preset.moreFields) {
preset.moreFields.forEach(function(field) {
if (fields[field] === undefined) {
console.error('Unknown preset field "' + field + '" in "moreFields" array of preset ' + preset.name);
process.exit(1);
}
});
}
var fieldsIntersection = _intersection(preset.fields, preset.moreFields);
if (fieldsIntersection.length > 0) {
console.error('Preset field "' + fieldsIntersection[0] + '" in both "fields" and "moreFields" arrays of preset ' + preset.name);
process.exit(1);
}
// the keys for properties that contain arrays of field ids
var fieldKeys = ['fields', 'moreFields'];
fieldKeys.forEach(function(fieldsKey) {
if (preset[fieldsKey]) {
preset[fieldsKey].forEach(function(field) {
if (fields[field] === undefined) {
var regexResult = betweenBracketsRegex.exec(field);
if (regexResult) {
var foreignPresetID = regexResult[0];
if (presets[foreignPresetID] === undefined) {
console.error('Unknown preset "' + foreignPresetID + '" referenced in "' + fieldsKey + '" array of preset ' + preset.name);
process.exit(1);
}
} else {
console.error('Unknown preset field "' + field + '" in "' + fieldsKey + '" array of preset ' + preset.name);
process.exit(1);
}
}
});
}
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion data/presets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ en:
onsen: Japanese Onsen
beauty:
# beauty=*
label: Shop Type
label: Beauty Specialty
bench:
# bench=*
label: Bench
Expand Down
35 changes: 34 additions & 1 deletion data/presets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,40 @@ The complete JSON schema for presets can be found in [`data/presets/schema/prese

#### Preset Properties

##### searchable
##### `fields`/`moreFields`

Both these properties are arrays of field paths (e.g. `description` or `generator/type`).
`fields` are shown by default and `moreFields` are shown if manually added by the
user or if a matching tag is present. Note that some fields have a `prerequisiteTag`
property that limits when they will be shown.

A preset can reference the fields of another by using that preset's name contained in
brackets, like `{preset}`. For example, `shop/books` references and extends the fields
of `shop`:

```javascript
"fields": [
"{shop}",
"internet_access"
],
"moreFields": [
"{shop}",
"internet_access/fee",
"internet_access/ssid"
],
"tags": {
"shop": "books"
}
```

If `fields` or `moreFields` are not defined, the values of the preset's "parent"
preset are used. For example, `shop/convenience` automatically uses the same
fields as `shop`.

In both explicit and implicit inheritance, fields for keys that define the
preset are not inherited. E.g. the `shop` field is not inherited by `shop/…` presets.

##### `searchable`

Deprecated or generic presets can include the property `"searchable": false`.
This means that they will be recognized by iD when editing existing data,
Expand Down
2 changes: 1 addition & 1 deletion data/presets/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"bath/open_air": {"key": "bath:open_air", "label": "Open Air", "type": "check"},
"bath/sand_bath": {"key": "bath:sand_bath", "label": "Sand Bath", "type": "check"},
"bath/type": {"key": "bath:type", "type": "combo", "label": "Specialty", "strings": {"options": {"onsen": "Japanese Onsen", "foot_bath": "Foot Bath", "hot_spring": "Hot Spring"}}},
"beauty": {"key": "beauty", "type": "combo", "label": "Shop Type"},
"beauty": {"key": "beauty", "type": "combo", "label": "Beauty Specialty"},
"bench": {"key": "bench", "type": "check", "label": "Bench"},
"bicycle_parking": {"key": "bicycle_parking", "type": "combo", "label": "Type"},
"bin": {"key": "bin", "type": "check", "label": "Waste Bin"},
Expand Down
2 changes: 1 addition & 1 deletion data/presets/fields/beauty.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"key": "beauty",
"type": "combo",
"label": "Shop Type"
"label": "Beauty Specialty"
}
2 changes: 1 addition & 1 deletion data/presets/fields/shop.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"key": "shop",
"type": "typeCombo",
"label": "Type"
}
}
Loading

0 comments on commit 03f3dd5

Please sign in to comment.