@k8ts/metadata
Version:
K8ts tools for working with k8ts metadata.
250 lines • 7.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Meta = void 0;
const immutable_1 = require("immutable");
const error_1 = require("./error");
const key_1 = require("./key");
const parse_key_1 = require("./key/parse-key");
const repr_1 = require("./key/repr");
var Meta;
(function (Meta_1) {
function _checkNameValue(what, v) {
if (!parse_key_1.pNameValue.parse(v).isOk) {
throw new error_1.MetadataError(`Invalid ${what}: ${v}`);
}
(0, repr_1.checkMetaString)(what, v, 63);
}
Meta_1._checkNameValue = _checkNameValue;
function _checkValue(key, v) {
if (key.metaType === "label") {
(0, repr_1.checkMetaString)(`value of ${key.str}`, v, 63);
}
else if (key.metaType === "core") {
_checkNameValue(`value of ${key.str}`, v);
}
}
Meta_1._checkValue = _checkValue;
class Meta {
_dict;
constructor(_dict) {
this._dict = _dict;
for (const [key, value] of _dict.entries()) {
_checkValue(key, value);
}
}
[Symbol.iterator]() {
return this._dict.entries()[Symbol.iterator]();
}
_create(raw) {
return new Meta(raw);
}
_createWith(f) {
return this._create(f(this._dict));
}
add(a, b) {
return this._createWith(raw => {
const parsed = _pairToMap([a, b]);
const newMap = raw.mergeWith((prev, cur, key) => {
throw new error_1.MetadataError(`Duplicate entry for ${key}, was ${prev} now ${cur}`, {
key: key.str
});
}, parsed);
return newMap;
});
}
equals(other) {
return this._dict.equals(make(other)._dict);
}
section(key) {
return this._createWith(raw => {
return raw.mapKeys(k => k.section(key));
});
}
overwrite(a, b) {
if (a === undefined) {
return this;
}
return this._createWith(raw => {
const fromPair = _pairToMap([a, b]);
return raw.merge(fromPair);
});
}
has(key) {
const parsed = (0, key_1.parseKey)(key);
if (parsed instanceof repr_1.ValueKey) {
return this._dict.has(parsed);
}
else {
return this._matchSectionKeys(parsed).size > 0;
}
}
get(key) {
const parsed = (0, key_1.parseKey)(key);
const v = this._dict.get(parsed);
if (v === undefined) {
throw new error_1.MetadataError(`Key ${key} not found!`, { key });
}
return v;
}
tryGet(key, fallback) {
const parsed = (0, key_1.parseKey)(key);
if (!(parsed instanceof repr_1.ValueKey)) {
throw new error_1.MetadataError("Unexpected section key!", { key });
}
return this._dict.get(parsed) ?? fallback;
}
_matchSectionKeys(key) {
return this._dict.filter((_, k) => k.parent?.equals(key));
}
pick(...keySpecs) {
return this._createWith(raw => {
const parsed = keySpecs.map(key_1.parseKey);
let keySet = (0, immutable_1.Set)();
for (const key of parsed) {
if (key instanceof repr_1.ValueKey) {
keySet = keySet.add(key);
}
else {
const sectionKeys = this._matchSectionKeys(key);
keySet = keySet.union(sectionKeys.keySeq());
}
}
return raw.filter((_, k) => keySet.has(k));
});
}
_prefixed(prefix) {
return this._dict
.filter((_, k) => k._prefix === prefix)
.mapKeys(k => k.suffix)
.toObject();
}
get labels() {
return this._prefixed("%");
}
get annotations() {
return this._prefixed("^");
}
get comments() {
return this._prefixed("#");
}
get values() {
return this._dict.toJS();
}
get core() {
return this._prefixed("");
}
remove(a, b) {
return this._createWith(raw => {
const parsed = (0, key_1.parseKey)(a);
if (parsed instanceof repr_1.ValueKey) {
return raw.delete(parsed);
}
else {
return raw.filter((_, k) => !k.parent?.equals(parsed));
}
});
}
expand() {
const labels = this.labels;
const annotations = this.annotations;
const core = this.core;
return {
...core,
labels,
annotations
};
}
toMutable() {
return new MutableMeta(this);
}
}
Meta_1.Meta = Meta;
function make(a, b) {
return new Meta(_pairToMap([a, b]));
}
Meta_1.make = make;
function _pairToObject(pair) {
const [key, value] = pair;
if (key instanceof Meta) {
return (0, immutable_1.Map)(key);
}
if (typeof key === "string") {
return {
[key]: value
};
}
return key;
}
function _pairToMap(pair) {
return (0, key_1.parseMetaInput)(_pairToObject(pair)).filter((v, k) => v != null);
}
function makeMutable(input = {}) {
return make(input).toMutable();
}
Meta_1.makeMutable = makeMutable;
function splat(...input) {
return input.map(make).reduce((acc, meta) => acc.add(meta), make());
}
Meta_1.splat = splat;
function is(value) {
return value instanceof Meta;
}
Meta_1.is = is;
class MutableMeta {
_meta;
constructor(_meta) {
this._meta = _meta;
}
add(a, b) {
this._meta = this._meta.add(a, b);
return this;
}
remove(a, b) {
this._meta = this._meta.remove(a, b);
return this;
}
overwrite(a, b) {
this._meta = this._meta.overwrite(a, b);
return this;
}
equals(other) {
return this._meta.equals(other);
}
get(key) {
return this._meta.get(key);
}
tryGet(key, fallback) {
return this._meta.tryGet(key, fallback);
}
has(key) {
return this._meta.has(key);
}
pick(...keySpecs) {
const newMeta = this._meta.pick(...keySpecs);
return new MutableMeta(newMeta);
}
toMutable() {
return new MutableMeta(this._meta);
}
section(key) {
return new MutableMeta(this._meta.section(key));
}
get labels() {
return this._meta.labels;
}
get annotations() {
return this._meta.annotations;
}
get comments() {
return this._meta.comments;
}
get core() {
return this._meta.core;
}
toImmutable() {
return this._meta;
}
}
Meta_1.MutableMeta = MutableMeta;
})(Meta || (exports.Meta = Meta = {}));
//# sourceMappingURL=meta.js.map