Search in sources :

Example 1 with EnumMemberModel

use of cz.habarta.typescript.generator.compiler.EnumMemberModel 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 EnumMemberModel

use of cz.habarta.typescript.generator.compiler.EnumMemberModel 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 EnumMemberModel

use of cz.habarta.typescript.generator.compiler.EnumMemberModel in project typescript-generator by vojtechhabarta.

the class Jackson2Parser method parseEnumOrObjectEnum.

private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass) {
    final JsonFormat jsonFormat = sourceClass.type.getAnnotation(JsonFormat.class);
    if (jsonFormat != null && jsonFormat.shape() == JsonFormat.Shape.OBJECT) {
        return parseBean(sourceClass);
    }
    final boolean isNumberBased = jsonFormat != null && (jsonFormat.shape() == JsonFormat.Shape.NUMBER || jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT);
    final List<EnumMemberModel> enumMembers = new ArrayList<>();
    if (sourceClass.type.isEnum()) {
        final Class<?> enumClass = (Class<?>) sourceClass.type;
        try {
            Method valueMethod = null;
            final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass);
            for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
                final Method readMethod = propertyDescriptor.getReadMethod();
                if (readMethod.isAnnotationPresent(JsonValue.class)) {
                    valueMethod = readMethod;
                }
            }
            int index = 0;
            for (Field field : enumClass.getFields()) {
                if (field.isEnumConstant()) {
                    if (isNumberBased) {
                        final Number value = getNumberEnumValue(field, valueMethod, index++);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    } else {
                        final String value = getStringEnumValue(field, valueMethod);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName()));
            e.printStackTrace(System.out);
        }
    }
    return new EnumModel(sourceClass.type, isNumberBased ? EnumKind.NumberBased : EnumKind.StringBased, enumMembers, null);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) Field(java.lang.reflect.Field) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat)

Example 4 with EnumMemberModel

use of cz.habarta.typescript.generator.compiler.EnumMemberModel in project typescript-generator by vojtechhabarta.

the class Javadoc method enrichEnum.

private EnumModel enrichEnum(EnumModel enumModel) {
    final Enum dEnum = findJavadocEnum(enumModel.getOrigin(), dRoots);
    final List<EnumMemberModel> enrichedMembers = new ArrayList<>();
    for (EnumMemberModel member : enumModel.getMembers()) {
        final EnumMemberModel enrichedMember = enrichEnumMember(member, dEnum);
        enrichedMembers.add(enrichedMember);
    }
    final String enumComment = dEnum != null ? dEnum.getComment() : null;
    final List<TagInfo> tags = dEnum != null ? dEnum.getTag() : null;
    return enumModel.withMembers(enrichedMembers).withComments(Utils.concat(getComments(enumComment, tags), enumModel.getComments()));
}
Also used : Enum(cz.habarta.typescript.generator.xmldoclet.Enum) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TagInfo(cz.habarta.typescript.generator.xmldoclet.TagInfo)

Example 5 with EnumMemberModel

use of cz.habarta.typescript.generator.compiler.EnumMemberModel in project typescript-generator by vojtechhabarta.

the class Emitter method emitLiteralEnum.

private void emitLiteralEnum(TsEnumModel enumModel, boolean exportKeyword, boolean declareKeyword) {
    writeNewLine();
    emitComments(enumModel.getComments());
    final String declareText = declareKeyword ? "declare " : "";
    final String constText = settings.nonConstEnums ? "" : "const ";
    writeIndentedLine(exportKeyword, declareText + constText + "enum " + enumModel.getName().getSimpleName() + " {");
    indent++;
    for (EnumMemberModel member : enumModel.getMembers()) {
        emitComments(member.getComments());
        final Object value = member.getEnumValue();
        final String initializer = value != null ? " = " + (value instanceof String ? quote((String) value, settings) : String.valueOf(value)) : "";
        writeIndentedLine(member.getPropertyName() + initializer + ",");
    }
    indent--;
    writeIndentedLine("}");
}
Also used : EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel)

Aggregations

EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)6 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)3 Field (java.lang.reflect.Field)2 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)1 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 Enum (cz.habarta.typescript.generator.xmldoclet.Enum)1 TagInfo (cz.habarta.typescript.generator.xmldoclet.TagInfo)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1