Search in sources :

Example 1 with TsReturnStatement

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

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

the class ModelCompiler method processRestMethod.

private TsMethodModel processRestMethod(TsModel tsModel, SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, RestMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
    final String path = Utils.joinPath(pathPrefix, method.getPath());
    final PathTemplate pathTemplate = PathTemplate.parse(path);
    final List<String> comments = Utils.concat(method.getComments(), Arrays.asList("HTTP " + method.getHttpMethod() + " /" + path, "Java method: " + method.getOriginClass().getName() + "." + method.getName()));
    final List<TsParameterModel> parameters = new ArrayList<>();
    // path params
    for (MethodParameterModel parameter : method.getPathParams()) {
        parameters.add(processParameter(symbolTable, method, parameter));
    }
    // entity param
    if (method.getEntityParam() != null) {
        parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
    }
    // query params
    final List<RestQueryParam> queryParams = method.getQueryParams();
    final TsParameterModel queryParameter;
    if (queryParams != null && !queryParams.isEmpty()) {
        final List<TsType> types = new ArrayList<>();
        if (queryParams.stream().anyMatch(param -> param instanceof RestQueryParam.Map)) {
            types.add(new TsType.IndexedArrayType(TsType.String, TsType.Any));
        } else {
            final List<TsProperty> currentSingles = new ArrayList<>();
            final Runnable flushSingles = () -> {
                if (!currentSingles.isEmpty()) {
                    types.add(new TsType.ObjectType(currentSingles));
                    currentSingles.clear();
                }
            };
            for (RestQueryParam restQueryParam : queryParams) {
                if (restQueryParam instanceof RestQueryParam.Single) {
                    final MethodParameterModel queryParam = ((RestQueryParam.Single) restQueryParam).getQueryParam();
                    final TsType type = typeFromJava(symbolTable, queryParam.getType(), method.getName(), method.getOriginClass());
                    currentSingles.add(new TsProperty(queryParam.getName(), restQueryParam.required ? type : new TsType.OptionalType(type)));
                }
                if (restQueryParam instanceof RestQueryParam.Bean) {
                    final BeanModel queryBean = ((RestQueryParam.Bean) restQueryParam).getBean();
                    flushSingles.run();
                    final Symbol queryParamsSymbol = symbolTable.getSymbol(queryBean.getOrigin(), "QueryParams");
                    if (tsModel.getBean(queryParamsSymbol) == null) {
                        tsModel.getBeans().add(new TsBeanModel(queryBean.getOrigin(), TsBeanCategory.Data, /*isClass*/
                        false, queryParamsSymbol, /*typeParameters*/
                        null, /*parent*/
                        null, /*extendsList*/
                        null, /*implementsList*/
                        null, processProperties(symbolTable, null, queryBean), /*constructor*/
                        null, /*methods*/
                        null, /*comments*/
                        null));
                    }
                    types.add(new TsType.ReferenceType(queryParamsSymbol));
                }
            }
            flushSingles.run();
        }
        boolean allQueryParamsOptional = queryParams.stream().noneMatch(queryParam -> queryParam.required);
        TsType.IntersectionType queryParamType = new TsType.IntersectionType(types);
        queryParameter = new TsParameterModel("queryParams", allQueryParamsOptional ? new TsType.OptionalType(queryParamType) : queryParamType);
        parameters.add(queryParameter);
    } else {
        queryParameter = null;
    }
    if (optionsType != null) {
        final TsParameterModel optionsParameter = new TsParameterModel("options", new TsType.OptionalType(optionsType));
        parameters.add(optionsParameter);
    }
    // return type
    final TsType returnType = typeFromJava(symbolTable, method.getReturnType(), method.getName(), method.getOriginClass());
    final TsType wrappedReturnType = new TsType.GenericReferenceType(responseSymbol, returnType);
    // method name
    final String nameSuffix;
    if (createLongName) {
        nameSuffix = "$" + method.getHttpMethod() + "$" + pathTemplate.format("", "", false).replaceAll("/", "_").replaceAll("\\W", "");
    } else {
        nameSuffix = "";
    }
    // implementation
    final List<TsStatement> body;
    if (implement) {
        body = new ArrayList<>();
        body.add(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsMemberExpression(new TsThisExpression(), "httpClient"), "request"), new TsObjectLiteral(new TsPropertyDefinition("method", new TsStringLiteral(method.getHttpMethod())), new TsPropertyDefinition("url", processPathTemplate(pathTemplate)), queryParameter != null ? new TsPropertyDefinition("queryParams", new TsIdentifierReference("queryParams")) : null, method.getEntityParam() != null ? new TsPropertyDefinition("data", new TsIdentifierReference(method.getEntityParam().getName())) : null, optionsType != null ? new TsPropertyDefinition("options", new TsIdentifierReference("options")) : null))));
    } else {
        body = null;
    }
    // method
    final TsMethodModel tsMethodModel = new TsMethodModel(method.getName() + nameSuffix, TsModifierFlags.None, null, parameters, wrappedReturnType, body, comments);
    return tsMethodModel;
}
Also used : TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) ArrayList(java.util.ArrayList) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) 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) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 3 with TsReturnStatement

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

use of cz.habarta.typescript.generator.emitter.TsReturnStatement 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 5 with TsReturnStatement

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

the class JsonDeserializationExtension method addCopyFnToJaxrsMethod.

private static TsMethodModel addCopyFnToJaxrsMethod(SymbolTable symbolTable, TsModel tsModel, TsMethodModel method) {
    final TsType returnType = method.getReturnType();
    if (!(returnType instanceof TsType.GenericReferenceType))
        return null;
    final TsType.GenericReferenceType genericReferenceReturnType = (TsType.GenericReferenceType) returnType;
    if (genericReferenceReturnType.symbol != symbolTable.getSyntheticSymbol("RestResponse"))
        return null;
    final List<TsType> typeArguments = genericReferenceReturnType.typeArguments;
    if (typeArguments == null || typeArguments.size() != 1)
        return null;
    final TsType returnDataType = typeArguments.get(0);
    final List<TsStatement> body = method.getBody();
    if (body == null || body.size() != 1)
        return null;
    final TsStatement statement = body.get(0);
    if (!(statement instanceof TsReturnStatement))
        return null;
    final TsReturnStatement returnStatement = (TsReturnStatement) statement;
    final TsExpression returnExpression = returnStatement.getExpression();
    if (returnExpression == null)
        return null;
    if (!(returnExpression instanceof TsCallExpression))
        return null;
    final TsCallExpression callExpression = (TsCallExpression) returnExpression;
    final List<TsExpression> arguments = callExpression.getArguments();
    if (arguments == null || arguments.isEmpty())
        return null;
    final TsExpression firstArgument = arguments.get(0);
    if (!(firstArgument instanceof TsObjectLiteral))
        return null;
    final TsObjectLiteral objectLiteral = (TsObjectLiteral) firstArgument;
    // todo create changed method instead of modifying existing
    final int index = Math.max(objectLiteral.getPropertyDefinitions().size() - 1, 0);
    final TsExpression copyFunction = returnDataType == TsType.Void ? TsIdentifierReference.Undefined : getCopyFunctionForTsType(symbolTable, tsModel, returnDataType);
    objectLiteral.getPropertyDefinitions().add(index, new TsPropertyDefinition("copyFn", copyFunction));
    return method;
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition)

Aggregations

TsType (cz.habarta.typescript.generator.TsType)5 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)5 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)5 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)5 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)4 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)4 ArrayList (java.util.ArrayList)4 TsTypeReferenceExpression (cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression)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 TsObjectLiteral (cz.habarta.typescript.generator.emitter.TsObjectLiteral)2 TsPropertyDefinition (cz.habarta.typescript.generator.emitter.TsPropertyDefinition)2 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)2 TsParameter (cz.habarta.typescript.generator.TsParameter)1 TsProperty (cz.habarta.typescript.generator.TsProperty)1 TsArrowFunction (cz.habarta.typescript.generator.emitter.TsArrowFunction)1 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)1