1
0
Fork 0
mirror of https://github.com/dawidd6/action-ansible-playbook.git synced 2024-11-23 09:17:19 +00:00
action-ansible-playbook/node_modules/yaml/browser/dist/compose/compose-collection.js

77 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2024-01-17 09:08:10 +00:00
import { isNode } from '../nodes/identity.js';
2022-10-21 14:44:44 +00:00
import { Scalar } from '../nodes/Scalar.js';
2024-01-17 09:08:10 +00:00
import { YAMLMap } from '../nodes/YAMLMap.js';
import { YAMLSeq } from '../nodes/YAMLSeq.js';
2022-10-21 14:44:44 +00:00
import { resolveBlockMap } from './resolve-block-map.js';
import { resolveBlockSeq } from './resolve-block-seq.js';
import { resolveFlowCollection } from './resolve-flow-collection.js';
2024-01-17 09:08:10 +00:00
function resolveCollection(CN, ctx, token, onError, tagName, tag) {
const coll = token.type === 'block-map'
? resolveBlockMap(CN, ctx, token, onError, tag)
: token.type === 'block-seq'
? resolveBlockSeq(CN, ctx, token, onError, tag)
: resolveFlowCollection(CN, ctx, token, onError, tag);
2022-10-21 14:44:44 +00:00
const Coll = coll.constructor;
2024-01-17 09:08:10 +00:00
// If we got a tagName matching the class, or the tag name is '!',
// then use the tagName from the node class used to create it.
2022-10-21 14:44:44 +00:00
if (tagName === '!' || tagName === Coll.tagName) {
coll.tag = Coll.tagName;
return coll;
}
2024-01-17 09:08:10 +00:00
if (tagName)
coll.tag = tagName;
return coll;
}
function composeCollection(CN, ctx, token, tagToken, onError) {
const tagName = !tagToken
? null
: ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
const expType = token.type === 'block-map'
? 'map'
: token.type === 'block-seq'
? 'seq'
: token.start.source === '{'
? 'map'
: 'seq';
// shortcut: check if it's a generic YAMLMap or YAMLSeq
// before jumping into the custom tag logic.
if (!tagToken ||
!tagName ||
tagName === '!' ||
(tagName === YAMLMap.tagName && expType === 'map') ||
(tagName === YAMLSeq.tagName && expType === 'seq') ||
!expType) {
return resolveCollection(CN, ctx, token, onError, tagName);
}
let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
2022-10-21 14:44:44 +00:00
if (!tag) {
const kt = ctx.schema.knownTags[tagName];
if (kt && kt.collection === expType) {
ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
tag = kt;
}
else {
2024-01-17 09:08:10 +00:00
if (kt?.collection) {
onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
}
else {
onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
}
return resolveCollection(CN, ctx, token, onError, tagName);
2022-10-21 14:44:44 +00:00
}
}
2024-01-17 09:08:10 +00:00
const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
2022-10-21 14:44:44 +00:00
const node = isNode(res)
? res
: new Scalar(res);
node.range = coll.range;
node.tag = tagName;
if (tag?.format)
node.format = tag.format;
return node;
}
export { composeCollection };