Search in sources :

Example 1 with TsTypeReferenceExpression

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

the class JsonDeserializationExtension method createDeserializationGenericFunctionConstructor.

private static TsMethodModel createDeserializationGenericFunctionConstructor(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
    final Symbol beanIdentifier = symbolTable.getSymbol(bean.getOrigin());
    List<TsType.GenericVariableType> typeParameters = getTypeParameters(bean.getOrigin());
    final TsType.ReferenceType dataType = new TsType.GenericReferenceType(beanIdentifier, typeParameters);
    final List<TsParameterModel> constructorFnOfParameters = getConstructorFnOfParameters(typeParameters);
    final List<TsExpression> arguments = new ArrayList<>();
    arguments.add(new TsIdentifierReference("data"));
    for (TsParameterModel constructorFnOfParameter : constructorFnOfParameters) {
        arguments.add(new TsIdentifierReference(constructorFnOfParameter.name));
    }
    final List<TsStatement> body = new ArrayList<>();
    body.add(new TsReturnStatement(new TsArrowFunction(Arrays.asList(new TsParameter("data", null)), new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(beanIdentifier)), "fromData"), null, arguments))));
    return new TsMethodModel("fromDataFn", TsModifierFlags.None.setStatic(), typeParameters, constructorFnOfParameters, new TsType.FunctionType(Arrays.asList(new TsParameter("data", dataType)), dataType), body, null);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) Symbol(cz.habarta.typescript.generator.compiler.Symbol) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsArrowFunction(cz.habarta.typescript.generator.emitter.TsArrowFunction) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsParameter(cz.habarta.typescript.generator.TsParameter) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 2 with TsTypeReferenceExpression

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

the class JsonDeserializationExtension method getCopyFunctionForTsType.

private static TsExpression getCopyFunctionForTsType(SymbolTable symbolTable, TsModel tsModel, TsType tsType) {
    if (tsType instanceof TsType.GenericReferenceType) {
        final TsType.GenericReferenceType genericReferenceType = (TsType.GenericReferenceType) tsType;
        // Class.fromDataFn<T1...>(constructorFnOfT1...)
        final List<TsExpression> arguments = new ArrayList<>();
        for (TsType typeArgument : genericReferenceType.typeArguments) {
            arguments.add(getCopyFunctionForTsType(symbolTable, tsModel, typeArgument));
        }
        return new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(genericReferenceType.symbol)), "fromDataFn"), genericReferenceType.typeArguments, arguments);
    }
    if (tsType instanceof TsType.ReferenceType) {
        final TsType.ReferenceType referenceType = (TsType.ReferenceType) tsType;
        final TsBeanModel referencedBean = tsModel.getBean(symbolTable.getSymbolClass(referenceType.symbol));
        if (referencedBean != null && referencedBean.isClass()) {
            if (referencedBean.getTaggedUnionAlias() != null) {
                // Class.fromDataUnion (tagged union)
                return new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(referencedBean.getName())), "fromDataUnion");
            } else {
                // Class.fromData
                return new TsMemberExpression(new TsTypeReferenceExpression(referenceType), "fromData");
            }
        }
    }
    if (tsType instanceof TsType.BasicArrayType) {
        // __getCopyArrayFn
        final TsType.BasicArrayType arrayType = (TsType.BasicArrayType) tsType;
        return new TsCallExpression(new TsIdentifierReference("__getCopyArrayFn"), getCopyFunctionForTsType(symbolTable, tsModel, arrayType.elementType));
    }
    if (tsType instanceof TsType.IndexedArrayType) {
        // __getCopyObjectFn
        final TsType.IndexedArrayType objectType = (TsType.IndexedArrayType) tsType;
        return new TsCallExpression(new TsIdentifierReference("__getCopyObjectFn"), getCopyFunctionForTsType(symbolTable, tsModel, objectType.elementType));
    }
    if (tsType instanceof TsType.GenericVariableType) {
        // constructorFnOfT
        final TsType.GenericVariableType genericVariableType = (TsType.GenericVariableType) tsType;
        return new TsIdentifierReference("constructorFnOf" + genericVariableType.name);
    }
    // __identity
    return new TsCallExpression(new TsIdentifierReference("__identity"), Arrays.asList(tsType), Collections.<TsExpression>emptyList());
}
Also used : TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 3 with TsTypeReferenceExpression

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

the class JsonDeserializationExtension method createDeserializationMethodForTaggedUnion.

private static TsMethodModel createDeserializationMethodForTaggedUnion(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
    final List<TsSwitchCaseClause> caseClauses = new ArrayList<>();
    for (Class<?> cls : bean.getTaggedUnionClasses()) {
        final TsBeanModel tuBean = tsModel.getBean(cls);
        caseClauses.add(new TsSwitchCaseClause(new TsStringLiteral(tuBean.getDiscriminantLiteral()), Arrays.<TsStatement>asList(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(symbolTable.getSymbol(cls))), "fromData"), new TsIdentifierReference("data"))))));
    }
    final List<TsStatement> body = new ArrayList<>();
    body.add(ifUndefinedThenReturnItStatement("data"));
    body.add(new TsSwitchStatement(new TsMemberExpression(new TsIdentifierReference("data"), bean.getDiscriminantProperty()), caseClauses, null));
    final TsType.ReferenceType unionType = new TsType.ReferenceType(bean.getTaggedUnionAlias().getName());
    return new TsMethodModel("fromDataUnion", TsModifierFlags.None.setStatic(), // typeParameters,
    null, Arrays.asList(new TsParameterModel("data", unionType)), unionType, body, null);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsSwitchStatement(cz.habarta.typescript.generator.emitter.TsSwitchStatement) TsSwitchCaseClause(cz.habarta.typescript.generator.emitter.TsSwitchCaseClause) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 4 with TsTypeReferenceExpression

use of cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression 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)4 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsTypeReferenceExpression (cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression)4 ArrayList (java.util.ArrayList)4 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)3 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)3 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)3 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)3 Symbol (cz.habarta.typescript.generator.compiler.Symbol)2 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)2 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)2 TsParameter (cz.habarta.typescript.generator.TsParameter)1 TsArrowFunction (cz.habarta.typescript.generator.emitter.TsArrowFunction)1 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)1 TsBinaryExpression (cz.habarta.typescript.generator.emitter.TsBinaryExpression)1 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)1 TsNewExpression (cz.habarta.typescript.generator.emitter.TsNewExpression)1 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)1