Search in sources :

Example 1 with TsModifierFlags

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

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

the class ModelCompiler method processProperty.

private TsPropertyModel processProperty(SymbolTable symbolTable, BeanModel bean, PropertyModel property, String prefix, String suffix) {
    final TsType type = typeFromJava(symbolTable, property.getType(), property.getContext(), property.getName(), bean.getOrigin());
    final TsType tsType = property.isOptional() ? type.optional() : type;
    final TsModifierFlags modifiers = TsModifierFlags.None.setReadonly(settings.declarePropertiesAsReadOnly);
    final List<String> comments = settings.generateReadonlyAndWriteonlyJSDocTags ? Utils.concat(property.getComments(), getPropertyAccessComments(property.getAccess())) : property.getComments();
    return new TsPropertyModel(prefix + property.getName() + suffix, tsType, modifiers, /*ownProperty*/
    false, comments);
}
Also used : TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags)

Aggregations

TsType (cz.habarta.typescript.generator.TsType)2 TsModifierFlags (cz.habarta.typescript.generator.emitter.TsModifierFlags)2 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)2 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1