Search in sources :

Example 1 with NullabilityDefinition

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;
}
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 2 with NullabilityDefinition

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()));
}
Also used : TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) Arrays(java.util.Arrays) PropertyModel(cz.habarta.typescript.generator.parser.PropertyModel) NullabilityDefinition(cz.habarta.typescript.generator.NullabilityDefinition) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) GenericsResolver(cz.habarta.typescript.generator.util.GenericsResolver) EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) OptionalPropertiesDeclaration(cz.habarta.typescript.generator.OptionalPropertiesDeclaration) Matcher(java.util.regex.Matcher) Map(java.util.Map) RestMethodModel(cz.habarta.typescript.generator.parser.RestMethodModel) TsHelper(cz.habarta.typescript.generator.emitter.TsHelper) RestApplicationModel(cz.habarta.typescript.generator.parser.RestApplicationModel) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags) DateMapping(cz.habarta.typescript.generator.DateMapping) Set(java.util.Set) TsTaggedTemplateLiteral(cz.habarta.typescript.generator.emitter.TsTaggedTemplateLiteral) Collectors(java.util.stream.Collectors) TypeScriptGenerator(cz.habarta.typescript.generator.TypeScriptGenerator) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) Objects(java.util.Objects) List(java.util.List) IdentifierCasing(cz.habarta.typescript.generator.IdentifierCasing) TsTemplateLiteral(cz.habarta.typescript.generator.emitter.TsTemplateLiteral) Stream(java.util.stream.Stream) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) EnumModel(cz.habarta.typescript.generator.parser.EnumModel) Pattern(java.util.regex.Pattern) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) EnumMapping(cz.habarta.typescript.generator.EnumMapping) TsProperty(cz.habarta.typescript.generator.TsProperty) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TsAccessibilityModifier(cz.habarta.typescript.generator.emitter.TsAccessibilityModifier) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) JTypeWithNullability(cz.habarta.typescript.generator.type.JTypeWithNullability) ArrayList(java.util.ArrayList) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) LinkedHashMap(java.util.LinkedHashMap) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) Extension(cz.habarta.typescript.generator.Extension) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel) LinkedHashSet(java.util.LinkedHashSet) TsParameter(cz.habarta.typescript.generator.TsParameter) MethodModel(cz.habarta.typescript.generator.parser.MethodModel) TypeProcessor(cz.habarta.typescript.generator.TypeProcessor) RestNamespacing(cz.habarta.typescript.generator.RestNamespacing) PropertyAccess(cz.habarta.typescript.generator.parser.PropertyAccess) TypeVariable(java.lang.reflect.TypeVariable) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) Settings(cz.habarta.typescript.generator.Settings) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) MapMapping(cz.habarta.typescript.generator.MapMapping) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) Pair(cz.habarta.typescript.generator.util.Pair) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) TsBeanCategory(cz.habarta.typescript.generator.emitter.TsBeanCategory) TsType(cz.habarta.typescript.generator.TsType) Utils(cz.habarta.typescript.generator.util.Utils) Model(cz.habarta.typescript.generator.parser.Model) Comparator(java.util.Comparator) Collections(java.util.Collections) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) NullabilityDefinition(cz.habarta.typescript.generator.NullabilityDefinition)

Aggregations

NullabilityDefinition (cz.habarta.typescript.generator.NullabilityDefinition)2 TsType (cz.habarta.typescript.generator.TsType)2 TsAliasModel (cz.habarta.typescript.generator.emitter.TsAliasModel)2 TsModel (cz.habarta.typescript.generator.emitter.TsModel)2 DateMapping (cz.habarta.typescript.generator.DateMapping)1 EnumMapping (cz.habarta.typescript.generator.EnumMapping)1 Extension (cz.habarta.typescript.generator.Extension)1 IdentifierCasing (cz.habarta.typescript.generator.IdentifierCasing)1 MapMapping (cz.habarta.typescript.generator.MapMapping)1 OptionalPropertiesDeclaration (cz.habarta.typescript.generator.OptionalPropertiesDeclaration)1 RestNamespacing (cz.habarta.typescript.generator.RestNamespacing)1 Settings (cz.habarta.typescript.generator.Settings)1 TsParameter (cz.habarta.typescript.generator.TsParameter)1 TsProperty (cz.habarta.typescript.generator.TsProperty)1 TypeProcessor (cz.habarta.typescript.generator.TypeProcessor)1 TypeScriptGenerator (cz.habarta.typescript.generator.TypeScriptGenerator)1 EmitterExtension (cz.habarta.typescript.generator.emitter.EmitterExtension)1 TsAccessibilityModifier (cz.habarta.typescript.generator.emitter.TsAccessibilityModifier)1 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)1 TsBeanCategory (cz.habarta.typescript.generator.emitter.TsBeanCategory)1