All files / src/abap/types function_module_definition.ts

80.88% Statements 110/136
54.54% Branches 12/22
85.71% Functions 6/7
80.88% Lines 110/136

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 1361x 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 17x 17x 1x 1x 9x 9x 1x 1x     1x 1x 3x 3x 1x 1x 29x 29x 1x 1x 17x 17x 1x 1x 1x 1x 17x     17x 17x 17x 17x 17x 17x 17x   17x     17x 17x 8x 11x     11x 11x 11x 11x 11x 11x 11x 11x 8x 17x 17x                           17x 17x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 17x 17x 1x 1x
import {xmlToArray} from "../../xml_utils";
 
export enum FunctionModuleParameterDirection {
  importing = "importing",
  exporting = "exporting",
  changing = "changing",
  tables = "tables",
}
 
export interface IFunctionModuleParameter {
  name: string;
  direction: FunctionModuleParameterDirection;
  type: string | undefined;
  optional: boolean;
  defaultValue: string | undefined;
}
 
export enum FunctionModuleType {
  regular = "regular",
  remote = "remoteEnabled",
  update = "update",
}
 
export class FunctionModuleDefinition {
  private name: string;
  private description: string | undefined;
  private parameters: IFunctionModuleParameter[];
  private globalParameters: boolean;
  private moduleType: FunctionModuleType | undefined;
 
  public constructor(data: any) {
    this.parse(data);
  }
 
  public getParameters(): readonly IFunctionModuleParameter[] {
    return this.parameters;
  }
 
  public getModuleType(): FunctionModuleType | undefined {
    return this.moduleType;
  }
 
  public getDescription(): string | undefined {
    return this.description;
  }
 
  public getName(): string {
    return this.name;
  }
 
  public isGlobalParameters(): boolean {
    return this.globalParameters;
  }
 
///////////////
 
  private parse(data: any) {
    if (data.FUNCNAME === undefined) {
      throw new Error("Function module name undefined");
    }
    this.name = data.FUNCNAME;
    this.description = data.SHORT_TEXT;
    this.parameters = [];
    this.globalParameters = data.GLOBAL_FLAG === "X";
 
    this.moduleType = FunctionModuleType.regular;
    if (data.REMOTE_CALL === "R") {
      this.moduleType = FunctionModuleType.remote;
    } else if (data.UPDATE_TASK !== undefined) {
      this.moduleType = FunctionModuleType.update;
    }
 
    if (data.IMPORT) {
      for (const param of xmlToArray(data.IMPORT.RSIMP)) {
        if (param.PARAMETER === undefined) {
          throw new Error("Function module name parameter undefined, importing");
        }
        this.parameters.push({
          name: param.PARAMETER,
          direction: FunctionModuleParameterDirection.importing,
          type: param.TYP || param.DBFIELD,
          optional: param.OPTIONAL === "X",
          defaultValue: param.DEFAULT,
        });
      }
    }
 
    if (data.CHANGING) {
      for (const param of xmlToArray(data.CHANGING.RSCHA)) {
        if (param.PARAMETER === undefined) {
          throw new Error("Function module name parameter undefined, changing");
        }
        this.parameters.push({
          name: param.PARAMETER,
          direction: FunctionModuleParameterDirection.changing,
          type: param.TYP || param.DBFIELD,
          optional: param.OPTIONAL === "X",
          defaultValue: param.DEFAULT,
        });
      }
    }
 
    if (data.EXPORT) {
      for (const param of xmlToArray(data.EXPORT.RSEXP)) {
        if (param.PARAMETER === undefined) {
          throw new Error("Function module name parameter undefined, exporting");
        }
        this.parameters.push({
          name: param.PARAMETER,
          direction: FunctionModuleParameterDirection.exporting,
          type: param.TYP || param.DBFIELD,
          optional: true,
          defaultValue: undefined,
        });
      }
    }
 
    if (data.TABLES) {
      for (const param of xmlToArray(data.TABLES.RSTBL)) {
        if (param.PARAMETER === undefined) {
          throw new Error("Function module name parameter undefined, tables");
        }
        this.parameters.push({
          name: param.PARAMETER,
          direction: FunctionModuleParameterDirection.tables,
          // table types are stored in TYP
          type: param.DBSTRUCT || param.TYP,
          optional: param.OPTIONAL === "X",
          defaultValue: undefined,
        });
      }
    }
 
  }
 
}