Apply code review feedback
Allow more flexible comparisons where kind is not defined or the entity being compared against has some non-string value for `kind` or `spec.type`. Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com>
This commit is contained in:
@@ -2,4 +2,4 @@
|
||||
'@backstage/plugin-catalog': minor
|
||||
---
|
||||
|
||||
Added isEntityWith condition helper
|
||||
Added `isEntityWith` condition helper for `EntitySwitch` case statements.
|
||||
|
||||
@@ -142,6 +142,14 @@ describe('isEntityWith', () => {
|
||||
const checkEntity = isEntityWith({ kind: 'another-type', type: 'service' });
|
||||
expect(checkEntity(missingSpecType)).not.toBeTruthy();
|
||||
});
|
||||
it('allows a check against no criteria', () => {
|
||||
const checkEntity = isEntityWith({});
|
||||
expect(checkEntity(apiKind)).toBeTruthy();
|
||||
});
|
||||
it('allows a check against empty criteria', () => {
|
||||
const checkEntity = isEntityWith({ kind: [], type: [] });
|
||||
expect(checkEntity(apiKind)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isKind', () => {
|
||||
|
||||
@@ -17,20 +17,22 @@
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
|
||||
export interface EntityPredicates {
|
||||
kind: string | string[];
|
||||
kind?: string | string[];
|
||||
type?: string | string[];
|
||||
}
|
||||
|
||||
function strCmp(a: string | undefined, b: string | undefined): boolean {
|
||||
function strCmp(a: unknown, b: string | undefined): boolean {
|
||||
return Boolean(
|
||||
a && a?.toLocaleLowerCase('en-US') === b?.toLocaleLowerCase('en-US'),
|
||||
a &&
|
||||
typeof a === 'string' &&
|
||||
a?.toLocaleLowerCase('en-US') === b?.toLocaleLowerCase('en-US'),
|
||||
);
|
||||
}
|
||||
|
||||
function strCmpAll(value: string | undefined, cmpValues: string | string[]) {
|
||||
function strCmpAll(value: unknown, cmpValues: string | string[]) {
|
||||
return typeof cmpValues === 'string'
|
||||
? strCmp(value, cmpValues)
|
||||
: cmpValues.some(cmpVal => strCmp(value, cmpVal));
|
||||
: cmpValues.length === 0 || cmpValues.some(cmpVal => strCmp(value, cmpVal));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,28 +60,21 @@ export function isResourceType(types: string | string[]) {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* For use in EntitySwitch.Case. Matches if the entity is the specified kind and type (if present).
|
||||
* @public
|
||||
*/
|
||||
export function isEntityWith(predicate: EntityPredicates) {
|
||||
return (entity: Entity) => {
|
||||
if (!strCmpAll(entity.kind, predicate.kind)) {
|
||||
if (predicate.kind && !strCmpAll(entity.kind, predicate.kind)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
!predicate.type ||
|
||||
(Array.isArray(predicate.type) && predicate.type.length === 0)
|
||||
) {
|
||||
// there's no type check, return true
|
||||
return true;
|
||||
} else if (typeof entity.spec?.type !== 'string') {
|
||||
// we'll reject any entity's with some non-string (including undefined/null) spec.type value
|
||||
if (predicate.type && !strCmpAll(entity.spec?.type, predicate.type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strCmpAll(entity.spec.type, predicate.type);
|
||||
// there's no type check, return true
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
|
||||
export { EntitySwitch } from './EntitySwitch';
|
||||
export type { EntitySwitchProps, EntitySwitchCaseProps } from './EntitySwitch';
|
||||
export type { EntityPredicates } from './conditions';
|
||||
export {
|
||||
isKind,
|
||||
isNamespace,
|
||||
isComponentType,
|
||||
isResourceType,
|
||||
isEntityWith,
|
||||
} from './conditions';
|
||||
|
||||
Reference in New Issue
Block a user