All files / src/generator Mapper.ts

98.03% Statements 50/51
93.54% Branches 29/31
100% Functions 2/2
98.03% Lines 50/51

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 732x   2x 2x 2x 2x 2x 2x 2x 2x 2x         30x 30x 30x 2x 28x 11x   11x 2x 9x 1x 8x 2x 6x 3x     3x   17x 1x 16x 3x 13x 3x 10x 1x   9x 8x   1x 1x   2x 2x   1x 1x   2x 2x   2x 2x 2x       1x   30x       2x 2x  
import { SyntaxKind } from "typescript";
 
enum BuiltinType {
  String = "String",
  Boolean = "Boolean",
  Float = "Float",
  Int = "Int",
  Long = "Long",
  Date = "Date",
  DateTime = "DateTime",
  Json = "JSON",
}
 
class Mapper {
  public static mapType(type: any): string {
    let paramType = "";
    try {
      if (!type) {
        paramType = "JSON";
      } else if (type.kind === SyntaxKind.ArrayType) {
        Iif (type.elementType.kind === SyntaxKind.AnyKeyword) {
          paramType = BuiltinType.Json;
        } else if (type?.elementType?.typeName?.escapedText === "integer") {
          paramType = `[${BuiltinType.Int}]`;
        } else if (type?.elementType?.typeName?.escapedText === "long") {
          paramType = `[${BuiltinType.Long}]`;
        } else if (type?.elementType?.typeName?.escapedText === "float") {
          paramType = `[${BuiltinType.Float}]`;
        } else if (type?.elementType?.typeName?.escapedText) {
          paramType = `[${type.elementType.typeName.escapedText}]`;
        } else {
          // map recursively
          paramType = `[${Mapper.mapType(type.elementType)}]`;
        }
      } else if (type.kind === SyntaxKind.AnyKeyword) {
        paramType = BuiltinType.Json;
      } else if (type.kind === SyntaxKind.BooleanKeyword) {
        paramType = BuiltinType.Boolean;
      } else if (type.kind === SyntaxKind.StringKeyword) {
        paramType = BuiltinType.String;
      } else if (type.kind === SyntaxKind.NumberKeyword) {
        paramType = BuiltinType.Float;
      } else {
        const typeName = type.typeName.escapedText;
        switch (typeName) {
          case "Date":
            paramType = BuiltinType.DateTime;
            break;
          case "integer":
            paramType = BuiltinType.Int;
            break;
          case "long":
            paramType = BuiltinType.Long;
            break;
          case "float":
            paramType = BuiltinType.Float;
            break;
          default:
            paramType = typeName || BuiltinType.Json;
            console.log("Defaluting type to JSON, ", type.kind);
            break;
        }
      }
    } catch (ex: any) {
      console.log(ex.message);
    }
    return paramType;
  }
}
 
export default Mapper;
export { BuiltinType };