All files / lib/model/oasis/schema Function.js

100% Statements 28/28
100% Branches 8/8
100% Functions 10/10
100% Lines 27/27

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 80 81 82 83 84 85 86 87 88    1x 1x 1x 1x                                 9x 9x 1x     8x       1x         7x 7x 2x     7x 6x   7x 4x     7x 2x     7x 2x     7x 1x                         1x 1x 1x                   1x       1x  
"use strict";
 
const _ = require("lodash");
const AnnotationTarget = require("../annotations/AnnotationTarget");
const Parameter = require("./Parameter");
const ReturnType = require("./ReturnType");
 
/**
 * Function - service-defined operation.
 *
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_Function
 *
 * @class Function
 * @extends {AnnotationTarget}
 */
class Function extends AnnotationTarget {
  /**
   * Creates an instance of Function
   * @param {Object} rawMetadata raw metadata object for a function
   * @memberof Function
   */
  constructor(rawMetadata) {
    super(rawMetadata);
    if (!this.name) {
      throw new Error("Name attribute is mandatory for function.");
    }
 
    if (
      !_.isArray(rawMetadata.ReturnType) ||
      rawMetadata.ReturnType.length !== 1
    ) {
      throw new Error(
        `Function ${this.name} must contain one ReturnType element`
      );
    }
 
    let returnType = new ReturnType(rawMetadata.ReturnType[0]);
    Object.defineProperty(this, "returnType", {
      get: () => returnType,
    });
 
    let parameters = (rawMetadata.Parameter || []).map(
      (md) => new Parameter(md)
    );
    Object.defineProperty(this, "parameters", {
      get: () => parameters,
    });
 
    Object.defineProperty(this, "isBound", {
      get: () => rawMetadata.$.IsBound === "true",
    });
 
    Object.defineProperty(this, "isComposable", {
      get: () => rawMetadata.$.IsComposable === "true",
    });
 
    Object.defineProperty(this, "entitySetPath", {
      get: () => rawMetadata.$.EntitySetPath,
    });
  }
 
  /**
   * Initializes schema dependent properties. Decoupled from constructor,
   * because it needs to resolve schema (type) references.
   *
   * @param {CsdlSchema} schema to resolve references
   * @returns {Function} this to allow methods chaining
   * @memberof Function
   */
  initSchemaDependentProperties(schema) {
    this.returnType.initSchemaDependentProperties(schema);
    this.parameters.forEach((p) => p.initSchemaDependentProperties(schema));
    return this;
  }
 
  /**
   * Resolves model path within this type.
   *
   * @returns {Object} itself
   * @memberof Function
   */
  resolveModelPath() {
    return this;
  }
}
 
module.exports = Function;