Search in sources :

Example 1 with TsAliasModel

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

the class ModelCompiler method sortTypeDeclarations.

private TsModel sortTypeDeclarations(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = tsModel.getBeans();
    final List<TsAliasModel> aliases = tsModel.getTypeAliases();
    final List<TsEnumModel> enums = tsModel.getEnums();
    if (settings.sortDeclarations || settings.sortTypeDeclarations) {
        Collections.sort(beans);
        Collections.sort(aliases);
        Collections.sort(enums);
    }
    final LinkedHashSet<TsBeanModel> orderedBeans = new LinkedHashSet<>();
    for (TsBeanModel bean : beans) {
        addOrderedClass(symbolTable, tsModel, bean, orderedBeans);
    }
    return tsModel.withBeans(new ArrayList<>(orderedBeans)).withTypeAliases(aliases).withEnums(enums);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 2 with TsAliasModel

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

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

the class ModelCompiler method inlineEnums.

private TsModel inlineEnums(final TsModel tsModel, final SymbolTable symbolTable) {
    final Set<TsAliasModel> inlinedAliases = new LinkedHashSet<>();
    final TsModel newTsModel = transformBeanPropertyTypes(tsModel, new TsType.Transformer() {

        @Override
        public TsType transform(TsType.Context context, TsType tsType) {
            if (tsType instanceof TsType.EnumReferenceType) {
                final TsAliasModel alias = tsModel.getTypeAlias(getOriginClass(symbolTable, tsType));
                if (alias != null) {
                    inlinedAliases.add(alias);
                    return alias.getDefinition();
                }
            }
            return tsType;
        }
    });
    return newTsModel.withRemovedTypeAliases(new ArrayList<>(inlinedAliases));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsType(cz.habarta.typescript.generator.TsType)

Example 4 with TsAliasModel

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

the class ModelCompiler method transformNullableTypes.

private TsModel transformNullableTypes(final SymbolTable symbolTable, TsModel tsModel) {
    final AtomicBoolean declareNullableType = new AtomicBoolean(false);
    final NullabilityDefinition nullabilityDefinition = settings.getNullabilityDefinition();
    TsModel transformedModel = transformBeanPropertyTypes(tsModel, new TsType.Transformer() {

        @Override
        public TsType transform(TsType.Context context, TsType tsType) {
            if (tsType instanceof TsType.NullableType) {
                final TsType.NullableType nullableType = (TsType.NullableType) tsType;
                if (nullabilityDefinition.isInline()) {
                    return new TsType.UnionType(nullableType.type).add(nullabilityDefinition.getTypes());
                } else {
                    declareNullableType.set(true);
                }
            }
            return tsType;
        }
    });
    // type Nullable<T> = T | ...
    if (declareNullableType.get()) {
        final TsType.GenericVariableType tVar = new TsType.GenericVariableType("T");
        transformedModel = transformedModel.withAddedTypeAliases(Arrays.asList(new TsAliasModel(/*origin*/
        null, symbolTable.getSyntheticSymbol(TsType.NullableType.AliasName), Arrays.asList(tVar), new TsType.UnionType(tVar).add(nullabilityDefinition.getTypes()), /*comments*/
        null)));
    }
    return transformedModel;
}
Also used : NullabilityDefinition(cz.habarta.typescript.generator.NullabilityDefinition) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsType(cz.habarta.typescript.generator.TsType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TsModel(cz.habarta.typescript.generator.emitter.TsModel)

Example 5 with TsAliasModel

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

the class ModelCompiler method transformDates.

private TsModel transformDates(SymbolTable symbolTable, TsModel tsModel) {
    final TsAliasModel dateAsNumber = new TsAliasModel(null, symbolTable.getSyntheticSymbol("DateAsNumber"), null, TsType.Number, null);
    final TsAliasModel dateAsString = new TsAliasModel(null, symbolTable.getSyntheticSymbol("DateAsString"), null, TsType.String, null);
    final LinkedHashSet<TsAliasModel> typeAliases = new LinkedHashSet<>(tsModel.getTypeAliases());
    final TsModel model = transformBeanPropertyTypes(tsModel, new TsType.Transformer() {

        @Override
        public TsType transform(TsType.Context context, TsType type) {
            if (type == TsType.Date) {
                if (settings.mapDate == DateMapping.asNumber) {
                    typeAliases.add(dateAsNumber);
                    return new TsType.ReferenceType(dateAsNumber.getName());
                }
                if (settings.mapDate == DateMapping.asString) {
                    typeAliases.add(dateAsString);
                    return new TsType.ReferenceType(dateAsString.getName());
                }
            }
            return type;
        }
    });
    return model.withTypeAliases(new ArrayList<>(typeAliases));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsType(cz.habarta.typescript.generator.TsType)

Aggregations

TsAliasModel (cz.habarta.typescript.generator.emitter.TsAliasModel)8 TsType (cz.habarta.typescript.generator.TsType)7 LinkedHashSet (java.util.LinkedHashSet)5 TsModel (cz.habarta.typescript.generator.emitter.TsModel)4 ArrayList (java.util.ArrayList)3 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)2 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)2 NullabilityDefinition (cz.habarta.typescript.generator.NullabilityDefinition)1 Settings (cz.habarta.typescript.generator.Settings)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1