| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var PlainValue = require('./PlainValue-516d5bc2.js');
|
|---|
| 4 | var resolveSeq = require('./resolveSeq-95613e94.js');
|
|---|
| 5 | var warnings = require('./warnings-793925ce.js');
|
|---|
| 6 |
|
|---|
| 7 | function createMap(schema, obj, ctx) {
|
|---|
| 8 | const map = new resolveSeq.YAMLMap(schema);
|
|---|
| 9 | if (obj instanceof Map) {
|
|---|
| 10 | for (const [key, value] of obj) map.items.push(schema.createPair(key, value, ctx));
|
|---|
| 11 | } else if (obj && typeof obj === 'object') {
|
|---|
| 12 | for (const key of Object.keys(obj)) map.items.push(schema.createPair(key, obj[key], ctx));
|
|---|
| 13 | }
|
|---|
| 14 | if (typeof schema.sortMapEntries === 'function') {
|
|---|
| 15 | map.items.sort(schema.sortMapEntries);
|
|---|
| 16 | }
|
|---|
| 17 | return map;
|
|---|
| 18 | }
|
|---|
| 19 | const map = {
|
|---|
| 20 | createNode: createMap,
|
|---|
| 21 | default: true,
|
|---|
| 22 | nodeClass: resolveSeq.YAMLMap,
|
|---|
| 23 | tag: 'tag:yaml.org,2002:map',
|
|---|
| 24 | resolve: resolveSeq.resolveMap
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | function createSeq(schema, obj, ctx) {
|
|---|
| 28 | const seq = new resolveSeq.YAMLSeq(schema);
|
|---|
| 29 | if (obj && obj[Symbol.iterator]) {
|
|---|
| 30 | for (const it of obj) {
|
|---|
| 31 | const v = schema.createNode(it, ctx.wrapScalars, null, ctx);
|
|---|
| 32 | seq.items.push(v);
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | return seq;
|
|---|
| 36 | }
|
|---|
| 37 | const seq = {
|
|---|
| 38 | createNode: createSeq,
|
|---|
| 39 | default: true,
|
|---|
| 40 | nodeClass: resolveSeq.YAMLSeq,
|
|---|
| 41 | tag: 'tag:yaml.org,2002:seq',
|
|---|
| 42 | resolve: resolveSeq.resolveSeq
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | const string = {
|
|---|
| 46 | identify: value => typeof value === 'string',
|
|---|
| 47 | default: true,
|
|---|
| 48 | tag: 'tag:yaml.org,2002:str',
|
|---|
| 49 | resolve: resolveSeq.resolveString,
|
|---|
| 50 | stringify(item, ctx, onComment, onChompKeep) {
|
|---|
| 51 | ctx = Object.assign({
|
|---|
| 52 | actualString: true
|
|---|
| 53 | }, ctx);
|
|---|
| 54 | return resolveSeq.stringifyString(item, ctx, onComment, onChompKeep);
|
|---|
| 55 | },
|
|---|
| 56 | options: resolveSeq.strOptions
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | const failsafe = [map, seq, string];
|
|---|
| 60 |
|
|---|
| 61 | /* global BigInt */
|
|---|
| 62 | const intIdentify$2 = value => typeof value === 'bigint' || Number.isInteger(value);
|
|---|
| 63 | const intResolve$1 = (src, part, radix) => resolveSeq.intOptions.asBigInt ? BigInt(src) : parseInt(part, radix);
|
|---|
| 64 | function intStringify$1(node, radix, prefix) {
|
|---|
| 65 | const {
|
|---|
| 66 | value
|
|---|
| 67 | } = node;
|
|---|
| 68 | if (intIdentify$2(value) && value >= 0) return prefix + value.toString(radix);
|
|---|
| 69 | return resolveSeq.stringifyNumber(node);
|
|---|
| 70 | }
|
|---|
| 71 | const nullObj = {
|
|---|
| 72 | identify: value => value == null,
|
|---|
| 73 | createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
|
|---|
| 74 | default: true,
|
|---|
| 75 | tag: 'tag:yaml.org,2002:null',
|
|---|
| 76 | test: /^(?:~|[Nn]ull|NULL)?$/,
|
|---|
| 77 | resolve: () => null,
|
|---|
| 78 | options: resolveSeq.nullOptions,
|
|---|
| 79 | stringify: () => resolveSeq.nullOptions.nullStr
|
|---|
| 80 | };
|
|---|
| 81 | const boolObj = {
|
|---|
| 82 | identify: value => typeof value === 'boolean',
|
|---|
| 83 | default: true,
|
|---|
| 84 | tag: 'tag:yaml.org,2002:bool',
|
|---|
| 85 | test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
|
|---|
| 86 | resolve: str => str[0] === 't' || str[0] === 'T',
|
|---|
| 87 | options: resolveSeq.boolOptions,
|
|---|
| 88 | stringify: ({
|
|---|
| 89 | value
|
|---|
| 90 | }) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr
|
|---|
| 91 | };
|
|---|
| 92 | const octObj = {
|
|---|
| 93 | identify: value => intIdentify$2(value) && value >= 0,
|
|---|
| 94 | default: true,
|
|---|
| 95 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 96 | format: 'OCT',
|
|---|
| 97 | test: /^0o([0-7]+)$/,
|
|---|
| 98 | resolve: (str, oct) => intResolve$1(str, oct, 8),
|
|---|
| 99 | options: resolveSeq.intOptions,
|
|---|
| 100 | stringify: node => intStringify$1(node, 8, '0o')
|
|---|
| 101 | };
|
|---|
| 102 | const intObj = {
|
|---|
| 103 | identify: intIdentify$2,
|
|---|
| 104 | default: true,
|
|---|
| 105 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 106 | test: /^[-+]?[0-9]+$/,
|
|---|
| 107 | resolve: str => intResolve$1(str, str, 10),
|
|---|
| 108 | options: resolveSeq.intOptions,
|
|---|
| 109 | stringify: resolveSeq.stringifyNumber
|
|---|
| 110 | };
|
|---|
| 111 | const hexObj = {
|
|---|
| 112 | identify: value => intIdentify$2(value) && value >= 0,
|
|---|
| 113 | default: true,
|
|---|
| 114 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 115 | format: 'HEX',
|
|---|
| 116 | test: /^0x([0-9a-fA-F]+)$/,
|
|---|
| 117 | resolve: (str, hex) => intResolve$1(str, hex, 16),
|
|---|
| 118 | options: resolveSeq.intOptions,
|
|---|
| 119 | stringify: node => intStringify$1(node, 16, '0x')
|
|---|
| 120 | };
|
|---|
| 121 | const nanObj = {
|
|---|
| 122 | identify: value => typeof value === 'number',
|
|---|
| 123 | default: true,
|
|---|
| 124 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 125 | test: /^(?:[-+]?\.inf|(\.nan))$/i,
|
|---|
| 126 | resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
|
|---|
| 127 | stringify: resolveSeq.stringifyNumber
|
|---|
| 128 | };
|
|---|
| 129 | const expObj = {
|
|---|
| 130 | identify: value => typeof value === 'number',
|
|---|
| 131 | default: true,
|
|---|
| 132 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 133 | format: 'EXP',
|
|---|
| 134 | test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
|
|---|
| 135 | resolve: str => parseFloat(str),
|
|---|
| 136 | stringify: ({
|
|---|
| 137 | value
|
|---|
| 138 | }) => Number(value).toExponential()
|
|---|
| 139 | };
|
|---|
| 140 | const floatObj = {
|
|---|
| 141 | identify: value => typeof value === 'number',
|
|---|
| 142 | default: true,
|
|---|
| 143 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 144 | test: /^[-+]?(?:\.([0-9]+)|[0-9]+\.([0-9]*))$/,
|
|---|
| 145 | resolve(str, frac1, frac2) {
|
|---|
| 146 | const frac = frac1 || frac2;
|
|---|
| 147 | const node = new resolveSeq.Scalar(parseFloat(str));
|
|---|
| 148 | if (frac && frac[frac.length - 1] === '0') node.minFractionDigits = frac.length;
|
|---|
| 149 | return node;
|
|---|
| 150 | },
|
|---|
| 151 | stringify: resolveSeq.stringifyNumber
|
|---|
| 152 | };
|
|---|
| 153 | const core = failsafe.concat([nullObj, boolObj, octObj, intObj, hexObj, nanObj, expObj, floatObj]);
|
|---|
| 154 |
|
|---|
| 155 | /* global BigInt */
|
|---|
| 156 | const intIdentify$1 = value => typeof value === 'bigint' || Number.isInteger(value);
|
|---|
| 157 | const stringifyJSON = ({
|
|---|
| 158 | value
|
|---|
| 159 | }) => JSON.stringify(value);
|
|---|
| 160 | const json = [map, seq, {
|
|---|
| 161 | identify: value => typeof value === 'string',
|
|---|
| 162 | default: true,
|
|---|
| 163 | tag: 'tag:yaml.org,2002:str',
|
|---|
| 164 | resolve: resolveSeq.resolveString,
|
|---|
| 165 | stringify: stringifyJSON
|
|---|
| 166 | }, {
|
|---|
| 167 | identify: value => value == null,
|
|---|
| 168 | createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
|
|---|
| 169 | default: true,
|
|---|
| 170 | tag: 'tag:yaml.org,2002:null',
|
|---|
| 171 | test: /^null$/,
|
|---|
| 172 | resolve: () => null,
|
|---|
| 173 | stringify: stringifyJSON
|
|---|
| 174 | }, {
|
|---|
| 175 | identify: value => typeof value === 'boolean',
|
|---|
| 176 | default: true,
|
|---|
| 177 | tag: 'tag:yaml.org,2002:bool',
|
|---|
| 178 | test: /^true|false$/,
|
|---|
| 179 | resolve: str => str === 'true',
|
|---|
| 180 | stringify: stringifyJSON
|
|---|
| 181 | }, {
|
|---|
| 182 | identify: intIdentify$1,
|
|---|
| 183 | default: true,
|
|---|
| 184 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 185 | test: /^-?(?:0|[1-9][0-9]*)$/,
|
|---|
| 186 | resolve: str => resolveSeq.intOptions.asBigInt ? BigInt(str) : parseInt(str, 10),
|
|---|
| 187 | stringify: ({
|
|---|
| 188 | value
|
|---|
| 189 | }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
|
|---|
| 190 | }, {
|
|---|
| 191 | identify: value => typeof value === 'number',
|
|---|
| 192 | default: true,
|
|---|
| 193 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 194 | test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
|
|---|
| 195 | resolve: str => parseFloat(str),
|
|---|
| 196 | stringify: stringifyJSON
|
|---|
| 197 | }];
|
|---|
| 198 | json.scalarFallback = str => {
|
|---|
| 199 | throw new SyntaxError(`Unresolved plain scalar ${JSON.stringify(str)}`);
|
|---|
| 200 | };
|
|---|
| 201 |
|
|---|
| 202 | /* global BigInt */
|
|---|
| 203 | const boolStringify = ({
|
|---|
| 204 | value
|
|---|
| 205 | }) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr;
|
|---|
| 206 | const intIdentify = value => typeof value === 'bigint' || Number.isInteger(value);
|
|---|
| 207 | function intResolve(sign, src, radix) {
|
|---|
| 208 | let str = src.replace(/_/g, '');
|
|---|
| 209 | if (resolveSeq.intOptions.asBigInt) {
|
|---|
| 210 | switch (radix) {
|
|---|
| 211 | case 2:
|
|---|
| 212 | str = `0b${str}`;
|
|---|
| 213 | break;
|
|---|
| 214 | case 8:
|
|---|
| 215 | str = `0o${str}`;
|
|---|
| 216 | break;
|
|---|
| 217 | case 16:
|
|---|
| 218 | str = `0x${str}`;
|
|---|
| 219 | break;
|
|---|
| 220 | }
|
|---|
| 221 | const n = BigInt(str);
|
|---|
| 222 | return sign === '-' ? BigInt(-1) * n : n;
|
|---|
| 223 | }
|
|---|
| 224 | const n = parseInt(str, radix);
|
|---|
| 225 | return sign === '-' ? -1 * n : n;
|
|---|
| 226 | }
|
|---|
| 227 | function intStringify(node, radix, prefix) {
|
|---|
| 228 | const {
|
|---|
| 229 | value
|
|---|
| 230 | } = node;
|
|---|
| 231 | if (intIdentify(value)) {
|
|---|
| 232 | const str = value.toString(radix);
|
|---|
| 233 | return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
|
|---|
| 234 | }
|
|---|
| 235 | return resolveSeq.stringifyNumber(node);
|
|---|
| 236 | }
|
|---|
| 237 | const yaml11 = failsafe.concat([{
|
|---|
| 238 | identify: value => value == null,
|
|---|
| 239 | createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
|
|---|
| 240 | default: true,
|
|---|
| 241 | tag: 'tag:yaml.org,2002:null',
|
|---|
| 242 | test: /^(?:~|[Nn]ull|NULL)?$/,
|
|---|
| 243 | resolve: () => null,
|
|---|
| 244 | options: resolveSeq.nullOptions,
|
|---|
| 245 | stringify: () => resolveSeq.nullOptions.nullStr
|
|---|
| 246 | }, {
|
|---|
| 247 | identify: value => typeof value === 'boolean',
|
|---|
| 248 | default: true,
|
|---|
| 249 | tag: 'tag:yaml.org,2002:bool',
|
|---|
| 250 | test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
|
|---|
| 251 | resolve: () => true,
|
|---|
| 252 | options: resolveSeq.boolOptions,
|
|---|
| 253 | stringify: boolStringify
|
|---|
| 254 | }, {
|
|---|
| 255 | identify: value => typeof value === 'boolean',
|
|---|
| 256 | default: true,
|
|---|
| 257 | tag: 'tag:yaml.org,2002:bool',
|
|---|
| 258 | test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
|
|---|
| 259 | resolve: () => false,
|
|---|
| 260 | options: resolveSeq.boolOptions,
|
|---|
| 261 | stringify: boolStringify
|
|---|
| 262 | }, {
|
|---|
| 263 | identify: intIdentify,
|
|---|
| 264 | default: true,
|
|---|
| 265 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 266 | format: 'BIN',
|
|---|
| 267 | test: /^([-+]?)0b([0-1_]+)$/,
|
|---|
| 268 | resolve: (str, sign, bin) => intResolve(sign, bin, 2),
|
|---|
| 269 | stringify: node => intStringify(node, 2, '0b')
|
|---|
| 270 | }, {
|
|---|
| 271 | identify: intIdentify,
|
|---|
| 272 | default: true,
|
|---|
| 273 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 274 | format: 'OCT',
|
|---|
| 275 | test: /^([-+]?)0([0-7_]+)$/,
|
|---|
| 276 | resolve: (str, sign, oct) => intResolve(sign, oct, 8),
|
|---|
| 277 | stringify: node => intStringify(node, 8, '0')
|
|---|
| 278 | }, {
|
|---|
| 279 | identify: intIdentify,
|
|---|
| 280 | default: true,
|
|---|
| 281 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 282 | test: /^([-+]?)([0-9][0-9_]*)$/,
|
|---|
| 283 | resolve: (str, sign, abs) => intResolve(sign, abs, 10),
|
|---|
| 284 | stringify: resolveSeq.stringifyNumber
|
|---|
| 285 | }, {
|
|---|
| 286 | identify: intIdentify,
|
|---|
| 287 | default: true,
|
|---|
| 288 | tag: 'tag:yaml.org,2002:int',
|
|---|
| 289 | format: 'HEX',
|
|---|
| 290 | test: /^([-+]?)0x([0-9a-fA-F_]+)$/,
|
|---|
| 291 | resolve: (str, sign, hex) => intResolve(sign, hex, 16),
|
|---|
| 292 | stringify: node => intStringify(node, 16, '0x')
|
|---|
| 293 | }, {
|
|---|
| 294 | identify: value => typeof value === 'number',
|
|---|
| 295 | default: true,
|
|---|
| 296 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 297 | test: /^(?:[-+]?\.inf|(\.nan))$/i,
|
|---|
| 298 | resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
|
|---|
| 299 | stringify: resolveSeq.stringifyNumber
|
|---|
| 300 | }, {
|
|---|
| 301 | identify: value => typeof value === 'number',
|
|---|
| 302 | default: true,
|
|---|
| 303 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 304 | format: 'EXP',
|
|---|
| 305 | test: /^[-+]?([0-9][0-9_]*)?(\.[0-9_]*)?[eE][-+]?[0-9]+$/,
|
|---|
| 306 | resolve: str => parseFloat(str.replace(/_/g, '')),
|
|---|
| 307 | stringify: ({
|
|---|
| 308 | value
|
|---|
| 309 | }) => Number(value).toExponential()
|
|---|
| 310 | }, {
|
|---|
| 311 | identify: value => typeof value === 'number',
|
|---|
| 312 | default: true,
|
|---|
| 313 | tag: 'tag:yaml.org,2002:float',
|
|---|
| 314 | test: /^[-+]?(?:[0-9][0-9_]*)?\.([0-9_]*)$/,
|
|---|
| 315 | resolve(str, frac) {
|
|---|
| 316 | const node = new resolveSeq.Scalar(parseFloat(str.replace(/_/g, '')));
|
|---|
| 317 | if (frac) {
|
|---|
| 318 | const f = frac.replace(/_/g, '');
|
|---|
| 319 | if (f[f.length - 1] === '0') node.minFractionDigits = f.length;
|
|---|
| 320 | }
|
|---|
| 321 | return node;
|
|---|
| 322 | },
|
|---|
| 323 | stringify: resolveSeq.stringifyNumber
|
|---|
| 324 | }], warnings.binary, warnings.omap, warnings.pairs, warnings.set, warnings.intTime, warnings.floatTime, warnings.timestamp);
|
|---|
| 325 |
|
|---|
| 326 | const schemas = {
|
|---|
| 327 | core,
|
|---|
| 328 | failsafe,
|
|---|
| 329 | json,
|
|---|
| 330 | yaml11
|
|---|
| 331 | };
|
|---|
| 332 | const tags = {
|
|---|
| 333 | binary: warnings.binary,
|
|---|
| 334 | bool: boolObj,
|
|---|
| 335 | float: floatObj,
|
|---|
| 336 | floatExp: expObj,
|
|---|
| 337 | floatNaN: nanObj,
|
|---|
| 338 | floatTime: warnings.floatTime,
|
|---|
| 339 | int: intObj,
|
|---|
| 340 | intHex: hexObj,
|
|---|
| 341 | intOct: octObj,
|
|---|
| 342 | intTime: warnings.intTime,
|
|---|
| 343 | map,
|
|---|
| 344 | null: nullObj,
|
|---|
| 345 | omap: warnings.omap,
|
|---|
| 346 | pairs: warnings.pairs,
|
|---|
| 347 | seq,
|
|---|
| 348 | set: warnings.set,
|
|---|
| 349 | timestamp: warnings.timestamp
|
|---|
| 350 | };
|
|---|
| 351 |
|
|---|
| 352 | function findTagObject(value, tagName, tags) {
|
|---|
| 353 | if (tagName) {
|
|---|
| 354 | const match = tags.filter(t => t.tag === tagName);
|
|---|
| 355 | const tagObj = match.find(t => !t.format) || match[0];
|
|---|
| 356 | if (!tagObj) throw new Error(`Tag ${tagName} not found`);
|
|---|
| 357 | return tagObj;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | // TODO: deprecate/remove class check
|
|---|
| 361 | return tags.find(t => (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format);
|
|---|
| 362 | }
|
|---|
| 363 | function createNode(value, tagName, ctx) {
|
|---|
| 364 | if (value instanceof resolveSeq.Node) return value;
|
|---|
| 365 | const {
|
|---|
| 366 | defaultPrefix,
|
|---|
| 367 | onTagObj,
|
|---|
| 368 | prevObjects,
|
|---|
| 369 | schema,
|
|---|
| 370 | wrapScalars
|
|---|
| 371 | } = ctx;
|
|---|
| 372 | if (tagName && tagName.startsWith('!!')) tagName = defaultPrefix + tagName.slice(2);
|
|---|
| 373 | let tagObj = findTagObject(value, tagName, schema.tags);
|
|---|
| 374 | if (!tagObj) {
|
|---|
| 375 | if (typeof value.toJSON === 'function') value = value.toJSON();
|
|---|
| 376 | if (!value || typeof value !== 'object') return wrapScalars ? new resolveSeq.Scalar(value) : value;
|
|---|
| 377 | tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map;
|
|---|
| 378 | }
|
|---|
| 379 | if (onTagObj) {
|
|---|
| 380 | onTagObj(tagObj);
|
|---|
| 381 | delete ctx.onTagObj;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | // Detect duplicate references to the same object & use Alias nodes for all
|
|---|
| 385 | // after first. The `obj` wrapper allows for circular references to resolve.
|
|---|
| 386 | const obj = {
|
|---|
| 387 | value: undefined,
|
|---|
| 388 | node: undefined
|
|---|
| 389 | };
|
|---|
| 390 | if (value && typeof value === 'object' && prevObjects) {
|
|---|
| 391 | const prev = prevObjects.get(value);
|
|---|
| 392 | if (prev) {
|
|---|
| 393 | const alias = new resolveSeq.Alias(prev); // leaves source dirty; must be cleaned by caller
|
|---|
| 394 | ctx.aliasNodes.push(alias); // defined along with prevObjects
|
|---|
| 395 | return alias;
|
|---|
| 396 | }
|
|---|
| 397 | obj.value = value;
|
|---|
| 398 | prevObjects.set(value, obj);
|
|---|
| 399 | }
|
|---|
| 400 | obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new resolveSeq.Scalar(value) : value;
|
|---|
| 401 | if (tagName && obj.node instanceof resolveSeq.Node) obj.node.tag = tagName;
|
|---|
| 402 | return obj.node;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | function getSchemaTags(schemas, knownTags, customTags, schemaId) {
|
|---|
| 406 | let tags = schemas[schemaId.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
|
|---|
| 407 | if (!tags) {
|
|---|
| 408 | const keys = Object.keys(schemas).map(key => JSON.stringify(key)).join(', ');
|
|---|
| 409 | throw new Error(`Unknown schema "${schemaId}"; use one of ${keys}`);
|
|---|
| 410 | }
|
|---|
| 411 | if (Array.isArray(customTags)) {
|
|---|
| 412 | for (const tag of customTags) tags = tags.concat(tag);
|
|---|
| 413 | } else if (typeof customTags === 'function') {
|
|---|
| 414 | tags = customTags(tags.slice());
|
|---|
| 415 | }
|
|---|
| 416 | for (let i = 0; i < tags.length; ++i) {
|
|---|
| 417 | const tag = tags[i];
|
|---|
| 418 | if (typeof tag === 'string') {
|
|---|
| 419 | const tagObj = knownTags[tag];
|
|---|
| 420 | if (!tagObj) {
|
|---|
| 421 | const keys = Object.keys(knownTags).map(key => JSON.stringify(key)).join(', ');
|
|---|
| 422 | throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
|
|---|
| 423 | }
|
|---|
| 424 | tags[i] = tagObj;
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 | return tags;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
|
|---|
| 431 | class Schema {
|
|---|
| 432 | // TODO: remove in v2
|
|---|
| 433 |
|
|---|
| 434 | constructor({
|
|---|
| 435 | customTags,
|
|---|
| 436 | merge,
|
|---|
| 437 | schema,
|
|---|
| 438 | sortMapEntries,
|
|---|
| 439 | tags: deprecatedCustomTags
|
|---|
| 440 | }) {
|
|---|
| 441 | this.merge = !!merge;
|
|---|
| 442 | this.name = schema;
|
|---|
| 443 | this.sortMapEntries = sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
|
|---|
| 444 | if (!customTags && deprecatedCustomTags) warnings.warnOptionDeprecation('tags', 'customTags');
|
|---|
| 445 | this.tags = getSchemaTags(schemas, tags, customTags || deprecatedCustomTags, schema);
|
|---|
| 446 | }
|
|---|
| 447 | createNode(value, wrapScalars, tagName, ctx) {
|
|---|
| 448 | const baseCtx = {
|
|---|
| 449 | defaultPrefix: Schema.defaultPrefix,
|
|---|
| 450 | schema: this,
|
|---|
| 451 | wrapScalars
|
|---|
| 452 | };
|
|---|
| 453 | const createCtx = ctx ? Object.assign(ctx, baseCtx) : baseCtx;
|
|---|
| 454 | return createNode(value, tagName, createCtx);
|
|---|
| 455 | }
|
|---|
| 456 | createPair(key, value, ctx) {
|
|---|
| 457 | if (!ctx) ctx = {
|
|---|
| 458 | wrapScalars: true
|
|---|
| 459 | };
|
|---|
| 460 | const k = this.createNode(key, ctx.wrapScalars, null, ctx);
|
|---|
| 461 | const v = this.createNode(value, ctx.wrapScalars, null, ctx);
|
|---|
| 462 | return new resolveSeq.Pair(k, v);
|
|---|
| 463 | }
|
|---|
| 464 | }
|
|---|
| 465 | PlainValue._defineProperty(Schema, "defaultPrefix", PlainValue.defaultTagPrefix);
|
|---|
| 466 | // TODO: remove in v2
|
|---|
| 467 | PlainValue._defineProperty(Schema, "defaultTags", PlainValue.defaultTags);
|
|---|
| 468 |
|
|---|
| 469 | exports.Schema = Schema;
|
|---|