All files / src/objects view.ts

89.75% Statements 184/205
78.64% Branches 81/103
88.88% Functions 8/9
89.75% Lines 184/205

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 2061x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x     1x 1x 1x 1x 12x 12x 12x 1x 1x 17x 12x 12x 17x     17x 17x 17x 17x 17x 17x 39x     39x 21x 21x 21x 15x 15x 15x 15x 15x 15x 15x 21x 21x 21x 21x 39x     18x 18x 18x 39x 6x 6x 39x       39x 6x 6x   6x 6x 6x 6x 18x 18x 18x 18x 17x 17x 17x 4x 4x 13x 13x 13x 1x 1x 1x     1x 1x 1x 4x 3x 3x 4x 1x 1x 1x 1x 2x     2x 2x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 2x 2x 10x 12x 12x 12x 12x 12x 12x 12x 12x 26x 26x 26x 3x 3x 23x 23x 23x 23x 23x 26x 26x 26x 26x 26x 10x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 10x 1x 1x  
import * as Types from "../abap/types/basic";
import {AbstractObject} from "./_abstract_object";
import {xmlToArray} from "../xml_utils";
import {IRegistry} from "../_iregistry";
import {DDIC} from "../ddic";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {IObjectAndToken} from "../_iddic_references";
 
enum ViewClass {
  ExternalView = "X",
}
 
export class View extends AbstractObject {
  private parsedData: {
    header: {
      VIEWCLASS: string,
      DDTEXT: string,
    },
    fields: {
      VIEWFIELD: string,
      TABNAME: string,
      FIELDNAME: string,
      KEYFLAG: string,
      EXVDATATYPE?: string,
      EXVFLENGTH?: string,
      EXVDECIMALS?: string,
    }[],
    join: {
      LTAB: string,
      LFIELD: string,
      OPERATOR: string,
      RTAB: string,
      RFIELD: string,
    }[]} | undefined;
 
  public getType(): string {
    return "VIEW";
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 16,
      allowNamespace: true,
    };
  }
 
  public getFields() {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    return this.parsedData?.fields;
  }
 
  public getJoin() {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    return this.parsedData?.join;
  }
 
  public setDirty(): void {
    this.parsedData = undefined;
    super.setDirty();
  }
 
  public parseType(reg: IRegistry): AbstractType {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    if (this.parsedData === undefined) {
      return new Types.UnknownType("View, parser error", this.getName());
    }
 
    const components: Types.IStructureComponent[] = [];
    const references: IObjectAndToken[] = [];
 
    const ddic = new DDIC(reg);
    for (const field of this.parsedData.fields) {
      if (field.VIEWFIELD === "*" || field.VIEWFIELD === "-") {
        // ignore, this is a special case of old style .INCLUDE
        continue;
      } else if (this.parsedData.header.VIEWCLASS === ViewClass.ExternalView) {
        // for external views TABNAME is the view itself, the types are given directly in the xml
        let type: AbstractType = Types.VoidType.get("ExternalView");
        if (field.EXVDATATYPE !== undefined) {
          type = ddic.textToType({
            text: field.EXVDATATYPE,
            length: field.EXVFLENGTH,
            decimals: field.EXVDECIMALS,
            infoText: this.getName() + "-" + field.VIEWFIELD,
          });
        }
        components.push({
          name: field.VIEWFIELD,
          type: type});
        continue;
      } else if (field.TABNAME === this.getName()) {
        throw new Error("Unexpected self reference in view " + this.getName() + ", " + field.FIELDNAME + " " + field.FIELDNAME);
      }
 
      const lookup = ddic.lookupTableOrView(field.TABNAME);
      let found = lookup.type;
      if (lookup.object) {
        references.push({object: lookup.object});
      }
      if (field.VIEWFIELD === ".APPEND") {
// it is already expanded in the abapGit xml
        continue;
      }
      if (found instanceof Types.StructureType) {
        const s = found.getComponentByName(field.FIELDNAME);
        if (s === undefined) {
          found = new Types.UnknownType(field.FIELDNAME + " not found in " + field.TABNAME + ", VIEW parse type");
        } else {
          found = s;
        }
      }
      components.push({
        name: field.VIEWFIELD,
        type: found});
    }
 
    reg.getDDICReferences().setUsing(this, references);
    if (components.length === 0) {
      return new Types.UnknownType("View " + this.getName() + " does not contain any components");
    }
 
    return new Types.StructureType(components, this.getName());
  }
 
  public listKeys(): string[] {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
 
    const ret: string[] = [];
    for (const p of this.parsedData?.fields || []) {
      if (p.KEYFLAG === "X") {
        ret.push(p.FIELDNAME);
      }
    }
    return ret;
  }
 
  public getDescription(): string | undefined {
    if (this.parsedData === undefined) {
      this.parseXML();
    }
    return this.parsedData?.header.DDTEXT;
  }
 
///////////////
 
  private parseXML() {
    this.parsedData = {
      header: {
        VIEWCLASS: "",
        DDTEXT: "",
      },
      fields: [],
      join: [],
    };
 
    const parsed = super.parseRaw2();
    if (parsed === undefined || parsed.abapGit === undefined) {
      return;
    }
 
    const header = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.DD25V;
    this.parsedData.header = {
      VIEWCLASS: header?.VIEWCLASS || "",
      DDTEXT: header?.DDTEXT || "",
    };
 
    const fields = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.DD27P_TABLE;
    for (const field of xmlToArray(fields?.DD27P)) {
      if (typeof field?.VIEWFIELD !== "string"
          || typeof field?.TABNAME !== "string"
          || typeof field?.FIELDNAME !== "string") {
        continue;
      }
      this.parsedData.fields.push({
        VIEWFIELD: field.VIEWFIELD,
        TABNAME: field.TABNAME,
        FIELDNAME: field.FIELDNAME,
        KEYFLAG: field.KEYFLAG,
        EXVDATATYPE: typeof field.EXVDATATYPE === "string" && field.EXVDATATYPE !== "" ? field.EXVDATATYPE : undefined,
        EXVFLENGTH: typeof field.EXVFLENGTH === "string" ? field.EXVFLENGTH : undefined,
        EXVDECIMALS: typeof field.EXVDECIMALS === "string" ? field.EXVDECIMALS : undefined,
      });
    }
 
    const join = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.DD28J_TABLE;
    for (const j of xmlToArray(join?.DD28J)) {
      this.parsedData.join.push({
        LTAB: j.LTAB,
        LFIELD: j.LFIELD,
        OPERATOR: j.OPERATOR,
        RTAB: j.RTAB,
        RFIELD: j.RFIELD,
      });
    }
  }
 
}