Search in sources :

Example 1 with TsAssignmentExpression

use of cz.habarta.typescript.generator.emitter.TsAssignmentExpression 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);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 2 with TsAssignmentExpression

use of cz.habarta.typescript.generator.emitter.TsAssignmentExpression 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);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 3 with TsAssignmentExpression

use of cz.habarta.typescript.generator.emitter.TsAssignmentExpression 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)

Example 4 with TsAssignmentExpression

use of cz.habarta.typescript.generator.emitter.TsAssignmentExpression in project typescript-generator by vojtechhabarta.

the class OnePossiblePropertyValueAssigningExtension method createValueAssignmentStatement.

private static TsExpressionStatement createValueAssignmentStatement(TsPropertyModel property, TsExpression value) {
    TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
    TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, value);
    return new TsExpressionStatement(assignment);
}
Also used : TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression)

Aggregations

TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)4 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsType (cz.habarta.typescript.generator.TsType)3 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)3 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)3 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)3 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)3 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)3 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)3 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)3 ArrayList (java.util.ArrayList)3 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)2 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)2 Symbol (cz.habarta.typescript.generator.compiler.Symbol)1 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 TsBinaryExpression (cz.habarta.typescript.generator.emitter.TsBinaryExpression)1 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)1 TsNewExpression (cz.habarta.typescript.generator.emitter.TsNewExpression)1 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)1