Skip to content

Commit

Permalink
[bug][6.x] Revert mappings lib changes (#22004)
Browse files Browse the repository at this point in the history
* Remove new library

* Reinstate old mappings lib

* Update paths to mappings lib

* Remove remaining mappings lib
  • Loading branch information
archanid authored Aug 15, 2018
1 parent 27bc4d2 commit 5098b5f
Show file tree
Hide file tree
Showing 27 changed files with 293 additions and 396 deletions.
6 changes: 0 additions & 6 deletions src/core/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ Platform Server Modules
Http Server
-----------
TODO: explain

Saved Objects Service
-----------

Elasticsearch Service
-----------

This file was deleted.

92 changes: 0 additions & 92 deletions src/core/server/saved_objects/mappings/index_mappings.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/core/server/saved_objects/mappings/lib/types.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/core/server/saved_objects/mappings/utils/index.ts

This file was deleted.

37 changes: 0 additions & 37 deletions src/core/server/saved_objects/mappings/utils/prose.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { times, cloneDeep, pick, partition } from 'lodash';
import Chance from 'chance';

import { patchKibanaIndex } from '../patch_kibana_index';
import { getRootProperties, getRootType } from '../../../../core/server/saved_objects/mappings';
import { getRootProperties, getRootType } from '../../../../server/mappings';

const chance = new Chance();

Expand Down
2 changes: 1 addition & 1 deletion src/core_plugins/elasticsearch/lib/patch_kibana_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getTypes,
getRootType,
getRootProperties
} from '../../../core/server/saved_objects/mappings';
} from '../../../server/mappings';

/**
* Checks that the root type in the kibana index has all of the
Expand Down
2 changes: 1 addition & 1 deletion src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import * as Plugins from './plugins';
import { indexPatternsMixin } from './index_patterns';
import { savedObjectsMixin } from './saved_objects';
import { sampleDataMixin } from './sample_data';
import { kibanaIndexMappingsMixin } from './saved_objects/mappings';
import { kibanaIndexMappingsMixin } from './mappings';
import { urlShorteningMixin } from './url_shortening';
import { serverExtensionsMixin } from './server_extensions';
import { uiMixin } from '../ui';
Expand Down
5 changes: 5 additions & 0 deletions src/server/mappings/__snapshots__/index_mappings.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`server/mapping/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[`server/mapping/index_mapping constructor throws if any of the new properties start with _ 1`] = `"Property names _foo are not allowed to start with an underscore (_)"`;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

export { IndexMappings } from './index_mappings';
export {
kibanaIndexMappingsMixin
} from './kibana_index_mappings_mixin';

export {
getTypes,
Expand Down
90 changes: 90 additions & 0 deletions src/server/mappings/index_mappings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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, isPlainObject } from 'lodash';

import { formatListAsProse } from '../../utils';
import { getRootProperties, getRootType } from './lib';

const DEFAULT_INITIAL_DSL = {
rootType: {
type: 'object',
properties: {},
},
};

export class IndexMappings {
constructor(initialDsl = DEFAULT_INITIAL_DSL, mappingExtensions = []) {
this._dsl = cloneDeep(initialDsl);
if (!isPlainObject(this._dsl)) {
throw new TypeError('initial mapping must be an object');
}

// ensure that we have a properties object in the dsl
// and 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
});
});
}

getDsl() {
return cloneDeep(this._dsl);
}

_setProperties(newProperties) {
const rootType = getRootType(this._dsl);
this._dsl = {
...this._dsl,
[rootType]: {
...this._dsl[rootType],
properties: newProperties
}
};
}
}
Loading

0 comments on commit 5098b5f

Please sign in to comment.