Search in sources :

Example 1 with TsEnumModel

use of cz.habarta.typescript.generator.emitter.TsEnumModel in project typescript-generator by vojtechhabarta.

the class EnumConstantsExtension method emitElements.

@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
    String exportString = exportKeyword ? "export " : "";
    List<TsEnumModel> enums = model.getOriginalStringEnums();
    Collections.sort(enums);
    for (TsEnumModel tsEnum : enums) {
        writer.writeIndentedLine("");
        writer.writeIndentedLine(exportString + "const " + tsEnum.getName().getSimpleName() + " = {");
        for (EnumMemberModel member : tsEnum.getMembers()) {
            writer.writeIndentedLine(settings.indentString + member.getPropertyName() + ": " + "<" + tsEnum.getName().getSimpleName() + ">\"" + member.getEnumValue() + "\",");
        }
        writer.writeIndentedLine("}");
    }
}
Also used : EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 2 with TsEnumModel

use of cz.habarta.typescript.generator.emitter.TsEnumModel in project typescript-generator by vojtechhabarta.

the class NonConstEnumsExtension method emitElements.

@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
    String exportString = exportKeyword ? "export " : "";
    List<TsEnumModel> enums = model.getOriginalStringEnums();
    Collections.sort(enums);
    for (TsEnumModel tsEnum : enums) {
        writer.writeIndentedLine("");
        writer.writeIndentedLine(exportString + "enum " + tsEnum.getName().getSimpleName() + " {");
        for (EnumMemberModel member : tsEnum.getMembers()) {
            writer.writeIndentedLine(settings.indentString + member.getPropertyName() + ",");
        }
        writer.writeIndentedLine("}");
    }
}
Also used : EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 3 with TsEnumModel

use of cz.habarta.typescript.generator.emitter.TsEnumModel in project typescript-generator by vojtechhabarta.

the class ClassEnumExtension method getTransformers.

@Override
public List<TransformerDefinition> getTransformers() {
    return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.BeforeEnums, new ModelTransformer() {

        @Override
        public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
            List<TsBeanModel> beans = model.getBeans();
            List<TsBeanModel> classEnums = new ArrayList<>();
            for (TsBeanModel bean : beans) {
                if (bean.getName().getSimpleName().contains(classEnumPattern))
                    classEnums.add(bean);
            }
            List<TsEnumModel> stringEnums = new ArrayList<>();
            for (TsBeanModel tsBeanModel : classEnums) {
                List<EnumMemberModel> members = new ArrayList<>();
                for (Field declaredField : tsBeanModel.getOrigin().getDeclaredFields()) {
                    if (declaredField.getType().getName().equals(tsBeanModel.getOrigin().getName())) {
                        members.add(new EnumMemberModel(declaredField.getName(), declaredField.getName(), null));
                    }
                }
                TsEnumModel temp = new TsEnumModel(tsBeanModel.getOrigin(), tsBeanModel.getName(), EnumKind.StringBased, members, null);
                stringEnums.add(temp);
            }
            return model.withEnums(stringEnums).withoutBeans(classEnums);
        }
    }));
}
Also used : EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) Field(java.lang.reflect.Field) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) TsModel(cz.habarta.typescript.generator.emitter.TsModel) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Aggregations

EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)3 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)3 ModelTransformer (cz.habarta.typescript.generator.compiler.ModelTransformer)1 SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)1 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 TsModel (cz.habarta.typescript.generator.emitter.TsModel)1 Field (java.lang.reflect.Field)1