From 0cd807b6eb0d86a87abe2276448bcb5aa906e766 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Thu, 23 Sep 2021 10:58:05 -0700 Subject: [PATCH] Removes dependencies and code no longer required --- .../public/lib/create_field_list.test.ts | 178 ------------------ .../public/lib/create_field_list.ts | 128 ------------- .../public/lib/in_app_url.test.ts | 115 ----------- .../public/lib/in_app_url.ts | 29 --- .../public/lib/index.ts | 2 - 5 files changed, 452 deletions(-) delete mode 100644 src/plugins/saved_objects_management/public/lib/create_field_list.test.ts delete mode 100644 src/plugins/saved_objects_management/public/lib/create_field_list.ts delete mode 100644 src/plugins/saved_objects_management/public/lib/in_app_url.test.ts delete mode 100644 src/plugins/saved_objects_management/public/lib/in_app_url.ts diff --git a/src/plugins/saved_objects_management/public/lib/create_field_list.test.ts b/src/plugins/saved_objects_management/public/lib/create_field_list.test.ts deleted file mode 100644 index f7b60b116a2bf3..00000000000000 --- a/src/plugins/saved_objects_management/public/lib/create_field_list.test.ts +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { SimpleSavedObject, SavedObjectReference } from '../../../../core/public'; -import { savedObjectsServiceMock } from '../../../../core/public/mocks'; -import { createFieldList } from './create_field_list'; - -const savedObjectClientMock = savedObjectsServiceMock.createStartContract().client; - -const createObject = ( - attributes: T, - references: SavedObjectReference[] = [] -): SimpleSavedObject => - new SimpleSavedObject(savedObjectClientMock, { - id: 'id', - type: 'type', - migrationVersion: {}, - attributes, - references, - }); - -describe('createFieldList', () => { - it('generate fields based on the object attributes', () => { - const obj = createObject({ - textField: 'some text', - numberField: 12, - boolField: true, - }); - expect(createFieldList(obj)).toMatchInlineSnapshot(` - Array [ - Object { - "name": "textField", - "type": "text", - "value": "some text", - }, - Object { - "name": "numberField", - "type": "number", - "value": 12, - }, - Object { - "name": "boolField", - "type": "boolean", - "value": true, - }, - Object { - "name": "references", - "type": "array", - "value": "[]", - }, - ] - `); - }); - - it('detects json fields', () => { - const obj = createObject({ - jsonField: `{"data": "value"}`, - }); - expect(createFieldList(obj)).toMatchInlineSnapshot(` - Array [ - Object { - "name": "jsonField", - "type": "json", - "value": "{ - \\"data\\": \\"value\\" - }", - }, - Object { - "name": "references", - "type": "array", - "value": "[]", - }, - ] - `); - }); - - it('handles array fields', () => { - const obj = createObject({ - someArray: [1, 2, 3], - }); - expect(createFieldList(obj)).toMatchInlineSnapshot(` - Array [ - Object { - "name": "someArray", - "type": "array", - "value": "[ - 1, - 2, - 3 - ]", - }, - Object { - "name": "references", - "type": "array", - "value": "[]", - }, - ] - `); - }); - - it(`generates a field for the object's references`, () => { - const obj = createObject( - { - someString: 'foo', - }, - [ - { id: 'ref1', type: 'type', name: 'Ref 1' }, - { id: 'ref12', type: 'other-type', name: 'Ref 2' }, - ] - ); - expect(createFieldList(obj)).toMatchInlineSnapshot(` - Array [ - Object { - "name": "someString", - "type": "text", - "value": "foo", - }, - Object { - "name": "references", - "type": "array", - "value": "[ - { - \\"id\\": \\"ref1\\", - \\"type\\": \\"type\\", - \\"name\\": \\"Ref 1\\" - }, - { - \\"id\\": \\"ref12\\", - \\"type\\": \\"other-type\\", - \\"name\\": \\"Ref 2\\" - } - ]", - }, - ] - `); - }); - - it('recursively collect nested fields', () => { - const obj = createObject({ - firstLevel: { - firstLevelField: 'foo', - secondLevel: { - secondLevelFieldA: 'A', - secondLevelFieldB: 'B', - }, - }, - }); - expect(createFieldList(obj)).toMatchInlineSnapshot(` - Array [ - Object { - "name": "firstLevel.firstLevelField", - "type": "text", - "value": "foo", - }, - Object { - "name": "firstLevel.secondLevel.secondLevelFieldA", - "type": "text", - "value": "A", - }, - Object { - "name": "firstLevel.secondLevel.secondLevelFieldB", - "type": "text", - "value": "B", - }, - Object { - "name": "references", - "type": "array", - "value": "[]", - }, - ] - `); - }); -}); diff --git a/src/plugins/saved_objects_management/public/lib/create_field_list.ts b/src/plugins/saved_objects_management/public/lib/create_field_list.ts deleted file mode 100644 index 329ed80857696b..00000000000000 --- a/src/plugins/saved_objects_management/public/lib/create_field_list.ts +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { forOwn, keyBy, isNumber, isBoolean, isPlainObject, isString } from 'lodash'; -import { SimpleSavedObject } from '../../../../core/public'; -import { castEsToKbnFieldTypeName } from '../../../data/public'; -import { ObjectField } from '../management_section/types'; -import { SavedObjectLoader } from '../../../saved_objects/public'; -import { SavedObjectWithMetadata } from '../types'; - -const maxRecursiveIterations = 20; - -export function createFieldList( - object: SimpleSavedObject | SavedObjectWithMetadata, - service?: SavedObjectLoader -): ObjectField[] { - let fields = Object.entries(object.attributes as Record).reduce( - (objFields, [key, value]) => { - return [...objFields, ...createFields(key, value)]; - }, - [] as ObjectField[] - ); - // Special handling for references which isn't within "attributes" - fields = [...fields, ...createFields('references', object.references)]; - - if (service && (service as any).Class) { - addFieldsFromClass((service as any).Class, fields); - } - - return fields; -} - -/** - * Creates a field definition and pushes it to the memo stack. This function - * is designed to be used in conjunction with _.reduce(). If the - * values is plain object it will recurse through all the keys till it hits - * a string, number or an array. - * - * @param {string} key The key of the field - * @param {mixed} value The value of the field - * @param {array} parents The parent keys to the field - * @returns {array} - */ -const createFields = (key: string, value: any, parents: string[] = []): ObjectField[] => { - const path = [...parents, key]; - if (path.length > maxRecursiveIterations) { - return []; - } - - const field: ObjectField = { type: 'text', name: path.join('.'), value }; - - if (isString(field.value)) { - try { - field.value = JSON.stringify(JSON.parse(field.value), undefined, 2); - field.type = 'json'; - } catch (err) { - field.type = 'text'; - } - } else if (isNumber(field.value)) { - field.type = 'number'; - } else if (Array.isArray(field.value)) { - field.type = 'array'; - field.value = JSON.stringify(field.value, undefined, 2); - } else if (isBoolean(field.value)) { - field.type = 'boolean'; - } else if (isPlainObject(field.value)) { - let fields: ObjectField[] = []; - forOwn(field.value, (childValue, childKey) => { - fields = [...fields, ...createFields(childKey as string, childValue, path)]; - }); - return fields; - } - - return [field]; -}; - -const addFieldsFromClass = function ( - Class: { mapping: Record; searchSource: any }, - fields: ObjectField[] -) { - const fieldMap = keyBy(fields, 'name'); - - forOwn(Class.mapping, (esType, name) => { - if (!name || fieldMap[name]) { - return; - } - - const getFieldTypeFromEsType = () => { - switch (castEsToKbnFieldTypeName(esType)) { - case 'string': - return 'text'; - case 'number': - return 'number'; - case 'boolean': - return 'boolean'; - default: - return 'json'; - } - }; - - fields.push({ - name, - type: getFieldTypeFromEsType(), - value: undefined, - }); - }); - - if (Class.searchSource && !fieldMap['kibanaSavedObjectMeta.searchSourceJSON']) { - fields.push({ - name: 'kibanaSavedObjectMeta.searchSourceJSON', - type: 'json', - value: '{}', - }); - } - - if (!fieldMap.references) { - fields.push({ - name: 'references', - type: 'array', - value: '[]', - }); - } -}; diff --git a/src/plugins/saved_objects_management/public/lib/in_app_url.test.ts b/src/plugins/saved_objects_management/public/lib/in_app_url.test.ts deleted file mode 100644 index 28853f836085c5..00000000000000 --- a/src/plugins/saved_objects_management/public/lib/in_app_url.test.ts +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { Capabilities } from '../../../../core/public'; -import { canViewInApp } from './in_app_url'; - -const createCapabilities = (sections: Record): Capabilities => { - return { - navLinks: {}, - management: {}, - catalogue: {}, - ...sections, - }; -}; - -describe('canViewInApp', () => { - it('should handle saved searches', () => { - let uiCapabilities = createCapabilities({ - discover: { - show: true, - }, - }); - expect(canViewInApp(uiCapabilities, 'search')).toEqual(true); - expect(canViewInApp(uiCapabilities, 'searches')).toEqual(true); - - uiCapabilities = createCapabilities({ - discover: { - show: false, - }, - }); - expect(canViewInApp(uiCapabilities, 'search')).toEqual(false); - expect(canViewInApp(uiCapabilities, 'searches')).toEqual(false); - }); - - it('should handle visualizations', () => { - let uiCapabilities = createCapabilities({ - visualize: { - show: true, - }, - }); - expect(canViewInApp(uiCapabilities, 'visualization')).toEqual(true); - expect(canViewInApp(uiCapabilities, 'visualizations')).toEqual(true); - - uiCapabilities = createCapabilities({ - visualize: { - show: false, - }, - }); - expect(canViewInApp(uiCapabilities, 'visualization')).toEqual(false); - expect(canViewInApp(uiCapabilities, 'visualizations')).toEqual(false); - }); - - it('should handle index patterns', () => { - let uiCapabilities = createCapabilities({ - management: { - kibana: { - indexPatterns: true, - }, - }, - }); - expect(canViewInApp(uiCapabilities, 'index-pattern')).toEqual(true); - expect(canViewInApp(uiCapabilities, 'index-patterns')).toEqual(true); - expect(canViewInApp(uiCapabilities, 'indexPatterns')).toEqual(true); - - uiCapabilities = createCapabilities({ - management: { - kibana: { - indexPatterns: false, - }, - }, - }); - expect(canViewInApp(uiCapabilities, 'index-pattern')).toEqual(false); - expect(canViewInApp(uiCapabilities, 'index-patterns')).toEqual(false); - expect(canViewInApp(uiCapabilities, 'indexPatterns')).toEqual(false); - }); - - it('should handle dashboards', () => { - let uiCapabilities = createCapabilities({ - dashboard: { - show: true, - }, - }); - expect(canViewInApp(uiCapabilities, 'dashboard')).toEqual(true); - expect(canViewInApp(uiCapabilities, 'dashboards')).toEqual(true); - - uiCapabilities = createCapabilities({ - dashboard: { - show: false, - }, - }); - expect(canViewInApp(uiCapabilities, 'dashboard')).toEqual(false); - expect(canViewInApp(uiCapabilities, 'dashboards')).toEqual(false); - }); - - it('should have a default case', () => { - let uiCapabilities = createCapabilities({ - foo: { - show: true, - }, - }); - expect(canViewInApp(uiCapabilities, 'foo')).toEqual(true); - - uiCapabilities = createCapabilities({ - foo: { - show: false, - }, - }); - expect(canViewInApp(uiCapabilities, 'foo')).toEqual(false); - }); -}); diff --git a/src/plugins/saved_objects_management/public/lib/in_app_url.ts b/src/plugins/saved_objects_management/public/lib/in_app_url.ts deleted file mode 100644 index c102ec21a83d0e..00000000000000 --- a/src/plugins/saved_objects_management/public/lib/in_app_url.ts +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { Capabilities } from 'src/core/public'; - -export function canViewInApp(uiCapabilities: Capabilities, type: string): boolean { - switch (type) { - case 'search': - case 'searches': - return uiCapabilities.discover.show as boolean; - case 'visualization': - case 'visualizations': - return uiCapabilities.visualize.show as boolean; - case 'index-pattern': - case 'index-patterns': - case 'indexPatterns': - return uiCapabilities.management.kibana.indexPatterns as boolean; - case 'dashboard': - case 'dashboards': - return uiCapabilities.dashboard.show as boolean; - default: - return uiCapabilities[type].show as boolean; - } -} diff --git a/src/plugins/saved_objects_management/public/lib/index.ts b/src/plugins/saved_objects_management/public/lib/index.ts index aefa5b614f618e..e317bb5e39f735 100644 --- a/src/plugins/saved_objects_management/public/lib/index.ts +++ b/src/plugins/saved_objects_management/public/lib/index.ts @@ -8,7 +8,6 @@ export { fetchExportByTypeAndSearch } from './fetch_export_by_type_and_search'; export { fetchExportObjects } from './fetch_export_objects'; -export { canViewInApp } from './in_app_url'; export { getRelationships } from './get_relationships'; export { getSavedObjectCounts } from './get_saved_object_counts'; export { getSavedObjectLabel } from './get_saved_object_label'; @@ -24,6 +23,5 @@ export { getDefaultTitle } from './get_default_title'; export { findObjects } from './find_objects'; export { bulkGetObjects } from './bulk_get_objects'; export { extractExportDetails, SavedObjectsExportResultDetails } from './extract_export_details'; -export { createFieldList } from './create_field_list'; export { getAllowedTypes } from './get_allowed_types'; export { getTagFindReferences } from './get_tag_references';