-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: filter out ADMIN application and add feature dependency logic #49
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
isFeatureDependBySelectedFeatures, | ||
getFinalFeatureIdsByDependency, | ||
generateFeatureDependencyMap, | ||
} from './feature'; | ||
|
||
describe('feature utils', () => { | ||
describe('isFeatureDependBySelectedFeatures', () => { | ||
it('should return true', () => { | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['a'] })).toBe(true); | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['a', 'c'] })).toBe(true); | ||
}); | ||
it('should return false', () => { | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], { b: ['c'] })).toBe(false); | ||
expect(isFeatureDependBySelectedFeatures('a', ['b'], {})).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getFinalFeatureIdsByDependency', () => { | ||
it('should return consistent feature ids', () => { | ||
expect(getFinalFeatureIdsByDependency(['a'], { a: ['b'] }, ['c', 'd'])).toStrictEqual([ | ||
'c', | ||
'd', | ||
'a', | ||
'b', | ||
]); | ||
expect(getFinalFeatureIdsByDependency(['a'], { a: ['b', 'e'] }, ['c', 'd'])).toStrictEqual([ | ||
'c', | ||
'd', | ||
'a', | ||
'b', | ||
'e', | ||
]); | ||
}); | ||
}); | ||
|
||
it('should generate consistent features dependency map', () => { | ||
expect( | ||
generateFeatureDependencyMap([ | ||
{ id: 'a', dependencies: { b: { type: 'required' }, c: { type: 'optional' } } }, | ||
{ id: 'b', dependencies: { c: { type: 'required' } } }, | ||
]) | ||
).toEqual({ | ||
a: ['b'], | ||
b: ['c'], | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { App } from '../../../../../core/public'; | ||
|
||
export const isFeatureDependBySelectedFeatures = ( | ||
featureId: string, | ||
selectedFeatureIds: string[], | ||
featureDependencies: { [key: string]: string[] } | ||
) => | ||
selectedFeatureIds.some((selectedFeatureId) => | ||
(featureDependencies[selectedFeatureId] || []).some((dependencies) => | ||
dependencies.includes(featureId) | ||
) | ||
); | ||
|
||
/** | ||
* | ||
* Generate new feature id list based the old feature id list | ||
* and feature dependencies map. The feature dependency map may | ||
* has duplicate ids with old feature id list. Use set here to | ||
* get the unique feature ids. | ||
* | ||
* @param featureIds a feature id list need to add based old feature id list | ||
* @param featureDependencies a feature dependencies map to get depended feature ids | ||
* @param oldFeatureIds a feature id list that represent current feature id selection states | ||
*/ | ||
export const getFinalFeatureIdsByDependency = ( | ||
featureIds: string[], | ||
featureDependencies: { [key: string]: string[] }, | ||
oldFeatureIds: string[] = [] | ||
) => | ||
Array.from( | ||
new Set([ | ||
...oldFeatureIds, | ||
...featureIds.reduce( | ||
(pValue, featureId) => [...pValue, ...(featureDependencies[featureId] || [])], | ||
featureIds | ||
), | ||
]) | ||
); | ||
|
||
export const generateFeatureDependencyMap = ( | ||
allFeatures: Array<Pick<App, 'id' | 'dependencies'>> | ||
) => | ||
allFeatures.reduce<{ [key: string]: string[] }>( | ||
(pValue, { id, dependencies }) => | ||
dependencies | ||
? { | ||
...pValue, | ||
[id]: [ | ||
...(pValue[id] || []), | ||
...Object.keys(dependencies).filter((key) => dependencies[key].type === 'required'), | ||
], | ||
} | ||
: pValue, | ||
{} | ||
); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is a little bit large(about 300 lines), maybe we can extract some filter functions, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can separate these util function to a single file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's
oldFeatureIds
? Maybe you could leave some comments on this function?