use of cz.habarta.typescript.generator.emitter.TsSuperExpression 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.TsSuperExpression in project typescript-generator by vojtechhabarta.
the class ModelCompiler method addConstructors.
private TsModel addConstructors(SymbolTable symbolTable, TsModel tsModel) {
final List<TsBeanModel> beans = new ArrayList<>();
for (TsBeanModel bean : tsModel.getBeans()) {
final Symbol beanIdentifier = symbolTable.getSymbol(bean.getOrigin());
final List<TsType.GenericVariableType> typeParameters = getTypeParameters(bean.getOrigin());
final TsType.ReferenceType dataType = typeParameters.isEmpty() ? new TsType.ReferenceType(beanIdentifier) : new TsType.GenericReferenceType(beanIdentifier, typeParameters);
final List<TsStatement> body = new ArrayList<>();
if (bean.getParent() != null) {
body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression(), new TsIdentifierReference("data"))));
}
for (TsPropertyModel property : bean.getProperties()) {
final Map<String, TsType> inheritedProperties = ModelCompiler.getInheritedProperties(symbolTable, tsModel, Utils.listFromNullable(bean.getParent()));
if (!inheritedProperties.containsKey(property.getName())) {
body.add(new TsExpressionStatement(new TsAssignmentExpression(new TsMemberExpression(new TsThisExpression(), property.name), new TsMemberExpression(new TsIdentifierReference("data"), property.name))));
}
}
if (bean.isClass()) {
final TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, Arrays.asList(new TsParameterModel("data", dataType)), body, /*comments*/
null);
beans.add(bean.withConstructor(constructor));
} else {
beans.add(bean);
}
}
return tsModel.withBeans(beans);
}
use of cz.habarta.typescript.generator.emitter.TsSuperExpression in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method createDeserializationMethod.
private static TsMethodModel createDeserializationMethod(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
final Symbol beanIdentifier = symbolTable.getSymbol(bean.getOrigin());
List<TsType.GenericVariableType> typeParameters = getTypeParameters(bean.getOrigin());
final TsType.ReferenceType dataType = typeParameters.isEmpty() ? new TsType.ReferenceType(beanIdentifier) : new TsType.GenericReferenceType(beanIdentifier, typeParameters);
final List<TsParameterModel> parameters = new ArrayList<>();
parameters.add(new TsParameterModel("data", dataType));
parameters.addAll(getConstructorFnOfParameters(typeParameters));
parameters.add(new TsParameterModel("target", dataType.optional()));
final List<TsStatement> body = new ArrayList<>();
body.add(ifUndefinedThenReturnItStatement("data"));
body.add(new TsVariableDeclarationStatement(/*const*/
true, "instance", /*type*/
null, new TsBinaryExpression(new TsIdentifierReference("target"), TsBinaryOperator.BarBar, new TsNewExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(beanIdentifier)), typeParameters, getConstructorParameters(bean)))));
if (bean.getParent() != null) {
body.add(new TsExpressionStatement(new TsCallExpression(new TsMemberExpression(new TsSuperExpression(), "fromData"), new TsIdentifierReference("data"), new TsIdentifierReference("instance"))));
}
for (TsPropertyModel property : bean.getProperties()) {
final Map<String, TsType> inheritedProperties = ModelCompiler.getInheritedProperties(symbolTable, tsModel, Utils.listFromNullable(bean.getParent()));
if (!inheritedProperties.containsKey(property.getName())) {
body.add(new TsExpressionStatement(new TsAssignmentExpression(new TsMemberExpression(new TsIdentifierReference("instance"), property.name), getPropertyCopy(symbolTable, tsModel, bean, property))));
}
}
body.add(new TsReturnStatement(new TsIdentifierReference("instance")));
return new TsMethodModel("fromData", TsModifierFlags.None.setStatic(), typeParameters, parameters, dataType, body, null);
}
use of cz.habarta.typescript.generator.emitter.TsSuperExpression in project typescript-generator by vojtechhabarta.
the class OnePossiblePropertyValueAssigningExtension method createConstructor.
private static TsConstructorModel createConstructor(TsBeanModel bean, Collection<TsExpressionStatement> valueAssignmentStatements) {
List<TsStatement> body = new ArrayList<>();
if (bean.getParent() != null) {
body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression())));
}
body.addAll(valueAssignmentStatements);
return new TsConstructorModel(TsModifierFlags.None, Collections.emptyList(), body, null);
}
Aggregations