All files / src/cds cds_determine_types.ts

75.82% Statements 69/91
75% Branches 24/32
100% Functions 1/1
75.82% Lines 69/91

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 911x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x   12x 12x 12x 12x 42x 15x 15x 15x 13x 13x 15x 15x 15x 1x 1x 1x 1x 1x 1x 15x 4x 4x 4x 4x 4x 4x 4x             10x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x           10x 8x 8x 8x 8x 8x 8x 15x                     42x 27x 27x 27x 27x 27x 42x 12x 12x 12x 1x 1x
import {AbstractType, IRegistry} from "..";
import {IStructureComponent, StructureType, UnknownType, VoidType} from "../abap/types/basic";
import {DDIC} from "../ddic";
import {ParsedDataDefinition} from "../objects";
 
export class CDSDetermineTypes {
 
  public parseType(reg: IRegistry, parsedData: ParsedDataDefinition, ddlsName: string): AbstractType {
    const ddic = new DDIC(reg);
 
    if (parsedData?.fields.length === 0) {
      return new VoidType("DDLS:todo");
    } else {
      const components: IStructureComponent[] = [];
 
      for (const f of parsedData?.fields || []) {
        if (f.prefix !== "") {
          const prefixUpper = f.prefix.toUpperCase();
          let source = parsedData.sources.find((s) => s.as?.toUpperCase() === prefixUpper);
          if (source?.name === undefined) {
            source = parsedData.sources.find((s) => s.name.toUpperCase() === prefixUpper);
          }
          if (source?.name === undefined
              && (parsedData.associations.find((s) => s.name.toUpperCase() === prefixUpper)
              || parsedData.associations.find((s) => s.as?.toUpperCase() === prefixUpper))) {
            components.push({
              name: f.name,
              type: new VoidType("DDLS:association"),
            });
            continue;
          }
          if (source?.name === undefined) {
            if (prefixUpper.startsWith("_")) {
              components.push({
                name: f.name,
                type: new VoidType("DDLS:association"),
              });
              continue;
            }
            components.push({
              name: f.name,
              type: new UnknownType("CDS parser error, unknown source, " + ddlsName),
            });
            continue;
          }
 
          const lookup = ddic.lookupTableOrView(source.name);
          if (lookup.type) {
            if (lookup.type instanceof StructureType) {
              const type = lookup.type.getComponentByName(f.name);
              if (type) {
                components.push({
                  name: f.name,
                  type: type,
                });
              } else {
                components.push({
                  name: f.name,
                  type: new UnknownType(f.name + " not found in " + source.name + ", CDSDetermineTypes"),
                });
              }
            } else {
              // its void or unknown
              components.push({
                name: f.name,
                type: lookup.type,
              });
            }
          } else if (reg.inErrorNamespace(source.name)) {
            components.push({
              name: f.name,
              type: new UnknownType(source.name + " not found, CDSDetermineTypes"),
            });
          } else {
            components.push({
              name: f.name,
              type: new VoidType(source.name),
            });
          }
        } else {
          components.push({
            name: f.name,
            type: new VoidType("DDLS:fieldname:" + ddlsName),
          });
        }
      }
      return new StructureType(components, parsedData.definitionName, parsedData.definitionName, parsedData.description);
    }
  }
 
}