UNPKG

@k8ts/metadata

Version:

K8ts tools for working with k8ts metadata.

71 lines (62 loc) 2.35 kB
import { anyCharOf, anyStringOf, digit, letter, lower, string } from "parjs" import { many1, map, maybe, or, qthen, stringify, then, thenq } from "parjs/combinators" import { K8tsMetadataError } from "../../error" import { Metadata_Key_Domain } from "./domain-prefix" import { Metadata_Key_Value } from "./metadata-key" const cPrefix = anyCharOf("%^#") const cSection = string("/") export const normalChar = letter().pipe(or(digit())).expects("alphanumeric") const cDot = anyCharOf(".") const cExtra = anyCharOf(`-_`).pipe(or(cDot)).expects("'-', '_', or '.'") const cNameChar = lower().pipe(or(digit())).pipe(or("-")) const cInterior = normalChar.pipe(or(cExtra)).expects("alphanumeric, '-', '_', or '.'") export const pNameValue = cNameChar.pipe(many1(), stringify()) const pSpecialKey = anyStringOf("namespace", "name").pipe( map(key => { throw new K8tsMetadataError( `Tried to set resource UID key '${key}' via metadata, which isn't allowed in k8ts. Modify the resource directly instead.` ) }) ) const pCleanKey = cInterior.pipe(many1(), stringify()) const pSectionKey = pCleanKey.pipe( thenq(cSection), map(x => new Metadata_Key_Domain(x)) ) const pInnerKey = cPrefix.pipe( then(pCleanKey), map(([prefix, name]) => { return new Metadata_Key_Value(prefix, "", name) }) ) const pKey = cPrefix.pipe( then(pCleanKey, cSection.pipe(qthen(pCleanKey), stringify(), maybe())), map(([prefix, nameOrSection, name]) => { if (name) { return new Metadata_Key_Value(prefix, nameOrSection, name) } return new Metadata_Key_Value(prefix, "", nameOrSection) }), or(pSpecialKey) ) const pOuterKey = pKey.pipe(or(pSectionKey)) export function parseSectionKey(key: string) { return pSectionKey.parse(key).value } export function checkValue(value: string) { return value === "" ? "" : pCleanKey.parse(value).value } export function parseInnerKey(key: string) { return pInnerKey.parse(key).value } export function parseOuterKey(key: string) { const result = pOuterKey.parse(key) if (result.kind === "OK") { return result.value } throw new K8tsMetadataError(`Failed to parse key ${key}: ${result.toString()}`, { key, error: result.toString() }) } export { parseOuterKey as parseKey }