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("}");
}
}
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("}");
}
}
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);
}
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()));
}
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("}");
}
Aggregations