UNPKG

@k8ts/instruments

Version:

A collection of utilities and core components for k8ts.

125 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KindMap = void 0; const immutable_1 = require("immutable"); const api_kind_1 = require("../api-kind"); const error_1 = require("../error"); const ref_key_1 = require("../ref-key"); const separator = "/"; class KindMap { _entryMap; _parent; constructor(_entryMap = (0, immutable_1.Map)([]), _parent = undefined) { this._entryMap = _entryMap; this._parent = _parent; } refKey(kind, name) { const trueKind = this.getKind(kind); return new ref_key_1.RefKey.RefKey(trueKind, name); } _bindEntry(entry) { for (const key of [entry.kind, entry.class, entry.ident]) { this._entryMap = this._entryMap.set(key, entry); } } add(kind, klass) { this._bindEntry({ kind: kind.name, class: klass, ident: kind }); } parse(ref) { const result = this.tryParse(ref); if (!result) { throw new error_1.InstrumentsError(`Could not parse reference key: ${ref}`); } return result; } tryParse(ref) { if (typeof ref !== "string" && typeof ref !== "object") { return undefined; } if (ref == null) { return undefined; } if (ref instanceof ref_key_1.RefKey.RefKey) { return ref; } if (typeof ref === "object") { return undefined; } const [kind, name] = ref.split(separator).map(s => s.trim()); if (!kind || !name) { return undefined; } return this.refKey(kind, name); } get kinds() { return this._entryMap .keySeq() .filter(k => typeof k === "string") .toSet(); } _unknownNameError(kind) { return new error_1.InstrumentsError(`The shorthand name ${kind} is not registered`); } _unknownIdentError(ident) { return new error_1.InstrumentsError(`The kind identifier ${ident} is not registered`); } _unknownClassError(klass) { return new error_1.InstrumentsError(`The class ${klass.name} is not registered`); } _convert(something) { if (typeof something === "string") { if (something.includes("/")) { return this.parse(something).kind; } return something; } else if (typeof something === "function" || something instanceof api_kind_1.Kind.Identifier) { return something; } else if (something instanceof ref_key_1.RefKey.RefKey) { return something.kind; } throw new error_1.InstrumentsError(`Invalid argument ${something}`); } _getEntry(key) { const converted = this._convert(key); const entry = this._tryGetEntry(key); if (!entry) { if (typeof converted === "string") { throw this._unknownNameError(converted); } else if (converted instanceof api_kind_1.Kind.Identifier) { throw this._unknownIdentError(converted); } else if (typeof converted === "function") { throw this._unknownClassError(converted); } } return entry; } _tryGetEntry(key) { const converted = this._convert(key); return this._entryMap.get(converted) ?? this._parent?._tryGetEntry(converted) ?? undefined; } tryGetKind(kindOrIdent) { return this._tryGetEntry(kindOrIdent)?.ident; } getKind(kindOrClass) { return this._getEntry(kindOrClass).ident; } tryGetClass(kindOrIdent) { return this._tryGetEntry(kindOrIdent)?.class; } getClass(kindOrClass) { return this._getEntry(kindOrClass).class; } has(kindOrClass) { return this._entryMap.has(kindOrClass); } } exports.KindMap = KindMap; //# sourceMappingURL=index.js.map