Search in sources :

Example 1 with TsThisExpression

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

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

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

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

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