[d24f17c] | 1 | import { clone } from 'ramda';
|
---|
| 2 | import YamlTagError from "../../errors/YamlTagError.mjs";
|
---|
| 3 | import { YamlNodeKind } from "../../nodes/YamlTag.mjs";
|
---|
| 4 | import GenericMappingTag from "./GenericMapping.mjs";
|
---|
| 5 | import GenericSequenceTag from "./GenericSequence.mjs";
|
---|
| 6 | import GenericStringTag from "./GenericString.mjs";
|
---|
| 7 | import ScalarTag from "../ScalarTag.mjs";
|
---|
| 8 | class FailsafeSchema {
|
---|
| 9 | tags;
|
---|
| 10 | tagDirectives;
|
---|
| 11 | constructor() {
|
---|
| 12 | this.tags = [];
|
---|
| 13 | this.tagDirectives = [];
|
---|
| 14 | this.registerTag(new GenericMappingTag());
|
---|
| 15 | this.registerTag(new GenericSequenceTag());
|
---|
| 16 | this.registerTag(new GenericStringTag());
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | // eslint-disable-next-line class-methods-use-this
|
---|
| 20 | toSpecificTagName(node) {
|
---|
| 21 | let specificTagName = node.tag.explicitName;
|
---|
| 22 | if (node.tag.explicitName === '!') {
|
---|
| 23 | // non-specific tag; we assume tag by kind
|
---|
| 24 | if (node.tag.kind === YamlNodeKind.Scalar) {
|
---|
| 25 | specificTagName = GenericStringTag.uri;
|
---|
| 26 | } else if (node.tag.kind === YamlNodeKind.Sequence) {
|
---|
| 27 | specificTagName = GenericSequenceTag.uri;
|
---|
| 28 | } else if (node.tag.kind === YamlNodeKind.Mapping) {
|
---|
| 29 | specificTagName = GenericMappingTag.uri;
|
---|
| 30 | }
|
---|
| 31 | } else if (node.tag.explicitName.startsWith('!<')) {
|
---|
| 32 | // verbatim form
|
---|
| 33 | specificTagName = node.tag.explicitName.replace(/^!</, '').replace(/>$/, '');
|
---|
| 34 | } else if (node.tag.explicitName.startsWith('!!')) {
|
---|
| 35 | // shorthand notation
|
---|
| 36 | specificTagName = `tag:yaml.org,2002:${node.tag.explicitName.replace(/^!!/, '')}`;
|
---|
| 37 | }
|
---|
| 38 | return specificTagName;
|
---|
| 39 | }
|
---|
| 40 | registerTagDirective(tagDirective) {
|
---|
| 41 | this.tagDirectives.push({
|
---|
| 42 | // @ts-ignore
|
---|
| 43 | handle: tagDirective.parameters.handle,
|
---|
| 44 | // @ts-ignore
|
---|
| 45 | prefix: tagDirective.parameters.prefix
|
---|
| 46 | });
|
---|
| 47 | }
|
---|
| 48 | registerTag(tag, beginning = false) {
|
---|
| 49 | if (beginning) {
|
---|
| 50 | this.tags.unshift(tag);
|
---|
| 51 | } else {
|
---|
| 52 | this.tags.push(tag);
|
---|
| 53 | }
|
---|
| 54 | return this;
|
---|
| 55 | }
|
---|
| 56 | overrideTag(tag) {
|
---|
| 57 | this.tags = this.tags.filter(itag => itag.tag === tag.tag);
|
---|
| 58 | this.tags.push(tag);
|
---|
| 59 | return this;
|
---|
| 60 | }
|
---|
| 61 | resolve(node) {
|
---|
| 62 | const specificTagName = this.toSpecificTagName(node);
|
---|
| 63 |
|
---|
| 64 | // leave this node unresolved
|
---|
| 65 | if (specificTagName === '?') {
|
---|
| 66 | return node;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // turn scalar nodes into canonical format before resolving
|
---|
| 70 | let canonicalNode = node;
|
---|
| 71 | if (ScalarTag.test(node)) {
|
---|
| 72 | canonicalNode = ScalarTag.canonicalFormat(node);
|
---|
| 73 | }
|
---|
| 74 | const tag = this.tags.find(itag => (itag === null || itag === void 0 ? void 0 : itag.tag) === specificTagName);
|
---|
| 75 |
|
---|
| 76 | // mechanism for resolving node (tag implementation) not found
|
---|
| 77 | if (typeof tag === 'undefined') {
|
---|
| 78 | throw new YamlTagError(`Tag "${specificTagName}" was not recognized.`, {
|
---|
| 79 | specificTagName,
|
---|
| 80 | explicitTagName: node.tag.explicitName,
|
---|
| 81 | tagKind: node.tag.kind,
|
---|
| 82 | tagPosition: clone(node.tag.position),
|
---|
| 83 | node: node.clone()
|
---|
| 84 | });
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | // node content is not compatible with resolving mechanism (tag implementation)
|
---|
| 88 | if (!tag.test(canonicalNode)) {
|
---|
| 89 | throw new YamlTagError(`Node couldn't be resolved against the tag "${specificTagName}"`, {
|
---|
| 90 | specificTagName,
|
---|
| 91 | explicitTagName: node.tag.explicitName,
|
---|
| 92 | tagKind: node.tag.kind,
|
---|
| 93 | tagPosition: clone(node.tag.position),
|
---|
| 94 | nodeCanonicalContent: canonicalNode.content,
|
---|
| 95 | node: node.clone()
|
---|
| 96 | });
|
---|
| 97 | }
|
---|
| 98 | return tag.resolve(canonicalNode);
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | export default FailsafeSchema; |
---|