All files / src/objects proxy_object.ts

84.71% Statements 133/157
50% Branches 15/30
71.42% Functions 5/7
84.71% Lines 133/157

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 1581x 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 7x 7x 1x 1x           1x 1x         1x 1x 2x 2x 2x 1x 1x 6x 4x 4x 2x 2x 2x 2x 2x 2x 6x 1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 1x 1x 4x 4x 4x 4x     4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x  
import {xmlToArray} from "../xml_utils";
import {AbstractObject} from "./_abstract_object";
import {Interface} from "./interface";
import {MemoryFile} from "../files/memory_file";
import {IRegistry} from "../_iregistry";
import {ABAPRelease} from "../version";
 
interface ProxyDataItem {
  OBJECT?: string;
  OBJ_NAME?: string;
  OBJECT1?: string;
  OBJ_NAME1?: string;
  OBJECT2?: string;
  OBJ_NAME2?: string;
  OBJECT_R?: string;
  OBJ_NAME_R?: string;
  R3_TYPE?: string;
  R3_NAME?: string;
  R3_TEXT?: string;
  IFR_TYPE?: string;
  IFR_NAME?: string;
}
 
interface ParsedProxy {
  proxyData: ProxyDataItem[];
}
 
export class ProxyObject extends AbstractObject {
  private parsedXML: ParsedProxy | undefined;
 
  public getType(): string {
    return "SPRX";
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 34,
      allowNamespace: true,
    };
  }
 
  public getDescription(): string | undefined {
    this.parse();
    const intfItem = this.parsedXML?.proxyData.find(i => i.R3_TYPE === "INTF");
    return intfItem?.R3_TEXT;
  }
 
  public setDirty(): void {
    this.parsedXML = undefined;
    super.setDirty();
  }
 
  public parse(_release?: ABAPRelease, _globalMacros?: readonly string[], reg?: IRegistry) {
    if (this.parsedXML) {
      return {updated: false, runtime: 0};
    }
 
    const start = Date.now();
    this.parsedXML = this.parseXML();
    const end = Date.now();
 
    const objects = this.generateABAPObjects();
    for (const obj of objects) {
      reg?.addDependencies(obj.getFiles());
    }
 
    return {updated: true, runtime: end - start};
  }
 
  private parseXML(): ParsedProxy {
    const result: ParsedProxy = {proxyData: []};
    const parsed = super.parseRaw2();
 
    if (parsed === undefined
        || parsed.abapGit === undefined
        || parsed.abapGit?.["asx:abap"]?.["asx:values"] === undefined) {
      return result;
    }
 
    const values = parsed?.abapGit?.["asx:abap"]?.["asx:values"];
    result.proxyData = xmlToArray(values.PROXY_DATA?.item);
 
    return result;
  }
 
  public generateABAPObjects(): AbstractObject[] {
    this.parse();
    const result: AbstractObject[] = [];
 
    if (!this.parsedXML) {
      return result;
    }
 
    // Find interface definition
    const intfItem = this.parsedXML.proxyData.find(i => i.R3_TYPE === "INTF");
    if (!intfItem || typeof intfItem.R3_NAME !== "string") {
      return result;
    }
 
    const intfName = intfItem.R3_NAME.toLowerCase();
 
    // Find methods
    const methods = this.parsedXML.proxyData.filter(i => i.R3_TYPE === "METH");
 
    // Build interface code
    let code = `INTERFACE ${intfName} PUBLIC.\n`;
 
    for (const method of methods) {
      if (typeof method.R3_NAME !== "string") {
        continue;
      }
      const methodName = method.R3_NAME.toLowerCase();
 
      // Find parameters for this method
      const importingParameters = this.parsedXML.proxyData.filter(
        i => i.R3_TYPE === "PAIM" && i.OBJ_NAME1 === method.OBJ_NAME1
      );
      const exportingParameters = this.parsedXML.proxyData.filter(
        i => i.R3_TYPE === "PAEX" && i.OBJ_NAME1 === method.OBJ_NAME1
      );
 
      code += `  METHODS ${methodName}\n`;
 
      if (importingParameters.length > 0) {
        code += `    IMPORTING\n`;
        for (let i = 0; i < importingParameters.length; i++) {
          const param = importingParameters[i];
          const paramName = typeof param.OBJ_NAME2 === "string" ? param.OBJ_NAME2.toLowerCase() : undefined;
          const paramType = typeof param.OBJ_NAME_R === "string" ? param.OBJ_NAME_R.toLowerCase() : undefined;
          const isLast = i === importingParameters.length - 1 && exportingParameters.length === 0;
          code += `      ${paramName} TYPE ${paramType}${isLast ? "." : ""}\n`;
        }
      }
 
      if (exportingParameters.length > 0) {
        code += `    EXPORTING\n`;
        for (let i = 0; i < exportingParameters.length; i++) {
          const param = exportingParameters[i];
          const paramName = typeof param.OBJ_NAME2 === "string" ? param.OBJ_NAME2.toLowerCase() : undefined;
          const paramType = typeof param.OBJ_NAME_R === "string" ? param.OBJ_NAME_R.toLowerCase() : undefined;
          const isLast = i === exportingParameters.length - 1;
          code += `      ${paramName} TYPE ${paramType}${isLast ? "." : ""}\n`;
        }
      }
    }
 
    code += `ENDINTERFACE.`;
 
    // Create the interface object
    const intf = new Interface(intfName.toUpperCase());
    intf.addFile(new MemoryFile(`${intfName}.intf.abap`, code));
 
    result.push(intf);
 
    return result;
  }
}