Search in sources :

Example 1 with TsMethodModel

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

the class ModelCompiler method createRestClients.

private void createRestClients(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
    final Symbol httpClientSymbol = symbolTable.getSyntheticSymbol("HttpClient");
    final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
    // HttpClient interface
    final TsType.GenericVariableType returnGenericVariable = new TsType.GenericVariableType("R");
    tsModel.getBeans().add(new TsBeanModel(null, TsBeanCategory.ServicePrerequisite, false, httpClientSymbol, typeParameters, null, null, null, null, null, Arrays.asList(new TsMethodModel("request", TsModifierFlags.None, Arrays.asList(returnGenericVariable), Arrays.asList(new TsParameterModel("requestConfig", new TsType.ObjectType(new TsProperty("method", TsType.String), new TsProperty("url", TsType.String), new TsProperty("queryParams", new TsType.OptionalType(TsType.Any)), new TsProperty("data", new TsType.OptionalType(TsType.Any)), new TsProperty("copyFn", new TsType.OptionalType(new TsType.FunctionType(Arrays.asList(new TsParameter("data", returnGenericVariable)), returnGenericVariable))), optionsType != null ? new TsProperty("options", new TsType.OptionalType(optionsType)) : null))), new TsType.GenericReferenceType(responseSymbol, returnGenericVariable), null, null)), null));
    // application client classes
    final TsType.ReferenceType httpClientType = optionsGenericVariable != null ? new TsType.GenericReferenceType(httpClientSymbol, optionsGenericVariable) : new TsType.ReferenceType(httpClientSymbol);
    final TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, Arrays.asList(new TsParameterModel(TsAccessibilityModifier.Protected, "httpClient", httpClientType)), Collections.<TsStatement>emptyList(), null);
    final boolean bothInterfacesAndClients = settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
    final String groupingSuffix = bothInterfacesAndClients ? null : "Client";
    final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, groupingSuffix, responseSymbol, optionsType, true);
    for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
        final Symbol symbol = bothInterfacesAndClients ? symbolTable.addSuffixToSymbol(entry.getKey(), "Client") : entry.getKey();
        final TsType interfaceType = bothInterfacesAndClients ? new TsType.ReferenceType(entry.getKey()) : null;
        final TsBeanModel clientModel = new TsBeanModel(null, TsBeanCategory.Service, true, symbol, typeParameters, null, null, Utils.listFromNullable(interfaceType), null, constructor, entry.getValue(), null);
        tsModel.getBeans().add(clientModel);
    }
    // helper
    tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/uriEncoding.ts"));
}
Also used : TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsParameter(cz.habarta.typescript.generator.TsParameter) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) List(java.util.List) ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 2 with TsMethodModel

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

the class JsonDeserializationExtension method useDeserializationMethodsInJaxrs.

private TsModel useDeserializationMethodsInJaxrs(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        if (bean.isJaxrsApplicationClientBean()) {
            final List<TsMethodModel> methods = new ArrayList<>();
            for (TsMethodModel method : bean.getMethods()) {
                final TsMethodModel changedMethod = addCopyFnToJaxrsMethod(symbolTable, tsModel, method);
                methods.add(changedMethod != null ? changedMethod : method);
            }
            beans.add(bean.withMethods(methods));
        } else {
            beans.add(bean);
        }
    }
    return tsModel.withBeans(beans);
}
Also used : ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel)

Example 3 with TsMethodModel

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

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

the class JsonDeserializationExtension method createDeserializationMethods.

private static TsModel createDeserializationMethods(SymbolTable symbolTable, TsModel tsModel) {
    tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/jsonDeserialization.ts"));
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        if (bean.isDataClass()) {
            final List<TsMethodModel> methods = new ArrayList<>(bean.getMethods());
            final TsMethodModel deserializationMethod = createDeserializationMethod(symbolTable, tsModel, bean);
            methods.add(0, deserializationMethod);
            if (!bean.getTypeParameters().isEmpty()) {
                final TsMethodModel genericFunctionConstructor = createDeserializationGenericFunctionConstructor(symbolTable, tsModel, bean);
                methods.add(0, genericFunctionConstructor);
            }
            if (bean.getTaggedUnionAlias() != null) {
                final TsMethodModel unionDeserializationMethod = createDeserializationMethodForTaggedUnion(symbolTable, tsModel, bean);
                methods.add(1, unionDeserializationMethod);
            }
            beans.add(bean.withMethods(methods));
        } else {
            beans.add(bean);
        }
    }
    return tsModel.withBeans(beans);
}
Also used : ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel)

Example 5 with TsMethodModel

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

the class ModelCompiler method transformBeanPropertyTypes.

private static TsModel transformBeanPropertyTypes(TsModel tsModel, TsType.Transformer transformer) {
    final List<TsBeanModel> newBeans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        final TsType.Context context = new TsType.Context();
        final List<TsPropertyModel> newProperties = new ArrayList<>();
        for (TsPropertyModel property : bean.getProperties()) {
            final TsType newType = TsType.transformTsType(context, property.getTsType(), transformer);
            newProperties.add(property.withTsType(newType));
        }
        final List<TsMethodModel> newMethods = new ArrayList<>();
        for (TsMethodModel method : bean.getMethods()) {
            final List<TsParameterModel> newParameters = new ArrayList<>();
            for (TsParameterModel parameter : method.getParameters()) {
                final TsType newParameterType = TsType.transformTsType(context, parameter.getTsType(), transformer);
                newParameters.add(new TsParameterModel(parameter.getAccessibilityModifier(), parameter.getName(), newParameterType));
            }
            final TsType newReturnType = TsType.transformTsType(context, method.getReturnType(), transformer);
            newMethods.add(new TsMethodModel(method.getName(), method.getModifiers(), method.getTypeParameters(), newParameters, newReturnType, method.getBody(), method.getComments()));
        }
        newBeans.add(bean.withProperties(newProperties).withMethods(newMethods));
    }
    return tsModel.withBeans(newBeans);
}
Also used : ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Aggregations

TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)10 ArrayList (java.util.ArrayList)9 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)7 TsType (cz.habarta.typescript.generator.TsType)6 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)6 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)5 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)4 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)4 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)3 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)3 TsTypeReferenceExpression (cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression)3 TsParameter (cz.habarta.typescript.generator.TsParameter)2 TsProperty (cz.habarta.typescript.generator.TsProperty)2 Symbol (cz.habarta.typescript.generator.compiler.Symbol)2 TsArrowFunction (cz.habarta.typescript.generator.emitter.TsArrowFunction)1 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)1 TsBinaryExpression (cz.habarta.typescript.generator.emitter.TsBinaryExpression)1 TsBooleanLiteral (cz.habarta.typescript.generator.emitter.TsBooleanLiteral)1