All files / src/objects view.ts

86.77% Statements 164/189
75.82% Branches 69/91
88.88% Functions 8/9
86.77% Lines 164/189

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 1901x 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 56x 56x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x     1x 1x 1x 1x 9x 9x 9x 1x 1x 10x 9x 9x 10x     10x 10x 10x 10x 10x 10x 18x     18x         18x     18x 18x 18x 18x 6x 6x 18x       18x 6x 6x   6x 6x 6x 6x 18x 18x 18x 18x 10x 10x 10x 4x 4x 6x 6x 6x 1x 1x 1x     1x 1x 1x 4x 3x 3x 4x 1x 1x 1x 1x 2x     2x 2x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 7x 9x 9x 9x 9x 9x 9x 9x 9x 17x 17x 17x 3x 3x 14x 14x 14x 14x 14x 14x 14x 7x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 7x 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,
    }[],
    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) {
        components.push({
          name: field.VIEWFIELD,
          type: Types.VoidType.get("ExternalView")});
        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,
      });
    }
 
    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,
      });
    }
  }
 
}