diff --git a/packages/catalog-model/src/entity/index.ts b/packages/catalog-model/src/entity/index.ts index e267c607e1..572df63557 100644 --- a/packages/catalog-model/src/entity/index.ts +++ b/packages/catalog-model/src/entity/index.ts @@ -34,6 +34,7 @@ export { parseEntityName, parseEntityRef, serializeEntityRef, + stringifyEntityRef, } from './ref'; export { entityHasChanges, diff --git a/packages/catalog-model/src/entity/ref.ts b/packages/catalog-model/src/entity/ref.ts index bf34962d01..7db9f2781c 100644 --- a/packages/catalog-model/src/entity/ref.ts +++ b/packages/catalog-model/src/entity/ref.ts @@ -172,6 +172,7 @@ export function parseEntityRef( * special/reserved characters, it outputs the string form, otherwise it * outputs the compound form. * + * @deprecated Use `stringifyEntityRef` instead * @param ref The reference to serialize * @returns The same reference on either string or compound form */ @@ -212,6 +213,33 @@ export function serializeEntityRef( return `${kind ? `${kind}:` : ''}${namespace ? `${namespace}/` : ''}${name}`; } +/** + * Takes an entity or entity name/reference, and returns the string form of an + * entity ref. + * + * @param ref The reference to serialize + * @returns The same reference on either string or compound form + */ +export function stringifyEntityRef( + ref: Entity | { kind?: string; namespace?: string; name: string }, +): string { + 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; + } + + return `${kind ? `${kind}:` : ''}${namespace ? `${namespace}/` : ''}${name}`; +} + /** * Compares an entity to either a string reference or a compound reference. *