typescript#EnumDeclaration TypeScript Examples

The following examples show how to use typescript#EnumDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: model-utils.ts    From jmix-frontend with Apache License 2.0 7 votes vote down vote up
/**
 * @param projectModel model context collected from
 * @return entity and enum map with fqn as key
 */
export function collectModelContext(projectModel: ProjectModel) {
  const entities: Entity[] = getEntitiesArray(projectModel.entities);
  const baseProjectEntities: Entity[] = getEntitiesArray(projectModel.baseProjectEntities);

  const entitiesMap = new Map<string, ProjectEntityInfo>();
  const enumsMap = new Map<string, EnumDeclaration>();
  createEnums(projectModel.enums).forEach(en => enumsMap.set(en.fqn, en.node));

  const addEntityToMap = (map: Map<string, ProjectEntityInfo>, isBaseProjectEntity = false) => (e: Entity) => {
    map.set(e.fqn, {
      isBaseProjectEntity,
      entity: e
    })
  };

  entities.forEach(addEntityToMap(entitiesMap));
  baseProjectEntities.forEach(addEntityToMap(entitiesMap, true));
  return {entitiesMap, enumsMap}
}
Example #2
Source File: import-utils.ts    From jmix-frontend with Apache License 2.0 6 votes vote down vote up
export function enumImportInfo(ed: EnumDeclaration, pathPrefix?: string) {
  return {
    className: ed.name.text,
    importPath: normalizeImportPath(path.posix.join(pathPrefix ? pathPrefix : '', `${ENUMS_DIR}/${ENUMS_FILE}`))
  };
}
Example #3
Source File: entities-generation.ts    From jmix-frontend with Apache License 2.0 4 votes vote down vote up
/**
 * TS attribute type - could be primitive, enum or entity class (single or array, depends on relation cardinality)
 *
 * @param entityAttr attribute which type should be computed
 * @param ctx context of attribute owner class
 */
function createAttributeType(entityAttr: EntityAttribute, ctx: ClassCreationContext): {
  node: ts.TypeNode
  importInfo: ImportInfo | undefined
} {

  let node: ts.TypeNode | undefined;
  let refEntity: ProjectEntityInfo | undefined;
  let enumDeclaration: EnumDeclaration | undefined;

  const {mappingType} = entityAttr;

  // primitive

  if (mappingType === 'DATATYPE') {
    switch (entityAttr.type.fqn) {
      case 'java.lang.Boolean':
        node = ts.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
        break;
      case 'java.lang.Integer':
        node = ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
        break;
      case 'java.lang.String':
        node = ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
        break;
      default:
        node = ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
        break;
    }
  }

  //objects

  if (mappingType === 'ASSOCIATION' || mappingType === 'COMPOSITION') {
    refEntity = ctx.entitiesMap.get(entityAttr.type.fqn);

    if (refEntity) {
      switch (entityAttr.cardinality) {
        case 'MANY_TO_MANY':
        case 'ONE_TO_MANY':
          node = ts.createArrayTypeNode(
            ts.createTypeReferenceNode(entityAttr.type.className, undefined)
          );
          break;
        case 'ONE_TO_ONE':
        case 'MANY_TO_ONE':
        default:
          node = ts.createTypeReferenceNode(entityAttr.type.className, undefined);
          break;
      }
    }
  }

  //enums

  if (mappingType == 'ENUM') {
    enumDeclaration = ctx.enumsMap.get(entityAttr.type.fqn);
    if (enumDeclaration) {
      node = ts.createTypeReferenceNode(enumDeclaration.name.text, undefined);
    }
  }

  if (!node) {
    node = ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
  }

  let importInfo = undefined;

  if (refEntity && refEntity.entity) importInfo = createImportInfo(refEntity, ctx.isBaseProjectEntity);

  if (enumDeclaration) importInfo = enumImportInfo(enumDeclaration, ctx.isBaseProjectEntity ? '../..' : '..');

  return {
    node,
    importInfo
  };
}
Example #4
Source File: entities-generation.test.ts    From jmix-frontend with Apache License 2.0 4 votes vote down vote up
describe('generate TS entity', function () {

  it('should create entity class', function () {
    const enMap = fillEntitiesMap([
        'com.company.mpg.entity.Garage',
        'com.company.mpg.entity.TechnicalCertificate',
      ],
      ['com.haulmont.cuba.core.entity.FileDescriptor']);

    const enumsMap = new Map<string, EnumDeclaration>();
    enumsMap.set('com.company.mpg.entity.CarType', {name: {text: 'CarType'}} as any);
    enumsMap.set('com.company.mpg.entity.EcoRank', {name: {text: 'EcoRank'}} as any);

    const classTsNode = createEntityClass({
      entity: entityModel,
      entitiesMap: enMap,
      enumsMap,
      isBaseProjectEntity: false
    });
    let content = renderTSNodes([classTsNode.classDeclaration]);

    let expected = '' +
      `export class Car {
      static NAME = "mpg$Car";
      manufacturer?: string | null;
      model?: string | null;
      regNumber?: string | null;
      purchaseDate?: any | null;
      wheelOnRight?: boolean | null;
      carType?: CarType | null;
      ecoRank?: EcoRank | null;
      garage?: Garage | null;
      maxPassengers?: number | null;
      price?: any | null;
      mileage?: any | null;
      technicalCertificate?: TechnicalCertificate | null;
      photo?: FileDescriptor | null;
    }`;
    assertContent(content, expected);

    const includes = createIncludes(classTsNode.importInfos, undefined);
    content = renderTSNodes(includes);
    expected = '' +
      `import { CarType, EcoRank } from "../enums/enums";
      import { Garage } from "./mpg$Garage";
      import { TechnicalCertificate } from "./mpg$TechnicalCertificate";
      import { FileDescriptor } from "./sys$FileDescriptor";`;

    assertContent(content, expected);
  });

  it('should create an Integer ID entity class', () => {
    const integerIdEntityModel = projectModel.entities.find((e: any) => e.name === 'scr_IntegerIdTestEntity');

    const classTsNode = createEntityClass({
      entity: integerIdEntityModel,
      entitiesMap: new Map<string, ProjectEntityInfo>(),
      enumsMap: new Map<string, EnumDeclaration>(),
      isBaseProjectEntity: false
    });
    const content = renderTSNodes([classTsNode.classDeclaration]);

    const expected =
      `export class IntegerIdTestEntity {
        static NAME = "scr_IntegerIdTestEntity";
        description?: string | null;
        createTs?: any | null;
        createdBy?: string | null;
        updateTs?: any | null;
        updatedBy?: string | null;
        deleteTs?: any | null;
        deletedBy?: string | null;
        version?: number | null;
        datatypesTestEntity3?: any | null;
        datatypesTestEntities?: any | null;
      }`;
      assertContent(content, expected);
  });

  it('should create a String ID entity class', () => {
    const stringIdEntityModel = projectModel.entities.find((e: any) => e.name === 'scr_StringIdTestEntity');

    const classTsNode = createEntityClass({
      entity: stringIdEntityModel,
      entitiesMap: new Map<string, ProjectEntityInfo>(),
      enumsMap: new Map<string, EnumDeclaration>(),
      isBaseProjectEntity: false
    });
    const content = renderTSNodes([classTsNode.classDeclaration]);

    const expected =
      `export class StringIdTestEntity {
        static NAME = "scr_StringIdTestEntity";
        id?: string;
        description?: string | null;
        productCode?: string | null;
        createTs?: any | null;
        createdBy?: string | null;
        updateTs?: any | null;
        updatedBy?: string | null;
        deleteTs?: any | null;
        deletedBy?: string | null;
        version?: number | null;
        datatypesTestEntity?: any | null;
        datatypesTestEntity3?: any | null;
      }`;
    assertContent(content, expected);
  });

  it('should create a class for a String ID entity that has an attribute named `id` ' +
    'but the actual ID attribute has a different name', () => {
    const weirdStringIdEntityModel =
      projectModel.entities.find((e: any) => e.name === 'scr_WeirdStringIdTestEntity');

    const classTsNode = createEntityClass({
      entity: weirdStringIdEntityModel,
      entitiesMap: new Map<string, ProjectEntityInfo>(),
      enumsMap: new Map<string, EnumDeclaration>(),
      isBaseProjectEntity: false
    });
    const content = renderTSNodes([classTsNode.classDeclaration]);

    const expected =
      `export class WeirdStringIdTestEntity {
        static NAME = "scr_WeirdStringIdTestEntity";
        id?: string;
        description?: string | null;
        createTs?: any | null;
        createdBy?: string | null;
        updateTs?: any | null;
        updatedBy?: string | null;
        deleteTs?: any | null;
        deletedBy?: string | null;
        version?: number | null;
        datatypesTestEntity3?: any | null;
      }`;
    assertContent(content, expected);
  });
});