This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Saved Objects] Create mappings lib within core/server/saved_objects (e…
…lastic#21093) This commit moves the large majority of the mappings library previously in src/server/mappings into src/core/server/saved_objects/mappings, since mappings should be owned by saved_objects. The kibanaIndexMappingsMixin remains in src/server, but is moved into src/server/saved_objects/mappings, keeping the same folder structure for consistency.
- Loading branch information
Showing
27 changed files
with
396 additions
and
293 deletions.
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
5 changes: 5 additions & 0 deletions
5
src/core/server/saved_objects/mappings/__snapshots__/index_mappings.test.ts.snap
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`saved objects index mapping constructor includes the pluginId from the extension in the _ error message if defined 1`] = `"Property names _foo registered by plugin abc123 are not allowed to start with an underscore (_)"`; | ||
|
||
exports[`saved objects index mapping constructor throws if any of the new properties start with _ 1`] = `"Property names _foo are not allowed to start with an underscore (_)"`; |
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
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,92 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { cloneDeep } from 'lodash'; | ||
|
||
import { EsMapping, EsMappings, getRootProperties, getRootType } from './lib'; | ||
import { formatListAsProse } from './utils'; | ||
|
||
interface MappingExtension { | ||
pluginId?: string; | ||
properties: { [key: string]: EsMapping }; | ||
} | ||
|
||
const DEFAULT_INITIAL_DSL: EsMappings = { | ||
rootType: { | ||
type: 'object', | ||
properties: {}, | ||
}, | ||
}; | ||
|
||
export class IndexMappings { | ||
private dsl: EsMappings = this.initialDsl; | ||
|
||
constructor( | ||
private initialDsl: EsMappings = DEFAULT_INITIAL_DSL, | ||
mappingExtensions: MappingExtension[] = [] | ||
) { | ||
// ensure that the dsl can be parsed with getRootProperties() and kin | ||
this.setProperties(getRootProperties(this.dsl) || {}); | ||
|
||
// extend this.dsl with each extension (which currently come from uiExports.savedObjectMappings) | ||
mappingExtensions.forEach(({ properties, pluginId }) => { | ||
const rootProperties = getRootProperties(this.dsl); | ||
|
||
const conflicts = Object.keys(properties).filter(key => rootProperties.hasOwnProperty(key)); | ||
|
||
const illegal = Object.keys(properties).filter(key => key.startsWith('_')); | ||
|
||
if (conflicts.length) { | ||
const props = formatListAsProse(conflicts); | ||
const owner = pluginId ? `registered by plugin ${pluginId} ` : ''; | ||
throw new Error(`Mappings for ${props} ${owner}have already been defined`); | ||
} | ||
|
||
if (illegal.length) { | ||
const props = formatListAsProse(illegal); | ||
const owner = pluginId ? `registered by plugin ${pluginId} ` : ''; | ||
throw new Error( | ||
`Property name${ | ||
props.length > 1 ? 's' : '' | ||
} ${props} ${owner}are not allowed to start with an underscore (_)` | ||
); | ||
} | ||
|
||
this.setProperties({ | ||
...rootProperties, | ||
...properties, | ||
}); | ||
}); | ||
} | ||
|
||
public getDsl() { | ||
return cloneDeep(this.dsl); | ||
} | ||
|
||
private setProperties(newProperties: MappingExtension['properties']) { | ||
const rootType = getRootType(this.dsl); | ||
this.dsl = { | ||
...this.dsl, | ||
[rootType]: { | ||
...this.dsl[rootType], | ||
properties: newProperties, | ||
}, | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.