Search in sources :

Example 1 with TsExpression

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

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

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

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

the class OnePossiblePropertyValueAssigningExtension method findOnlyValueForEnumReferenceType.

private static Optional<TsExpression> findOnlyValueForEnumReferenceType(TsModel model, TsType.EnumReferenceType propertyType) {
    Symbol symbol = propertyType.symbol;
    Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
    if (!enumModelOption.isPresent()) {
        return Optional.empty();
    }
    TsEnumModel enumModel = enumModelOption.get();
    if (enumModel.getMembers().size() != 1) {
        return Optional.empty();
    }
    EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
    Object enumValue = singleElement.getEnumValue();
    TsStringLiteral expression = new TsStringLiteral((String) enumValue);
    return Optional.of(expression);
}
Also used : TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) ArrayList(java.util.ArrayList) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) Extension(cz.habarta.typescript.generator.Extension) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Collection(java.util.Collection) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Collectors(java.util.stream.Collectors) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) List(java.util.List) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) Optional(java.util.Optional) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Collections(java.util.Collections) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 5 with TsExpression

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

the class RequiredPropertyConstructorExtension method getPredefinedValueForProperty.

private static Optional<TsExpression> getPredefinedValueForProperty(TsPropertyModel property, TsModel model) {
    if (property.tsType instanceof TsType.UnionType) {
        List<TsType> unionTypeElements = ((TsType.UnionType) property.tsType).types;
        if (unionTypeElements.size() != 1) {
            return Optional.empty();
        }
        TsType onlyElement = unionTypeElements.iterator().next();
        if (!(onlyElement instanceof TsType.StringLiteralType)) {
            return Optional.empty();
        }
        TsType.StringLiteralType onlyValue = (TsType.StringLiteralType) onlyElement;
        TsStringLiteral expression = new TsStringLiteral(onlyValue.literal);
        return Optional.of(expression);
    }
    if (property.tsType instanceof TsType.EnumReferenceType) {
        Symbol symbol = ((TsType.EnumReferenceType) property.tsType).symbol;
        Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
        if (!enumModelOption.isPresent()) {
            return Optional.empty();
        }
        TsEnumModel enumModel = enumModelOption.get();
        if (enumModel.getMembers().size() != 1) {
            return Optional.empty();
        }
        EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
        Object enumValue = singleElement.getEnumValue();
        TsStringLiteral expression = new TsStringLiteral((String) enumValue);
        return Optional.of(expression);
    }
    return Optional.empty();
}
Also used : TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) Arrays(java.util.Arrays) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) HashMap(java.util.HashMap) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) ArrayList(java.util.ArrayList) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) Extension(cz.habarta.typescript.generator.Extension) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Map(java.util.Map) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) List(java.util.List) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) Optional(java.util.Optional) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) Pattern(java.util.regex.Pattern) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral)

Aggregations

TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)9 ArrayList (java.util.ArrayList)7 TsType (cz.habarta.typescript.generator.TsType)6 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)6 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)6 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)5 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)5 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)5 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)4 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)4 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)4 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)4 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)4 Symbol (cz.habarta.typescript.generator.compiler.Symbol)3 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)3 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)3 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)3 Extension (cz.habarta.typescript.generator.Extension)2 TsParameter (cz.habarta.typescript.generator.TsParameter)2 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)2