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);
}
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));
}
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));
}
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;
}
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));
}
Aggregations