Açıklama Yok

compose-collection.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var Node = require('../nodes/Node.js');
  3. var Scalar = require('../nodes/Scalar.js');
  4. var resolveBlockMap = require('./resolve-block-map.js');
  5. var resolveBlockSeq = require('./resolve-block-seq.js');
  6. var resolveFlowCollection = require('./resolve-flow-collection.js');
  7. function composeCollection(CN, ctx, token, tagToken, onError) {
  8. let coll;
  9. switch (token.type) {
  10. case 'block-map': {
  11. coll = resolveBlockMap.resolveBlockMap(CN, ctx, token, onError);
  12. break;
  13. }
  14. case 'block-seq': {
  15. coll = resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError);
  16. break;
  17. }
  18. case 'flow-collection': {
  19. coll = resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError);
  20. break;
  21. }
  22. }
  23. if (!tagToken)
  24. return coll;
  25. const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
  26. if (!tagName)
  27. return coll;
  28. // Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841
  29. const Coll = coll.constructor;
  30. if (tagName === '!' || tagName === Coll.tagName) {
  31. coll.tag = Coll.tagName;
  32. return coll;
  33. }
  34. const expType = Node.isMap(coll) ? 'map' : 'seq';
  35. let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName);
  36. if (!tag) {
  37. const kt = ctx.schema.knownTags[tagName];
  38. if (kt && kt.collection === expType) {
  39. ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
  40. tag = kt;
  41. }
  42. else {
  43. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
  44. coll.tag = tagName;
  45. return coll;
  46. }
  47. }
  48. const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options);
  49. const node = Node.isNode(res)
  50. ? res
  51. : new Scalar.Scalar(res);
  52. node.range = coll.range;
  53. node.tag = tagName;
  54. if (tag?.format)
  55. node.format = tag.format;
  56. return node;
  57. }
  58. exports.composeCollection = composeCollection;