Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schema.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. /*eslint-disable max-len*/
  3. var common = require('./common');
  4. var YAMLException = require('./exception');
  5. var Type = require('./type');
  6. function compileList(schema, name, result) {
  7. var exclude = [];
  8. schema.include.forEach(function (includedSchema) {
  9. result = compileList(includedSchema, name, result);
  10. });
  11. schema[name].forEach(function (currentType) {
  12. result.forEach(function (previousType, previousIndex) {
  13. if (previousType.tag === currentType.tag && previousType.kind === currentType.kind) {
  14. exclude.push(previousIndex);
  15. }
  16. });
  17. result.push(currentType);
  18. });
  19. return result.filter(function (type, index) {
  20. return exclude.indexOf(index) === -1;
  21. });
  22. }
  23. function compileMap(/* lists... */) {
  24. var result = {
  25. scalar: {},
  26. sequence: {},
  27. mapping: {},
  28. fallback: {}
  29. }, index, length;
  30. function collectType(type) {
  31. result[type.kind][type.tag] = result['fallback'][type.tag] = type;
  32. }
  33. for (index = 0, length = arguments.length; index < length; index += 1) {
  34. arguments[index].forEach(collectType);
  35. }
  36. return result;
  37. }
  38. function Schema(definition) {
  39. this.include = definition.include || [];
  40. this.implicit = definition.implicit || [];
  41. this.explicit = definition.explicit || [];
  42. this.implicit.forEach(function (type) {
  43. if (type.loadKind && type.loadKind !== 'scalar') {
  44. throw new YAMLException('There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.');
  45. }
  46. });
  47. this.compiledImplicit = compileList(this, 'implicit', []);
  48. this.compiledExplicit = compileList(this, 'explicit', []);
  49. this.compiledTypeMap = compileMap(this.compiledImplicit, this.compiledExplicit);
  50. }
  51. Schema.DEFAULT = null;
  52. Schema.create = function createSchema() {
  53. var schemas, types;
  54. switch (arguments.length) {
  55. case 1:
  56. schemas = Schema.DEFAULT;
  57. types = arguments[0];
  58. break;
  59. case 2:
  60. schemas = arguments[0];
  61. types = arguments[1];
  62. break;
  63. default:
  64. throw new YAMLException('Wrong number of arguments for Schema.create function');
  65. }
  66. schemas = common.toArray(schemas);
  67. types = common.toArray(types);
  68. if (!schemas.every(function (schema) { return schema instanceof Schema; })) {
  69. throw new YAMLException('Specified list of super schemas (or a single Schema object) contains a non-Schema object.');
  70. }
  71. if (!types.every(function (type) { return type instanceof Type; })) {
  72. throw new YAMLException('Specified list of YAML types (or a single Type object) contains a non-Type object.');
  73. }
  74. return new Schema({
  75. include: schemas,
  76. explicit: types
  77. });
  78. };
  79. module.exports = Schema;