All files / lib/model/oasis/annotations Collection.js

95.65% Statements 22/23
100% Branches 12/12
90% Functions 9/10
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79  1x     10x 9x   10x 1x         9x       6x 6x       2x 2x 1x     1x       6x             10x 9x                                           10x 8x   8x 1x           8x           1x  
"use strict";
const conditionalType = "If";
 
function getItemType(metadata) {
  let names = metadata
    ? Object.getOwnPropertyNames(metadata).filter((n) => n !== conditionalType)
    : [];
  if (names.length > 1) {
    throw new Error(
      `The values of the Collection child expressions MUST all be type compatible. Different child expressions were found: ${names}`
    );
  }
 
  return names[0];
}
 
function getScalarBuilder(name, expressionBuilder) {
  let isScalar = expressionBuilder.scalarExpressions.includes(name);
  return isScalar ? (x) => x : undefined;
}
 
function getStructureBuilder(name, expressionBuilder) {
  let builder = expressionBuilder["build" + name];
  if (!builder) {
    throw new Error(`Unknown type '${name}' found in collection.`);
  }
 
  return builder;
}
 
function getBuilder(name, expressionBuilder) {
  return (
    getScalarBuilder(name, expressionBuilder) ||
    getStructureBuilder(name, expressionBuilder)
  );
}
 
function getItems(metadata, expressionBuilder) {
  let itemType = getItemType(metadata);
  return itemType
    ? metadata[itemType].map(getBuilder(itemType, expressionBuilder))
    : [];
}
 
/**
 * Envelops an collection. (OASIS-CSDL)
 *
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_Collection
 * (https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/ecc942a0-af88-4012-be6f-439c706641d4)
 *
 * @class Collection
 * @extends {Array}
 */
class Collection extends Array {
  /**
   * Creates an instance of Collection.
   * @param {Object} rawMetadata raw metadata object for the collection
   * @param {Object} expressionBuilder expression builder for creating value objects
   * @memberof Collection
   */
  constructor(rawMetadata, expressionBuilder) {
    let items = getItems(rawMetadata, expressionBuilder);
    super(...items);
 
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    // Expose at least metadata information for conditional items. With current xml parser, it is not possible to correctly
    // parse these expressions since the semantics depends on child elements order which is not provided by current xml parser.
    // http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_IfThenElse
    Object.defineProperty(this, "conditionalItems", {
      get: () => rawMetadata[conditionalType],
    });
  }
}
 
module.exports = Collection;