Search in sources :

Example 11 with TsPropertyModel

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

the class ModelCompiler method addImplementedProperties.

private TsModel addImplementedProperties(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        if (bean.isClass()) {
            final List<TsPropertyModel> resultProperties = new ArrayList<>(bean.getProperties());
            final Set<String> classPropertyNames = new LinkedHashSet<>();
            for (TsPropertyModel property : bean.getProperties()) {
                classPropertyNames.add(property.getName());
            }
            classPropertyNames.addAll(getInheritedProperties(symbolTable, tsModel, bean.getExtendsList()).keySet());
            final List<TsPropertyModel> implementedProperties = getImplementedProperties(symbolTable, tsModel, bean.getImplementsList());
            Collections.reverse(implementedProperties);
            for (TsPropertyModel implementedProperty : implementedProperties) {
                if (!classPropertyNames.contains(implementedProperty.getName())) {
                    resultProperties.add(0, implementedProperty);
                    classPropertyNames.add(implementedProperty.getName());
                }
            }
            beans.add(bean.withProperties(resultProperties));
        } else {
            beans.add(bean);
        }
    }
    return tsModel.withBeans(beans);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 12 with TsPropertyModel

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

the class ModelCompiler method processBean.

private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Type, List<BeanModel>> children, BeanModel bean) {
    final boolean isClass = mappedToClass(bean.getOrigin());
    final List<TsType> extendsList = new ArrayList<>();
    final List<TsType> implementsList = new ArrayList<>();
    final TsType parentType = typeFromJava(symbolTable, bean.getParent());
    if (parentType != null && !parentType.equals(TsType.Any)) {
        final boolean isParentMappedToClass = mappedToClass(getOriginClass(symbolTable, parentType));
        if (isClass && !isParentMappedToClass) {
            implementsList.add(parentType);
        } else {
            extendsList.add(parentType);
        }
    }
    final List<TsType> interfaces = new ArrayList<>();
    for (Type aInterface : bean.getInterfaces()) {
        final TsType interfaceType = typeFromJava(symbolTable, aInterface);
        if (!interfaceType.equals(TsType.Any)) {
            interfaces.add(interfaceType);
        }
    }
    if (isClass) {
        implementsList.addAll(interfaces);
    } else {
        extendsList.addAll(interfaces);
    }
    final List<TsPropertyModel> properties = processProperties(symbolTable, model, bean);
    boolean isTaggedUnion = false;
    if (bean.getDiscriminantProperty() != null && bean.getProperty(bean.getDiscriminantProperty()) == null) {
        isTaggedUnion = true;
        boolean isDisciminantProperty = true;
        final List<BeanModel> selfAndDescendants = getSelfAndDescendants(bean, children);
        final List<TsType.StringLiteralType> literals = new ArrayList<>();
        for (BeanModel descendant : selfAndDescendants) {
            if (descendant.getDiscriminantProperty() == null || descendant.getProperty(bean.getDiscriminantProperty()) != null) {
                // do not handle bean as tagged union if any descendant or it itself has duplicate discriminant property
                isTaggedUnion = false;
                isDisciminantProperty = false;
            }
            if (descendant.getDiscriminantLiteral() != null) {
                literals.add(new TsType.StringLiteralType(descendant.getDiscriminantLiteral()));
            }
        }
        final List<BeanModel> descendants = selfAndDescendants.subList(1, selfAndDescendants.size());
        for (BeanModel descendant : descendants) {
            // do not handle bean as tagged union if any descendant has "non-related" generic parameter
            final List<String> mappedGenericVariables = GenericsResolver.mapGenericVariablesToBase(descendant.getOrigin(), bean.getOrigin());
            if (mappedGenericVariables.contains(null)) {
                isTaggedUnion = false;
            }
        }
        final TsType discriminantType = isDisciminantProperty && !literals.isEmpty() ? new TsType.UnionType(literals) : TsType.String;
        final TsModifierFlags modifiers = TsModifierFlags.None.setReadonly(settings.declarePropertiesAsReadOnly);
        properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, modifiers, /*ownProperty*/
        true, null));
    }
    final TsBeanModel tsBean = new TsBeanModel(bean.getOrigin(), TsBeanCategory.Data, isClass, symbolTable.getSymbol(bean.getOrigin()), getTypeParameters(bean.getOrigin()), parentType, extendsList, implementsList, properties, /*constructor*/
    null, /*methods*/
    null, bean.getComments());
    return isTaggedUnion ? tsBean.withTaggedUnion(bean.getTaggedUnionClasses(), bean.getDiscriminantProperty(), bean.getDiscriminantLiteral()) : tsBean;
}
Also used : TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) Type(java.lang.reflect.Type) TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags)

Example 13 with TsPropertyModel

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

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

the class DefaultsFromInstanceExtension method transformBean.

protected TsBeanModel transformBean(TsModelTransformer.Context context, TsBeanModel tsBean) {
    if (!tsBean.isClass()) {
        return tsBean;
    }
    final BeanModel bean = context.getBeanModelOrigin(tsBean);
    if (bean == null) {
        return tsBean;
    }
    final Class<?> originClass = bean.getOrigin();
    if (originClass == null) {
        return tsBean;
    }
    try {
        final Constructor<?> constructor = originClass.getConstructor();
        final Object instance = constructor.newInstance();
        final List<TsPropertyModel> properties = tsBean.getProperties().stream().map(tsProperty -> withDefaultValue(bean, instance, tsProperty)).collect(Collectors.toList());
        return tsBean.withProperties(properties);
    } catch (Exception e) {
        TypeScriptGenerator.getLogger().verbose(String.format("Cannot create instance of class '%s' to get default values: %s", originClass.getName(), e.getMessage()));
        return tsBean;
    }
}
Also used : Arrays(java.util.Arrays) PropertyModel(cz.habarta.typescript.generator.parser.PropertyModel) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) Member(java.lang.reflect.Member) Field(java.lang.reflect.Field) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsNumberLiteral(cz.habarta.typescript.generator.emitter.TsNumberLiteral) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) TypeScriptGenerator(cz.habarta.typescript.generator.TypeScriptGenerator) List(java.util.List) Extension(cz.habarta.typescript.generator.Extension) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) TsModelTransformer(cz.habarta.typescript.generator.compiler.TsModelTransformer) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Method(java.lang.reflect.Method) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel)

Example 15 with TsPropertyModel

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

Aggregations

TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)17 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)13 ArrayList (java.util.ArrayList)12 TsType (cz.habarta.typescript.generator.TsType)11 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)5 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)5 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)4 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)4 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)4 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)4 Symbol (cz.habarta.typescript.generator.compiler.Symbol)3 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)3 TsModel (cz.habarta.typescript.generator.emitter.TsModel)3 TsModifierFlags (cz.habarta.typescript.generator.emitter.TsModifierFlags)3 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)3 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)3 List (java.util.List)3