All files / src/objects view.ts

87.03% Statements 141/162
71.73% Branches 33/46
75% Functions 6/8
87.03% Lines 141/162

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 1631x 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 47x 47x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x     1x 1x 1x 1x 6x 6x 6x 1x 1x 9x 6x 6x 9x     9x 9x 9x 9x 9x 9x 22x     22x         22x     22x 22x 22x 22x 10x 10x 22x 2x 2x 2x 22x 8x 8x   8x 8x 8x 8x 20x 20x 20x 20x 9x 9x 9x 1x 1x 8x 8x 8x 1x 1x       1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 6x 6x 6x 6x 6x 6x 6x 16x 16x 16x 16x 16x 16x 5x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 5x 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,
    },
    fields: {
      VIEWFIELD: string,
      TABNAME: string,
      FIELDNAME: 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: new Types.VoidType("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 getDescription(): string | undefined {
    // todo
    return undefined;
  }
 
///////////////
 
  private parseXML() {
    this.parsedData = {
      header: {
        VIEWCLASS: "",
      },
      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 || "",
    };
 
    const fields = parsed.abapGit["asx:abap"]["asx:values"]?.DD27P_TABLE;
    for (const field of xmlToArray(fields?.DD27P)) {
      this.parsedData.fields.push({
        VIEWFIELD: field.VIEWFIELD,
        TABNAME: field.TABNAME,
        FIELDNAME: field.FIELDNAME,
      });
    }
 
    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,
      });
    }
  }
 
}