All files / src/objects proxy_object.ts

88.11% Statements 126/143
50% Branches 11/22
71.42% Functions 5/7
88.11% Lines 126/143

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 1441x 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 4x 4x 1x 1x           1x 1x         1x 1x 1x 1x 1x 1x 1x 4x 3x 3x 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 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 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 {Version} 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(_version?: Version, _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 || !intfItem.R3_NAME) {
      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) {
      const methodName = method.R3_NAME?.toLowerCase();
      if (!methodName) {
        continue;
      }
 
      // Find parameters for this method
      const params = this.parsedXML.proxyData.filter(
        i => i.R3_TYPE === "PAIM" && i.OBJ_NAME1 === method.OBJ_NAME1
      );
 
      code += `  METHODS ${methodName}\n`;
 
      if (params.length > 0) {
        code += `    IMPORTING\n`;
        for (let i = 0; i < params.length; i++) {
          const param = params[i];
          const paramName = param.OBJ_NAME2?.toLowerCase();
          const paramType = param.OBJ_NAME_R?.toLowerCase();
          const isLast = i === params.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;
  }
}