Search in sources :

Example 1 with RestApplicationModel

use of cz.habarta.typescript.generator.parser.RestApplicationModel in project typescript-generator by vojtechhabarta.

the class ModelCompiler method javaToTypeScript.

public TsModel javaToTypeScript(Model model) {
    final SymbolTable symbolTable = new SymbolTable(settings);
    final List<Extension.TransformerDefinition> extensionTransformers = getExtensionTransformers();
    model = applyExtensionModelTransformers(symbolTable, model, extensionTransformers);
    TsModel tsModel = processModel(symbolTable, model);
    tsModel = addCustomTypeAliases(symbolTable, tsModel);
    tsModel = removeInheritedProperties(symbolTable, tsModel);
    tsModel = addImplementedProperties(symbolTable, tsModel);
    tsModel = sortPropertiesDeclarations(symbolTable, tsModel);
    if (settings.generateConstructors) {
        tsModel = addConstructors(symbolTable, tsModel);
    }
    // REST
    if (settings.isGenerateRest()) {
        final Symbol responseSymbol = createRestResponseType(symbolTable, tsModel);
        final TsType optionsType = settings.restOptionsType != null ? new TsType.VerbatimType(settings.restOptionsType) : null;
        final TsType.GenericVariableType optionsGenericVariable = settings.restOptionsTypeIsGeneric ? new TsType.GenericVariableType(settings.restOptionsType) : null;
        final List<RestApplicationModel> restApplicationsWithInterface = model.getRestApplications().stream().filter(restApplication -> restApplication.getType().generateInterface.apply(settings)).collect(Collectors.toList());
        final List<RestApplicationModel> restApplicationsWithClient = model.getRestApplications().stream().filter(restApplication -> restApplication.getType().generateClient.apply(settings)).collect(Collectors.toList());
        if (!restApplicationsWithInterface.isEmpty()) {
            createRestInterfaces(tsModel, symbolTable, restApplicationsWithInterface, responseSymbol, optionsGenericVariable, optionsType);
        }
        if (!restApplicationsWithClient.isEmpty()) {
            createRestClients(tsModel, symbolTable, restApplicationsWithClient, responseSymbol, optionsGenericVariable, optionsType);
        }
    }
    // dates
    tsModel = transformMaps(symbolTable, tsModel);
    // dates
    tsModel = transformDates(symbolTable, tsModel);
    // enums
    tsModel = applyExtensionTransformers(symbolTable, model, tsModel, TransformationPhase.BeforeEnums, extensionTransformers);
    tsModel = addEnumValuesToJavadoc(tsModel);
    if (settings.enumMemberCasing != null && settings.enumMemberCasing != IdentifierCasing.keepOriginal) {
        tsModel = transformEnumMembersCase(tsModel);
    }
    if (!settings.areDefaultStringEnumsOverriddenByExtension()) {
        if (settings.mapEnum == null || settings.mapEnum == EnumMapping.asUnion || settings.mapEnum == EnumMapping.asInlineUnion) {
            tsModel = transformEnumsToUnions(tsModel);
        }
        if (settings.mapEnum == EnumMapping.asInlineUnion) {
            tsModel = inlineEnums(tsModel, symbolTable);
        }
        if (settings.mapEnum == EnumMapping.asNumberBasedEnum) {
            tsModel = transformEnumsToNumberBasedEnum(tsModel);
        }
    }
    // after enum transformations transform Maps with rest of the enums (not unions) used in keys
    tsModel = transformNonStringEnumKeyMaps(symbolTable, tsModel);
    // tagged unions
    tsModel = createAndUseTaggedUnions(symbolTable, tsModel);
    // nullable types and optional properties
    tsModel = makeUndefinablePropertiesAndParametersOptional(symbolTable, tsModel);
    tsModel = transformNullableTypes(symbolTable, tsModel);
    tsModel = eliminateUndefinedFromOptionalPropertiesAndParameters(symbolTable, tsModel);
    tsModel = transformOptionalProperties(symbolTable, tsModel);
    tsModel = applyExtensionTransformers(symbolTable, model, tsModel, TransformationPhase.BeforeSymbolResolution, extensionTransformers);
    symbolTable.resolveSymbolNames();
    tsModel = sortTypeDeclarations(symbolTable, tsModel);
    tsModel = applyExtensionTransformers(symbolTable, model, tsModel, TransformationPhase.AfterDeclarationSorting, extensionTransformers);
    return tsModel;
}
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) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsType(cz.habarta.typescript.generator.TsType) RestApplicationModel(cz.habarta.typescript.generator.parser.RestApplicationModel)

Example 2 with RestApplicationModel

use of cz.habarta.typescript.generator.parser.RestApplicationModel in project typescript-generator by vojtechhabarta.

the class ModelCompiler method processRestMethodGroup.

private List<TsMethodModel> processRestMethodGroup(TsModel tsModel, SymbolTable symbolTable, List<Pair<RestApplicationModel, RestMethodModel>> methods, Symbol responseSymbol, TsType optionsType, boolean implement) {
    final List<TsMethodModel> resultMethods = new ArrayList<>();
    final Map<String, Long> methodNamesCount = groupingByMethodName(methods);
    for (Pair<RestApplicationModel, RestMethodModel> pair : methods) {
        final RestApplicationModel restApplication = pair.getValue1();
        final RestMethodModel method = pair.getValue2();
        final boolean createLongName = methodNamesCount.get(method.getName()) > 1;
        resultMethods.add(processRestMethod(tsModel, symbolTable, restApplication.getApplicationPath(), responseSymbol, method, createLongName, optionsType, implement));
    }
    return resultMethods;
}
Also used : ArrayList(java.util.ArrayList) RestMethodModel(cz.habarta.typescript.generator.parser.RestMethodModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) RestApplicationModel(cz.habarta.typescript.generator.parser.RestApplicationModel)

Aggregations

TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)2 RestApplicationModel (cz.habarta.typescript.generator.parser.RestApplicationModel)2 RestMethodModel (cz.habarta.typescript.generator.parser.RestMethodModel)2 ArrayList (java.util.ArrayList)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 NullabilityDefinition (cz.habarta.typescript.generator.NullabilityDefinition)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 TsType (cz.habarta.typescript.generator.TsType)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