Search in sources :

Example 1 with TsBinaryExpression

use of cz.habarta.typescript.generator.emitter.TsBinaryExpression 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);
}
Also used : TsBinaryExpression(cz.habarta.typescript.generator.emitter.TsBinaryExpression) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsNewExpression(cz.habarta.typescript.generator.emitter.TsNewExpression) TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsVariableDeclarationStatement(cz.habarta.typescript.generator.emitter.TsVariableDeclarationStatement) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Aggregations

TsType (cz.habarta.typescript.generator.TsType)1 Symbol (cz.habarta.typescript.generator.compiler.Symbol)1 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)1 TsBinaryExpression (cz.habarta.typescript.generator.emitter.TsBinaryExpression)1 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)1 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)1 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)1 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)1 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)1 TsNewExpression (cz.habarta.typescript.generator.emitter.TsNewExpression)1 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)1 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)1 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)1 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)1 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)1 TsTypeReferenceExpression (cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression)1 TsVariableDeclarationStatement (cz.habarta.typescript.generator.emitter.TsVariableDeclarationStatement)1 ArrayList (java.util.ArrayList)1