use of cz.habarta.typescript.generator.emitter.TsNewExpression 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);
}
Aggregations