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

100% Statements 25/25
100% Branches 8/8
100% Functions 11/11
100% Lines 25/25

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 89 90 91 92 93 94 95 96 97 98 99    1x 1x                                     25x   25x 6x       25x     25x 6x     25x     25x 1x     25x     25x 1x     25x 4x     25x 2x                       6x 7x     6x       2x 1x                       4x                 1x  
"use strict";
 
const _ = require("lodash");
const AnnotationTarget = require("../annotations/AnnotationTarget");
 
/**
 * Envelops a (structural) property.
 *
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_StructuralProperty
 *
 * Geometry or geography property not supported (SRID attribute)
 *
 * @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",
    });
 
    // in v 4.0 it is posible to use 'max' symbolic value, it is deprecated in 4.01. Not sure if sap implements 4.0 or 4.01
    let maxLength = _.has(rawMetadata.$, "MaxLength")
      ? Number(rawMetadata.$.MaxLength)
      : undefined;
    Object.defineProperty(this, "maxLength", {
      get: () => maxLength,
    });
 
    let precision = _.has(rawMetadata.$, "Precision")
      ? Number(rawMetadata.$.Precision)
      : undefined;
    Object.defineProperty(this, "precision", {
      get: () => precision,
    });
 
    let scale = _.has(rawMetadata.$, "Scale")
      ? Number(rawMetadata.$.Scale)
      : undefined;
    Object.defineProperty(this, "scale", {
      get: () => scale,
    });
 
    Object.defineProperty(this, "readOnly", {
      get: () => rawMetadata.$.ReadOnly === "true",
    });
 
    Object.defineProperty(this, "unicode", {
      get: () => rawMetadata.$.Unicode !== "false",
    });
  }
 
  /**
   * 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),
    });
 
    if (_.has(this.raw.$, "DefaultValue")) {
      // Values must be represented as defined in OData ABNF Construction Rules, e.g. binary in base64url encoding
      // Edm type implementation doesn't offer such conversion, so leaving as plain string for now
      // also not checking now that type is primitive or enumeration
      Object.defineProperty(this, "defaultValue", {
        get: () => this.raw.$.DefaultValue,
      });
    }
  }
 
  /**
   * 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;