All files / src/abap/types/basic structure_type.ts

91.89% Statements 68/74
88.23% Branches 15/17
87.5% Functions 7/8
91.89% Lines 68/74

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 741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4638x 4638x 4638x 4638x 4638x 4638x     4638x 4638x 4638x 8883x 1x 1x 8882x 8883x 1x 1x 8881x 8881x 4637x 4637x 1x 1x 4877x 4877x 1x 1x 3241x 3241x 1x 1x 66x 66x 307x 307x 66x 66x 66x 1x 1x 5053x 5053x 1x 1x 21x 21x 21x 21x     1x 1x 293x 293x 1x 1x     1x
import {AbstractType} from "./_abstract_type";
 
export interface IStructureComponent {
  name: string;
  type: AbstractType;
  asInclude?: boolean;
  suffix?: string;
}
 
export class StructureType extends AbstractType {
  private readonly indexed: {[index: string]: AbstractType};
  private readonly components: IStructureComponent[];
 
  public constructor(components: IStructureComponent[], qualifiedName?: string, ddicName?: string, description?: string) {
    super({
      qualifiedName: qualifiedName,
      ddicName: ddicName,
      description: description,
    });
    if (components.length === 0) {
      throw new Error("Structure does not contain any components");
    }
 
    this.indexed = {};
    for (const c of components) {
      if (c.name === "") {
        continue; // unnamed field, eg. "DATA: BEGIN OF str, (10), END OF str."
      }
      const upper = c.name.toUpperCase();
      if (this.indexed[upper] !== undefined) {
        throw new Error("Structure, duplicate field name \"" + upper + "\", " + qualifiedName);
      }
      this.indexed[upper] = c.type;
    }
    this.components = components;
  }
 
  public getComponents(): readonly IStructureComponent[] {
    return this.components;
  }
 
  public getComponentByName(name: string): AbstractType | undefined {
    return this.indexed[name.toUpperCase()];
  }
 
  public toText(level: number) {
    const compo: string[] = [];
    for (const c of this.components) {
      compo.push(c.name + " TYPE " + c.type.toText(level + 1));
    }
    const spaces = "  ".repeat(level);
    return "Structure\n" + spaces + "* " + compo.join("\n" + spaces + "* ");
  }
 
  public isGeneric() {
    return false;
  }
 
  public toABAP(): string {
    const ret = this.getQualifiedName();
    if (ret) {
      return ret;
    }
    return "StructureTypetoABAPtodo";
  }
 
  public containsVoid() {
    return this.getComponents().some(c => { return c.type.containsVoid(); });
  }
 
  public toCDS() {
    return "abap.TODO_STRUCTURE";
  }
}