Search in sources :

Example 1 with TsType

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

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

the class ModelCompiler method transformEnumsToUnions.

private TsModel transformEnumsToUnions(TsModel tsModel) {
    final List<TsEnumModel> stringEnums = tsModel.getEnums(EnumKind.StringBased);
    final LinkedHashSet<TsAliasModel> typeAliases = new LinkedHashSet<>(tsModel.getTypeAliases());
    for (TsEnumModel enumModel : stringEnums) {
        final List<TsType> values = new ArrayList<>();
        for (EnumMemberModel member : enumModel.getMembers()) {
            values.add(member.getEnumValue() instanceof Number ? new TsType.NumberLiteralType((Number) member.getEnumValue()) : new TsType.StringLiteralType(String.valueOf(member.getEnumValue())));
        }
        final TsType union = new TsType.UnionType(values);
        typeAliases.add(new TsAliasModel(enumModel.getOrigin(), enumModel.getName(), null, union, enumModel.getComments()));
    }
    return tsModel.withRemovedEnums(stringEnums).withTypeAliases(new ArrayList<>(typeAliases));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsType(cz.habarta.typescript.generator.TsType) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 3 with TsType

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

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

the class BeanPropertyPathExtension method getBeanModelByType.

private static TsBeanModel getBeanModelByType(TsModel model, TsType type) {
    TsType originalType = extractOriginalTsType(type);
    if (!(originalType instanceof TsType.ReferenceType)) {
        return null;
    }
    TsType.ReferenceType originalTypeBean = (TsType.ReferenceType) originalType;
    for (TsBeanModel curBean : model.getBeans()) {
        if (curBean.getName().equals(originalTypeBean.symbol)) {
            return curBean;
        }
    }
    return null;
}
Also used : TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 5 with TsType

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

Aggregations

TsType (cz.habarta.typescript.generator.TsType)25 ArrayList (java.util.ArrayList)16 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)12 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)10 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)9 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)8 TsAliasModel (cz.habarta.typescript.generator.emitter.TsAliasModel)7 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)7 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)7 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)7 TsModel (cz.habarta.typescript.generator.emitter.TsModel)6 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)5 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)5 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)5 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)5 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)5 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)5 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)5 LinkedHashSet (java.util.LinkedHashSet)5 TsModifierFlags (cz.habarta.typescript.generator.emitter.TsModifierFlags)4