catalog-model: Remove serializeEntityRef

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-02-11 14:10:58 +01:00
committed by Fredrik Adelöw
parent f8e3fe38c0
commit edbc03814a
8 changed files with 20 additions and 132 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-catalog-react': patch
'@backstage/plugin-todo-backend': patch
---
Replace usage of `serializeEntityRef` with `stringifyEntityRef`
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/catalog-model': minor
---
Remove deprecated `serializeEntityRef` which is replaced by `stringifyEntityRef`.
-11
View File
@@ -489,17 +489,6 @@ export class SchemaValidEntityPolicy implements EntityPolicy {
enforce(entity: Entity): Promise<Entity>;
}
// @public @deprecated
export function serializeEntityRef(
ref:
| Entity
| {
kind?: string;
namespace?: string;
name: string;
},
): EntityRef;
// @public
export const SOURCE_LOCATION_ANNOTATION = 'backstage.io/source-location';
@@ -40,7 +40,6 @@ export {
getEntityName,
parseEntityName,
parseEntityRef,
serializeEntityRef,
stringifyEntityRef,
} from './ref';
export type { EntityRefContext } from './ref';
+1 -64
View File
@@ -16,12 +16,7 @@
import { ENTITY_DEFAULT_NAMESPACE } from './constants';
import { Entity } from './Entity';
import {
compareEntityToRef,
parseEntityName,
parseEntityRef,
serializeEntityRef,
} from './ref';
import { compareEntityToRef, parseEntityName, parseEntityRef } from './ref';
describe('ref', () => {
describe('parseEntityName', () => {
@@ -329,64 +324,6 @@ describe('ref', () => {
});
});
describe('serializeEntityRef', () => {
it('handles partials', () => {
expect(
serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c' }),
).toEqual('a:b/c');
expect(serializeEntityRef({ namespace: 'b', name: 'c' })).toEqual('b/c');
expect(serializeEntityRef({ kind: 'a', name: 'c' })).toEqual('a:c');
expect(serializeEntityRef({ name: 'c' })).toEqual('c');
});
it('handles entities', () => {
const entityWithNamespace: Entity = {
apiVersion: 'a',
kind: 'b',
metadata: {
name: 'c',
namespace: 'd',
},
};
const entityWithoutNamespace: Entity = {
apiVersion: 'a',
kind: 'b',
metadata: {
name: 'c',
},
};
expect(serializeEntityRef(entityWithNamespace)).toEqual('b:d/c');
expect(serializeEntityRef(entityWithoutNamespace)).toEqual('b:c');
});
it('picks the least complex form', () => {
expect(
serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c' }),
).toEqual('a:b/c');
expect(serializeEntityRef({ namespace: 'b', name: 'c' })).toEqual('b/c');
expect(serializeEntityRef({ kind: 'a', name: 'c' })).toEqual('a:c');
expect(serializeEntityRef({ name: 'c' })).toEqual('c');
expect(
serializeEntityRef({ kind: 'a:x', namespace: 'b', name: 'c' }),
).toEqual({ kind: 'a:x', namespace: 'b', name: 'c' });
expect(
serializeEntityRef({ kind: 'a/x', namespace: 'b', name: 'c' }),
).toEqual({ kind: 'a/x', namespace: 'b', name: 'c' });
expect(
serializeEntityRef({ kind: 'a', namespace: 'b:x', name: 'c' }),
).toEqual({ kind: 'a', namespace: 'b:x', name: 'c' });
expect(
serializeEntityRef({ kind: 'a', namespace: 'b/x', name: 'c' }),
).toEqual({ kind: 'a', namespace: 'b/x', name: 'c' });
expect(
serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c:x' }),
).toEqual({ kind: 'a', namespace: 'b', name: 'c:x' });
expect(
serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c/x' }),
).toEqual({ kind: 'a', namespace: 'b', name: 'c/x' });
});
});
describe('compareEntityToRef', () => {
const entityWithNamespace: Entity = {
apiVersion: 'a',
-48
View File
@@ -184,54 +184,6 @@ export function parseEntityRef(
};
}
/**
* Takes an entity reference or name, and outputs an entity reference on the
* most compact form possible. I.e. if the parts do not contain any
* special/reserved characters, it outputs the string form, otherwise it
* outputs the compound form.
*
* @public
* @deprecated Use `stringifyEntityRef` instead
* @param ref - The reference to serialize
* @returns The same reference on either string or compound form
*/
export function serializeEntityRef(
ref:
| Entity
| {
kind?: string;
namespace?: string;
name: string;
},
): EntityRef {
let kind;
let namespace;
let name;
if ('metadata' in ref) {
kind = ref.kind;
namespace = ref.metadata.namespace;
name = ref.metadata.name;
} else {
kind = ref.kind;
namespace = ref.namespace;
name = ref.name;
}
if (
kind?.includes(':') ||
kind?.includes('/') ||
namespace?.includes(':') ||
namespace?.includes('/') ||
name.includes(':') ||
name.includes('/')
) {
return { kind, namespace, name };
}
return `${kind ? `${kind}:` : ''}${namespace ? `${namespace}/` : ''}${name}`;
}
/**
* Takes an entity or entity name/reference, and returns the string form of an
* entity ref.
@@ -18,7 +18,7 @@ import {
Entity,
EntityName,
ENTITY_DEFAULT_NAMESPACE,
serializeEntityRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
export function formatEntityRefTitle(
@@ -46,10 +46,10 @@ export function formatEntityRefTitle(
kind = kind.toLocaleLowerCase('en-US');
return `${serializeEntityRef({
return `${stringifyEntityRef({
kind:
defaultKind && defaultKind.toLocaleLowerCase('en-US') === kind
? undefined
? ''
: kind,
name,
namespace,
@@ -19,9 +19,9 @@ import { CatalogApi } from '@backstage/catalog-client';
import {
LOCATION_ANNOTATION,
SOURCE_LOCATION_ANNOTATION,
serializeEntityRef,
Entity,
parseLocationRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { TodoReader } from '../lib';
import { ListTodosRequest, ListTodosResponse, TodoService } from './types';
@@ -71,7 +71,7 @@ export class TodoReaderService implements TodoService {
});
if (!entity) {
throw new NotFoundError(
`Entity not found, ${serializeEntityRef(req.entity)}`,
`Entity not found, ${stringifyEntityRef(req.entity)}`,
);
}
@@ -133,7 +133,7 @@ export class TodoReaderService implements TodoService {
const parsed = parseLocationRef(sourceLocation);
if (parsed.type !== 'url') {
throw new InputError(
`Invalid entity source location type for ${serializeEntityRef(
`Invalid entity source location type for ${stringifyEntityRef(
entity,
)}, got '${parsed.type}'`,
);
@@ -146,7 +146,7 @@ export class TodoReaderService implements TodoService {
const parsed = parseLocationRef(location);
if (parsed.type !== 'url') {
throw new InputError(
`Invalid entity location type for ${serializeEntityRef(
`Invalid entity location type for ${stringifyEntityRef(
entity,
)}, got '${parsed.type}'`,
);
@@ -154,7 +154,7 @@ export class TodoReaderService implements TodoService {
return parsed.target;
}
throw new InputError(
`No entity location annotation found for ${serializeEntityRef(entity)}`,
`No entity location annotation found for ${stringifyEntityRef(entity)}`,
);
}
}