-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…43273) - Fixes a regression where values were not properly extracted from nested objects. - Moves inline code we had to solve this to a utility function getNestedProperty(). Kibana's idx is a lodash-get replacement with TypeScript support. However, it requires that you'd know the accessor up front, it doesn't work with dynamic string values. getNestedProperty() allows you to pass a string like lodash-get, but it uses idx on the inside so you still get TypeScript support.
- Loading branch information
Showing
6 changed files
with
120 additions
and
43 deletions.
There are no files selected for viewing
30 changes: 15 additions & 15 deletions
30
..._frame_new_pivot/components/source_index_preview/__snapshots__/expanded_row.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getNestedProperty } from './object_utils'; | ||
|
||
describe('object_utils', () => { | ||
test('getNestedProperty()', () => { | ||
const testObj = { | ||
the: { | ||
nested: { | ||
value: 'the-nested-value', | ||
}, | ||
}, | ||
}; | ||
|
||
const test1 = getNestedProperty(testObj, 'the'); | ||
expect(typeof test1).toBe('object'); | ||
expect(Object.keys(test1)).toStrictEqual(['nested']); | ||
|
||
const test2 = getNestedProperty(testObj, 'the$'); | ||
expect(typeof test2).toBe('undefined'); | ||
|
||
const test3 = getNestedProperty(testObj, 'the$', 'the-default-value'); | ||
expect(typeof test3).toBe('string'); | ||
expect(test3).toBe('the-default-value'); | ||
|
||
const test4 = getNestedProperty(testObj, 'the.neSted'); | ||
expect(typeof test4).toBe('undefined'); | ||
|
||
const test5 = getNestedProperty(testObj, 'the.nested'); | ||
expect(typeof test5).toBe('object'); | ||
expect(Object.keys(test5)).toStrictEqual(['value']); | ||
|
||
const test6 = getNestedProperty(testObj, 'the.nested.vaLue'); | ||
expect(typeof test6).toBe('undefined'); | ||
|
||
const test7 = getNestedProperty(testObj, 'the.nested.value'); | ||
expect(typeof test7).toBe('string'); | ||
expect(test7).toBe('the-nested-value'); | ||
|
||
const test8 = getNestedProperty(testObj, 'the.nested.value.doesntExist'); | ||
expect(typeof test8).toBe('undefined'); | ||
|
||
const test9 = getNestedProperty(testObj, 'the.nested.value.doesntExist', 'the-default-value'); | ||
expect(typeof test9).toBe('string'); | ||
expect(test9).toBe('the-default-value'); | ||
}); | ||
}); |
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,17 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { idx } from '@kbn/elastic-idx'; | ||
|
||
// This is similar to lodash's get() except that it's TypeScript aware and is able to infer return types. | ||
// It splits the attribute key string and uses reduce with an idx check to access nested attributes. | ||
export const getNestedProperty = ( | ||
obj: Record<string, any>, | ||
accessor: string, | ||
defaultValue?: any | ||
) => { | ||
return accessor.split('.').reduce((o, i) => idx(o, _ => _[i]), obj) || defaultValue; | ||
}; |