use of cz.habarta.typescript.generator.NullabilityDefinition 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.NullabilityDefinition in project typescript-generator by vojtechhabarta.
the class ModelCompiler method makeUndefinablePropertiesAndParametersOptional.
// example: transforms property `text: string | undefined` to `text?: string | undefined`
private TsModel makeUndefinablePropertiesAndParametersOptional(final SymbolTable symbolTable, TsModel tsModel) {
final NullabilityDefinition nullabilityDefinition = settings.getNullabilityDefinition();
if (!nullabilityDefinition.containsUndefined()) {
return tsModel;
}
return tsModel.withBeans(tsModel.getBeans().stream().map(bean -> {
bean = bean.withProperties(bean.getProperties().stream().map(property -> property.withTsType(makeNullableTypeOptional(property.getTsType()))).collect(Collectors.toList()));
bean = bean.withMethods(bean.getMethods().stream().map(method -> method.withParameters(method.getParameters().stream().map(parameter -> parameter.withTsType(makeNullableTypeOptional(parameter.getTsType()))).collect(Collectors.toList()))).collect(Collectors.toList()));
return bean;
}).collect(Collectors.toList()));
}
Aggregations