use of cz.habarta.typescript.generator.compiler.ModelTransformer 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);
}
}));
}
use of cz.habarta.typescript.generator.compiler.ModelTransformer in project openremote by openremote.
the class JsonSerializeExtension method getTransformers.
@Override
public List<cz.habarta.typescript.generator.Extension.TransformerDefinition> getTransformers() {
return Arrays.asList(new cz.habarta.typescript.generator.Extension.TransformerDefinition(ModelCompiler.TransformationPhase.BeforeTsModel, new ModelTransformer() {
@Override
public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
return model;
}
@Override
public Model transformModel(SymbolTable symbolTable, Model model) {
// Look for @JsonSerialize annotation and modify the property type accordingly
List<BeanModel> beans = model.getBeans();
beans.replaceAll(bean -> {
AtomicBoolean modified = new AtomicBoolean(false);
List<PropertyModel> properties = bean.getProperties().stream().map(p -> {
Member member = p.getOriginalMember();
JsonSerialize jsonSerialize = null;
if (member instanceof Field) {
Field field = (Field) member;
jsonSerialize = field.getAnnotation(JsonSerialize.class);
} else if (member instanceof Method) {
Method method = (Method) member;
jsonSerialize = method.getAnnotation(JsonSerialize.class);
}
if (jsonSerialize != null) {
// TODO: Add support for other options
if (jsonSerialize.as() != Void.class) {
modified.set(true);
return new PropertyModel(p.getName(), jsonSerialize.as(), p.isOptional(), p.getAccess(), p.getOriginalMember(), p.getPullProperties(), p.getContext(), p.getComments());
}
if (jsonSerialize.converter() != Converter.None.class) {
// Type info is not accessible with reflection so instantiate the converter
Method convertMethod = Arrays.stream(jsonSerialize.converter().getMethods()).filter(m -> m.getName().equals("convert")).findFirst().orElse(null);
if (convertMethod != null) {
modified.set(true);
return new PropertyModel(p.getName(), convertMethod.getGenericReturnType(), p.isOptional(), p.getAccess(), p.getOriginalMember(), p.getPullProperties(), p.getContext(), p.getComments());
}
}
}
return p;
}).collect(Collectors.toList());
if (modified.get()) {
return new BeanModel(bean.getOrigin(), bean.getParent(), bean.getTaggedUnionClasses(), bean.getDiscriminantProperty(), bean.getDiscriminantLiteral(), bean.getInterfaces(), properties, bean.getComments());
}
return bean;
});
return model;
}
}));
}
Aggregations