diff --git a/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.test.ts b/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.test.ts index 5d15ba2ca779a..f8472b8b92366 100644 --- a/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.test.ts +++ b/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.test.ts @@ -51,4 +51,40 @@ describe('unflattenObject', () => { }, }); }); + + it('handles nested arrays', () => { + expect( + unflattenObject({ + nested: [[{ 'first.second': 'foo' }, { 'first.second': 'bar' }]], + mixed: [[1, 2, { 'first.second': 'foo' }]], + }) + ).toEqual({ + nested: [[{ first: { second: 'foo' } }, { first: { second: 'bar' } }]], + mixed: [[1, 2, { first: { second: 'foo' } }]], + }); + }); + + it('unflattens nested flattened objects', () => { + expect( + unflattenObject({ + 'my.flattened.parent.key': { + 'an.internal.key': 1, + }, + }) + ).toEqual({ + my: { + flattened: { + parent: { + key: { + an: { + internal: { + key: 1, + }, + }, + }, + }, + }, + }, + }); + }); }); diff --git a/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.ts b/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.ts index d32f0c13bbeff..e6319bf8bdd88 100644 --- a/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.ts +++ b/src/platform/packages/shared/kbn-object-utils/src/unflatten_object.ts @@ -18,13 +18,18 @@ export function unflattenObject>( for (const key in source) { const val = source[key as keyof typeof source]; if (Array.isArray(val)) { - const unflattenedArray = val.map((item: unknown) => { - if (item && typeof item === 'object' && !Array.isArray(item)) { + const unflattenedArray = val.map(function unflattenArray(item: unknown): unknown { + if (Array.isArray(item)) { + return item.map(unflattenArray); + } + if (item !== null && typeof item === 'object') { return unflattenObject(item); } return item; }); set(target, key, unflattenedArray); + } else if (val !== null && typeof val === 'object') { + set(target, key, unflattenObject(val)); } else { set(target, key, val); } diff --git a/x-pack/solutions/observability/packages/utils-common/moon.yml b/x-pack/solutions/observability/packages/utils-common/moon.yml index ef3a8d9caf433..d232998d84b11 100644 --- a/x-pack/solutions/observability/packages/utils-common/moon.yml +++ b/x-pack/solutions/observability/packages/utils-common/moon.yml @@ -19,8 +19,6 @@ project: sourceRoot: x-pack/solutions/observability/packages/utils-common dependsOn: - '@kbn/es-query' - - '@kbn/safer-lodash-set' - - '@kbn/utility-types' tags: - shared-common - package diff --git a/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.test.ts b/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.test.ts deleted file mode 100644 index aa25821b4ac14..0000000000000 --- a/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.test.ts +++ /dev/null @@ -1,52 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { unflattenObject } from './unflatten_object'; - -describe('unflattenObject', () => { - it('unflattens deeply nested objects', () => { - expect(unflattenObject({ 'first.second.third': 'third' })).toEqual({ - first: { - second: { - third: 'third', - }, - }, - }); - }); - - it('does not unflatten arrays', () => { - expect( - unflattenObject({ - simpleArray: ['0', '1', '2'], - complexArray: [{ one: 'one', two: 'two', three: 'three' }], - 'nested.array': [0, 1, 2], - 'complex.nested': [{ one: 'one', two: 'two', 'first.second': 'foo', 'first.third': 'bar' }], - }) - ).toEqual({ - simpleArray: ['0', '1', '2'], - complexArray: [{ one: 'one', two: 'two', three: 'three' }], - nested: { - array: [0, 1, 2], - }, - complex: { - nested: [{ one: 'one', two: 'two', first: { second: 'foo', third: 'bar' } }], - }, - }); - }); - - it('handles null values correctly', () => { - expect( - unflattenObject({ - 'agent.name': null, - }) - ).toEqual({ - agent: { - name: null, - }, - }); - }); -}); diff --git a/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.ts b/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.ts deleted file mode 100644 index 88eb9003f5327..0000000000000 --- a/x-pack/solutions/observability/packages/utils-common/object/unflatten_object.ts +++ /dev/null @@ -1,32 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { set } from '@kbn/safer-lodash-set'; -import type { DedotObject } from '@kbn/utility-types'; - -export function unflattenObject>( - source: T, - target: Record = {} -): DedotObject { - // eslint-disable-next-line guard-for-in - for (const key in source) { - const val = source[key as keyof typeof source]; - if (Array.isArray(val)) { - const unflattenedArray = val.map((item: unknown) => { - if (item && typeof item === 'object' && !Array.isArray(item)) { - return unflattenObject(item); - } - return item; - }); - set(target, key, unflattenedArray); - } else { - set(target, key, val); - } - } - - return target as DedotObject; -} diff --git a/x-pack/solutions/observability/packages/utils-common/tsconfig.json b/x-pack/solutions/observability/packages/utils-common/tsconfig.json index 2ba42d09bb015..29ece47c5142b 100644 --- a/x-pack/solutions/observability/packages/utils-common/tsconfig.json +++ b/x-pack/solutions/observability/packages/utils-common/tsconfig.json @@ -17,7 +17,5 @@ ], "kbn_references": [ "@kbn/es-query", - "@kbn/safer-lodash-set", - "@kbn/utility-types", ] } diff --git a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/moon.yml b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/moon.yml index 5ca075498b206..610cac0c4bc43 100644 --- a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/moon.yml +++ b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/moon.yml @@ -93,6 +93,7 @@ dependsOn: - '@kbn/zod' - '@kbn/connector-schemas' - '@kbn/onechat-plugin' + - '@kbn/object-utils' tags: - plugin - prod diff --git a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/scripts/evaluation/read_kibana_config.ts b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/scripts/evaluation/read_kibana_config.ts index 66fb66bf06aeb..76f3affd9b18e 100644 --- a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/scripts/evaluation/read_kibana_config.ts +++ b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/scripts/evaluation/read_kibana_config.ts @@ -9,7 +9,7 @@ import path from 'path'; import fs from 'fs'; import yaml from 'js-yaml'; import { identity, pickBy } from 'lodash'; -import { unflattenObject } from '@kbn/observability-utils-common/object/unflatten_object'; +import { unflattenObject } from '@kbn/object-utils'; export type KibanaConfig = ReturnType; diff --git a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/tsconfig.json b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/tsconfig.json index edb4c5b8b94eb..cd787da034a33 100644 --- a/x-pack/solutions/observability/plugins/observability_ai_assistant_app/tsconfig.json +++ b/x-pack/solutions/observability/plugins/observability_ai_assistant_app/tsconfig.json @@ -88,7 +88,8 @@ "@kbn/utility-types-jest", "@kbn/zod", "@kbn/connector-schemas", - "@kbn/onechat-plugin" + "@kbn/onechat-plugin", + "@kbn/object-utils", ], "exclude": ["target/**/*"] }