@k8ts/metadata
Version:
K8ts tools for working with k8ts metadata.
101 lines • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SectionKey = exports.ValueKey = void 0;
exports.checkMetaString = checkMetaString;
const immutable_1 = require("immutable");
const error_1 = require("../error");
const parse_key_1 = require("./parse-key");
class KeyType {
equals(other) {
return this.constructor === other.constructor && this.toString() === other.toString();
}
hashCode() {
return (0, immutable_1.hash)(this.toString());
}
toString() {
return this.str;
}
}
function checkMetaString(thing, input, length) {
if (!parse_key_1.normalChar.parse(input[0]).isOk) {
throw new error_1.MetadataError(`${thing} must start with an alphanumeric character.`);
}
if (!parse_key_1.normalChar.parse(input[input.length - 1]).isOk) {
throw new error_1.MetadataError(`${thing} must end with an alphanumeric character.`);
}
if (thing.length > length) {
throw new error_1.MetadataError(`${thing} must be no more than ${length} characters.`);
}
}
class ValueKey extends KeyType {
_prefix;
_section;
_name;
type = "full";
constructor(_prefix, _section, _name) {
super();
this._prefix = _prefix;
this._section = _section;
this._name = _name;
if (_section) {
checkMetaString(`DNS prefix for ${this.str}`, _section, 253);
}
checkMetaString(`Metadata name of ${this.str}`, _name, 63);
}
get metaType() {
switch (this._prefix) {
case "":
return "core";
case "%":
return "label";
case "#":
return "comment";
case "^":
return "annotation";
default:
throw new Error(`Unknown prefix ${this._prefix}`);
}
}
get suffix() {
const parts = [];
if (this._section) {
parts.push(this._section);
if (!this._section.endsWith("/")) {
parts.push("/");
}
}
parts.push(this._name);
return parts.join("");
}
get str() {
return [this._prefix, this.suffix].join("");
}
get parent() {
if (this._section == null) {
return null;
}
return new SectionKey(this._section);
}
section(section) {
section = section instanceof SectionKey ? section.str : section;
if (this._section) {
throw new Error("Already has a section");
}
return new ValueKey(this._prefix, section, this._name);
}
}
exports.ValueKey = ValueKey;
class SectionKey extends KeyType {
_section;
type = "heading";
constructor(_section) {
super();
this._section = _section;
checkMetaString(`Section name ${this._section}`, _section, 253);
}
get str() {
return [this._section].join("");
}
}
exports.SectionKey = SectionKey;
//# sourceMappingURL=repr.js.map