From 67e13effa1e2507e1f8eb12679b8a9721c83594b Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 3 Mar 2021 16:48:07 +0100 Subject: [PATCH] catalog-model: add stringifyEntityRef and deprecate serializeEntityRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ben Lambert Co-authored-by: Patrik Oldsberg Co-authored-by: Fredrik Adelöw Signed-off-by: Johan Haals --- packages/catalog-model/src/entity/index.ts | 1 + packages/catalog-model/src/entity/ref.ts | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) 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. *