use of cz.habarta.typescript.generator.emitter.TsPropertyModel in project openremote by openremote.
the class CustomExtension method getTransformers.
@Override
public List<TransformerDefinition> getTransformers() {
return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.BeforeEnums, (TsModelTransformer) (context, model) -> {
// Special processing for AssetModelInfo meta item value descriptors as JsonSerialize extension doesn't support
// @JsonSerialize(contentConverter=...)
TsBeanModel assetTypeInfoBean = model.getBean(AssetTypeInfo.class);
if (assetTypeInfoBean != null) {
assetTypeInfoBean.getProperties().replaceAll(p -> p.getName().equals("metaItemDescriptors") || p.getName().equals("valueDescriptors") ? new TsPropertyModel(p.getName(), new TsType.BasicArrayType(TsType.String), p.modifiers, p.ownProperty, p.comments) : p);
}
Constants.IGNORE_TYPE_PARAMS_ON_CLASSES.forEach(beanClass -> {
TsBeanModel bean = model.getBean(beanClass);
if (bean != null && bean.getTypeParameters() != null) {
// Remove the type parameter - this works in conjunction with the CustomTypeProcessor which replaces
// field references
bean.getTypeParameters().clear();
}
});
return model;
}));
}
use of cz.habarta.typescript.generator.emitter.TsPropertyModel in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method createConstructor.
private static Optional<TsConstructorModel> createConstructor(TsBeanModel bean, TsModel model, Map<String, TsConstructorModel> generatedConstructors) {
List<TsParameterModel> parameters = new ArrayList<>();
List<TsParameterModel> optionalParameters = new ArrayList<>();
List<TsStatement> body = new ArrayList<>();
TsType parent = bean.getParent();
if (parent != null) {
if (!(parent instanceof TsType.ReferenceType)) {
throw new IllegalStateException("Generating constructor for non-reference parent types is not currently supported");
}
TsType.ReferenceType referenceParent = (TsType.ReferenceType) parent;
TsConstructorModel parentConstructor = generatedConstructors.get(referenceParent.symbol.getFullName());
if (parentConstructor == null) {
throw new IllegalStateException("Generating constructor for class with non-generated constructor is not currently supported");
}
List<TsParameterModel> parentParameters = parentConstructor.getParameters();
TsIdentifierReference[] callParameters = new TsIdentifierReference[parentParameters.size()];
int i = 0;
for (TsParameterModel parentParameter : parentParameters) {
List<TsParameterModel> targetParameterList = parentParameter.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
targetParameterList.add(parentParameter);
callParameters[i] = new TsIdentifierReference(parentParameter.name);
i++;
}
body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression(), callParameters)));
}
for (TsPropertyModel property : bean.getProperties()) {
if (!property.modifiers.isReadonly) {
continue;
}
TsExpression assignmentExpression;
Optional<TsExpression> predefinedValue = getPredefinedValueForProperty(property, model);
if (predefinedValue.isPresent()) {
assignmentExpression = predefinedValue.get();
} else {
TsParameterModel parameter = new TsParameterModel(property.name, property.tsType);
List<TsParameterModel> targetParameterList = property.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
targetParameterList.add(parameter);
assignmentExpression = new TsIdentifierReference(property.name);
}
TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, assignmentExpression);
TsExpressionStatement assignmentStatement = new TsExpressionStatement(assignment);
body.add(assignmentStatement);
}
parameters.addAll(optionalParameters);
if (parameters.isEmpty() && body.isEmpty()) {
return Optional.empty();
}
TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, parameters, body, null);
return Optional.of(constructor);
}
use of cz.habarta.typescript.generator.emitter.TsPropertyModel in project typescript-generator by vojtechhabarta.
the class BeanPropertyPathExtension method writeBeanAndParentsFieldSpecs.
/**
* Emits a bean and its parent beans before if needed.
* Returns the list of beans that were emitted.
*/
private static Set<TsBeanModel> writeBeanAndParentsFieldSpecs(Writer writer, Settings settings, TsModel model, Set<TsBeanModel> emittedSoFar, TsBeanModel bean) {
if (emittedSoFar.contains(bean)) {
return new HashSet<>();
}
final TsBeanModel parentBean = getBeanModelByType(model, bean.getParent());
final Set<TsBeanModel> emittedBeans = parentBean != null ? writeBeanAndParentsFieldSpecs(writer, settings, model, emittedSoFar, parentBean) : new HashSet<TsBeanModel>();
final String parentClassName = parentBean != null ? getBeanModelClassName(parentBean) + "Fields" : "Fields";
writer.writeIndentedLine("");
writer.writeIndentedLine("class " + getBeanModelClassName(bean) + "Fields extends " + parentClassName + " {");
writer.writeIndentedLine(settings.indentString + "constructor(parent?: Fields, name?: string) { super(parent, name); }");
for (TsPropertyModel property : bean.getProperties()) {
writeBeanProperty(writer, settings, model, bean, property);
}
writer.writeIndentedLine("}");
emittedBeans.add(bean);
return emittedBeans;
}
use of cz.habarta.typescript.generator.emitter.TsPropertyModel in project typescript-generator by vojtechhabarta.
the class ModelCompiler method removeInheritedProperties.
private TsModel removeInheritedProperties(SymbolTable symbolTable, TsModel tsModel) {
final List<TsBeanModel> beans = new ArrayList<>();
for (TsBeanModel bean : tsModel.getBeans()) {
final Map<String, TsType> inheritedPropertyTypes = getInheritedProperties(symbolTable, tsModel, bean.getAllParents());
final List<TsPropertyModel> properties = new ArrayList<>();
for (TsPropertyModel property : bean.getProperties()) {
if (property.isOwnProperty() || !Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
properties.add(property);
}
}
beans.add(bean.withProperties(properties));
}
return tsModel.withBeans(beans);
}
use of cz.habarta.typescript.generator.emitter.TsPropertyModel in project typescript-generator by vojtechhabarta.
the class ModelCompiler method getImplementedProperties.
private static List<TsPropertyModel> getImplementedProperties(SymbolTable symbolTable, TsModel tsModel, List<TsType> interfaces) {
final List<TsPropertyModel> properties = new ArrayList<>();
for (TsType aInterface : interfaces) {
final TsBeanModel bean = tsModel.getBean(getOriginClass(symbolTable, aInterface));
if (bean != null) {
properties.addAll(getImplementedProperties(symbolTable, tsModel, bean.getExtendsList()));
properties.addAll(bean.getProperties());
}
}
return properties;
}
Aggregations