-
Notifications
You must be signed in to change notification settings - Fork 219
feat(s2-core)S2内核支持通过“路径”读取嵌套数据结构中的字段值('a.b.c' 或 'a.b[0].c'), 嵌套层级不限(兼容原有平铺字段)。 #3233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xincheng123
wants to merge
3
commits into
antvis:next
Choose a base branch
from
xincheng123:next
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−27
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/s2-core/__tests__/unit/data-set/cell-data.nested.spec.ts
This file contains hidden or 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,20 @@ | ||
| import { CellData } from '@/data-set/cell-data'; | ||
|
|
||
| describe('CellData nested value access', () => { | ||
| const row = { | ||
| name: '中国', | ||
| user: { region: '华东', city: '上海' }, | ||
| metrics: { price: 100, city: { address: '测试名称' } }, | ||
| } as Record<string, any>; | ||
|
|
||
| test('VALUE_FIELD from nested extraField', () => { | ||
| const cell = CellData.getCellData(row as any, 'metrics.city.address'); | ||
|
|
||
| expect((cell as any).$$value$$ ?? (cell as any).value).toBeUndefined(); | ||
| expect((cell as any).raw).toBeDefined(); | ||
| expect((cell as any).value).toBeUndefined(); | ||
| expect((cell as any).getValueByField('metrics.city.address')).toBe( | ||
| '测试名称', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,26 @@ | ||
| import { getByPath, hasByPath } from '../../../src/utils/accessor'; | ||
|
|
||
| describe('utils/accessor nested path', () => { | ||
| const row = { | ||
| name: '中国', | ||
| user: { region: '华东', city: '上海' }, | ||
| metrics: { price: 100, city: { address: '测试名称' } }, | ||
| } as Record<string, any>; | ||
|
|
||
| test('getByPath supports flat key', () => { | ||
| expect(getByPath(row, 'name')).toBe('中国'); | ||
| }); | ||
|
|
||
| test('getByPath supports level-2 path', () => { | ||
| expect(getByPath(row, 'user.region')).toBe('华东'); | ||
| }); | ||
|
|
||
| test('getByPath supports level-3 path', () => { | ||
| expect(getByPath(row, 'metrics.city.address')).toBe('测试名称'); | ||
| }); | ||
|
|
||
| test('hasByPath works for existing and missing paths', () => { | ||
| expect(hasByPath(row, 'metrics.price')).toBe(true); | ||
| expect(hasByPath(row, 'metrics.missing')).toBe(false); | ||
| }); | ||
| }); |
28 changes: 28 additions & 0 deletions
28
packages/s2-core/__tests__/unit/utils/pivot-data-set.nested.spec.ts
This file contains hidden or 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,28 @@ | ||
| import { | ||
| getExistValues, | ||
| transformDimensionsValues, | ||
| } from '../../../src/utils/dataset/pivot-data-set'; | ||
|
|
||
| describe('pivot dataset nested paths', () => { | ||
| const row = { | ||
| name: '中国', | ||
| user: { region: '华东', city: '上海' }, | ||
| metrics: { price: 100, city: { address: '测试名称' } }, | ||
| } as Record<string, any>; | ||
|
|
||
| test('transformDimensionsValues supports nested paths', () => { | ||
| const dims = ['user.region', 'user.city', 'metrics.city.address']; | ||
| const values = transformDimensionsValues(row, dims); | ||
|
|
||
| expect(values).toEqual(['华东', '上海', '测试名称']); | ||
| }); | ||
|
|
||
| test('getExistValues supports nested values', () => { | ||
| const values = ['name', 'metrics.price', 'metrics.missing']; | ||
|
|
||
| expect(getExistValues(row as any, values)).toEqual([ | ||
| 'name', | ||
| 'metrics.price', | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,29 @@ | ||
| import { get, has } from 'lodash'; | ||
|
|
||
| export function getByPath<T = any>( | ||
| record: Record<string, any>, | ||
| field: string, | ||
| ): T { | ||
| if (!record || !field) { | ||
| return undefined as any; | ||
| } | ||
|
|
||
| // fast path for flat keys | ||
| if (field.indexOf('.') === -1 && field.indexOf('[') === -1) { | ||
| return record[field] as any; | ||
| } | ||
|
|
||
| return get(record, field) as any; | ||
| } | ||
|
|
||
| export function hasByPath(record: Record<string, any>, field: string): boolean { | ||
| if (!record || !field) { | ||
| return false; | ||
| } | ||
|
|
||
| if (field.indexOf('.') === -1 && field.indexOf('[') === -1) { | ||
| return Object.prototype.hasOwnProperty.call(record, field); | ||
| } | ||
|
|
||
| return has(record, field); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.