All files / lib/model/nw/schema Property.js

100% Statements 14/14
100% Branches 2/2
100% Functions 7/7
100% Lines 14/14

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    1x 1x                                   41x   41x 8x     41x     41x 8x     41x 6x                       13x 8x                     6x                 1x  
"use strict";
 
const _ = require("lodash");
const AnnotationTarget = require("../../oasis/annotations/AnnotationTarget");
 
/**
 * Envelops a (structural) property.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/50129087-bb7f-475e-a14d-7a8a4bdef966
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_StructuralProperty
 *
 * @class Property
 * @extends {AnnotationTarget}
 */
class Property extends AnnotationTarget {
  /**
   * Creates an instance of Property.
   * @param {Object} rawMetadata raw metadata object for property
   * @memberof Property
   */
  constructor(rawMetadata) {
    super(rawMetadata);
 
    Object.defineProperty(this, "nullable", {
      get: () => rawMetadata.$.Nullable !== "false",
    });
 
    let maxLength = _.has(rawMetadata.$, "MaxLength")
      ? Number(rawMetadata.$.MaxLength)
      : undefined;
    Object.defineProperty(this, "maxLength", {
      get: () => maxLength,
    });
 
    Object.defineProperty(this, "readOnly", {
      get: () => rawMetadata.$.ReadOnly === "true",
    });
  }
 
  /**
   * Initializes schema dependent properties. Decoupled from constructor,
   * because it needs to resolve schema (type) references.
   *
   * @param {CsdlSchema} schema to resolve references
   * @memberof AssociationEnd
   */
  initSchemaDependentProperties(schema) {
    Object.defineProperty(this, "type", {
      get: () => schema.getType(this.raw.$.Type),
    });
  }
 
  /**
   * Gets legacy api object. (XML casing, maybe some other changes.)
   *
   * @returns {Object} legacy api object
   * @memberof Property
   */
  getLegacyApiObject() {
    return Object.assign(super.getLegacyApiObject(), {
      MaxLength: this.maxLength,
      Nullable: this.nullable,
      ReadOnly: this.readOnly,
      Type: this.type.namespaceQualifiedName,
    });
  }
}
 
module.exports = Property;