Search in sources :

Example 1 with TsStringLiteral

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

the class OnePossiblePropertyValueAssigningExtension method findOnlyValueForEnumReferenceType.

private static Optional<TsExpression> findOnlyValueForEnumReferenceType(TsModel model, TsType.EnumReferenceType propertyType) {
    Symbol symbol = propertyType.symbol;
    Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
    if (!enumModelOption.isPresent()) {
        return Optional.empty();
    }
    TsEnumModel enumModel = enumModelOption.get();
    if (enumModel.getMembers().size() != 1) {
        return Optional.empty();
    }
    EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
    Object enumValue = singleElement.getEnumValue();
    TsStringLiteral expression = new TsStringLiteral((String) enumValue);
    return Optional.of(expression);
}
Also used : TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) ArrayList(java.util.ArrayList) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) Extension(cz.habarta.typescript.generator.Extension) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Collection(java.util.Collection) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Collectors(java.util.stream.Collectors) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) List(java.util.List) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) Optional(java.util.Optional) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Collections(java.util.Collections) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 2 with TsStringLiteral

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

the class RequiredPropertyConstructorExtension method getPredefinedValueForProperty.

private static Optional<TsExpression> getPredefinedValueForProperty(TsPropertyModel property, TsModel model) {
    if (property.tsType instanceof TsType.UnionType) {
        List<TsType> unionTypeElements = ((TsType.UnionType) property.tsType).types;
        if (unionTypeElements.size() != 1) {
            return Optional.empty();
        }
        TsType onlyElement = unionTypeElements.iterator().next();
        if (!(onlyElement instanceof TsType.StringLiteralType)) {
            return Optional.empty();
        }
        TsType.StringLiteralType onlyValue = (TsType.StringLiteralType) onlyElement;
        TsStringLiteral expression = new TsStringLiteral(onlyValue.literal);
        return Optional.of(expression);
    }
    if (property.tsType instanceof TsType.EnumReferenceType) {
        Symbol symbol = ((TsType.EnumReferenceType) property.tsType).symbol;
        Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
        if (!enumModelOption.isPresent()) {
            return Optional.empty();
        }
        TsEnumModel enumModel = enumModelOption.get();
        if (enumModel.getMembers().size() != 1) {
            return Optional.empty();
        }
        EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
        Object enumValue = singleElement.getEnumValue();
        TsStringLiteral expression = new TsStringLiteral((String) enumValue);
        return Optional.of(expression);
    }
    return Optional.empty();
}
Also used : TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) Arrays(java.util.Arrays) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) HashMap(java.util.HashMap) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) ArrayList(java.util.ArrayList) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) Extension(cz.habarta.typescript.generator.Extension) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Map(java.util.Map) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel) TsModifierFlags(cz.habarta.typescript.generator.emitter.TsModifierFlags) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) List(java.util.List) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) Optional(java.util.Optional) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) Pattern(java.util.regex.Pattern) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) Symbol(cz.habarta.typescript.generator.compiler.Symbol) TsType(cz.habarta.typescript.generator.TsType) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral)

Example 3 with TsStringLiteral

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

the class DecoratorsTest method testDecoratorsOnParameterAndMethod.

@Test
public void testDecoratorsOnParameterAndMethod() {
    final Settings settings = TestUtils.settings();
    settings.outputFileType = TypeScriptFileType.implementationFile;
    settings.outputKind = TypeScriptOutputKind.module;
    settings.mapClasses = ClassMapping.asClasses;
    settings.generateConstructors = true;
    final TypeScriptGenerator typeScriptGenerator = new TypeScriptGenerator(settings);
    final Model model = typeScriptGenerator.getModelParser().parseModel(City.class);
    final TsModel tsModel = typeScriptGenerator.getModelCompiler().javaToTypeScript(model);
    final TsBeanModel bean = tsModel.getBean(City.class);
    final TsBeanModel bean2 = bean.withConstructor(bean.getConstructor().withParameters(Arrays.asList(bean.getConstructor().getParameters().get(0).withDecorators(Arrays.asList(new TsDecorator(new TsIdentifierReference("Inject"), Arrays.asList(new TsStringLiteral("token")))))))).withMethods(Arrays.asList(new TsMethodModel("greet", null, null, Collections.emptyList(), TsType.Void, Collections.emptyList(), null).withDecorators(Arrays.asList(new TsDecorator(new TsIdentifierReference("enumerable"), Arrays.asList(new TsBooleanLiteral(false)))))));
    final TsModel tsModel2 = tsModel.withBeans(Arrays.asList(bean2));
    final String output = emit(typeScriptGenerator.getEmitter(), tsModel2);
    Assertions.assertTrue(output.contains("@Inject(\"token\")"));
    Assertions.assertTrue(output.contains("@enumerable(false)"));
}
Also used : TsModel(cz.habarta.typescript.generator.emitter.TsModel) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Model(cz.habarta.typescript.generator.parser.Model) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsBooleanLiteral(cz.habarta.typescript.generator.emitter.TsBooleanLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsDecorator(cz.habarta.typescript.generator.emitter.TsDecorator) Test(org.junit.jupiter.api.Test)

Example 4 with TsStringLiteral

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

the class ModelCompiler method processRestMethod.

private TsMethodModel processRestMethod(TsModel tsModel, SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, RestMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
    final String path = Utils.joinPath(pathPrefix, method.getPath());
    final PathTemplate pathTemplate = PathTemplate.parse(path);
    final List<String> comments = Utils.concat(method.getComments(), Arrays.asList("HTTP " + method.getHttpMethod() + " /" + path, "Java method: " + method.getOriginClass().getName() + "." + method.getName()));
    final List<TsParameterModel> parameters = new ArrayList<>();
    // path params
    for (MethodParameterModel parameter : method.getPathParams()) {
        parameters.add(processParameter(symbolTable, method, parameter));
    }
    // entity param
    if (method.getEntityParam() != null) {
        parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
    }
    // query params
    final List<RestQueryParam> queryParams = method.getQueryParams();
    final TsParameterModel queryParameter;
    if (queryParams != null && !queryParams.isEmpty()) {
        final List<TsType> types = new ArrayList<>();
        if (queryParams.stream().anyMatch(param -> param instanceof RestQueryParam.Map)) {
            types.add(new TsType.IndexedArrayType(TsType.String, TsType.Any));
        } else {
            final List<TsProperty> currentSingles = new ArrayList<>();
            final Runnable flushSingles = () -> {
                if (!currentSingles.isEmpty()) {
                    types.add(new TsType.ObjectType(currentSingles));
                    currentSingles.clear();
                }
            };
            for (RestQueryParam restQueryParam : queryParams) {
                if (restQueryParam instanceof RestQueryParam.Single) {
                    final MethodParameterModel queryParam = ((RestQueryParam.Single) restQueryParam).getQueryParam();
                    final TsType type = typeFromJava(symbolTable, queryParam.getType(), method.getName(), method.getOriginClass());
                    currentSingles.add(new TsProperty(queryParam.getName(), restQueryParam.required ? type : new TsType.OptionalType(type)));
                }
                if (restQueryParam instanceof RestQueryParam.Bean) {
                    final BeanModel queryBean = ((RestQueryParam.Bean) restQueryParam).getBean();
                    flushSingles.run();
                    final Symbol queryParamsSymbol = symbolTable.getSymbol(queryBean.getOrigin(), "QueryParams");
                    if (tsModel.getBean(queryParamsSymbol) == null) {
                        tsModel.getBeans().add(new TsBeanModel(queryBean.getOrigin(), TsBeanCategory.Data, /*isClass*/
                        false, queryParamsSymbol, /*typeParameters*/
                        null, /*parent*/
                        null, /*extendsList*/
                        null, /*implementsList*/
                        null, processProperties(symbolTable, null, queryBean), /*constructor*/
                        null, /*methods*/
                        null, /*comments*/
                        null));
                    }
                    types.add(new TsType.ReferenceType(queryParamsSymbol));
                }
            }
            flushSingles.run();
        }
        boolean allQueryParamsOptional = queryParams.stream().noneMatch(queryParam -> queryParam.required);
        TsType.IntersectionType queryParamType = new TsType.IntersectionType(types);
        queryParameter = new TsParameterModel("queryParams", allQueryParamsOptional ? new TsType.OptionalType(queryParamType) : queryParamType);
        parameters.add(queryParameter);
    } else {
        queryParameter = null;
    }
    if (optionsType != null) {
        final TsParameterModel optionsParameter = new TsParameterModel("options", new TsType.OptionalType(optionsType));
        parameters.add(optionsParameter);
    }
    // return type
    final TsType returnType = typeFromJava(symbolTable, method.getReturnType(), method.getName(), method.getOriginClass());
    final TsType wrappedReturnType = new TsType.GenericReferenceType(responseSymbol, returnType);
    // method name
    final String nameSuffix;
    if (createLongName) {
        nameSuffix = "$" + method.getHttpMethod() + "$" + pathTemplate.format("", "", false).replaceAll("/", "_").replaceAll("\\W", "");
    } else {
        nameSuffix = "";
    }
    // implementation
    final List<TsStatement> body;
    if (implement) {
        body = new ArrayList<>();
        body.add(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsMemberExpression(new TsThisExpression(), "httpClient"), "request"), new TsObjectLiteral(new TsPropertyDefinition("method", new TsStringLiteral(method.getHttpMethod())), new TsPropertyDefinition("url", processPathTemplate(pathTemplate)), queryParameter != null ? new TsPropertyDefinition("queryParams", new TsIdentifierReference("queryParams")) : null, method.getEntityParam() != null ? new TsPropertyDefinition("data", new TsIdentifierReference(method.getEntityParam().getName())) : null, optionsType != null ? new TsPropertyDefinition("options", new TsIdentifierReference("options")) : null))));
    } else {
        body = null;
    }
    // method
    final TsMethodModel tsMethodModel = new TsMethodModel(method.getName() + nameSuffix, TsModifierFlags.None, null, parameters, wrappedReturnType, body, comments);
    return tsMethodModel;
}
Also used : TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) ArrayList(java.util.ArrayList) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 5 with TsStringLiteral

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

the class JsonDeserializationExtension method createDeserializationMethodForTaggedUnion.

private static TsMethodModel createDeserializationMethodForTaggedUnion(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
    final List<TsSwitchCaseClause> caseClauses = new ArrayList<>();
    for (Class<?> cls : bean.getTaggedUnionClasses()) {
        final TsBeanModel tuBean = tsModel.getBean(cls);
        caseClauses.add(new TsSwitchCaseClause(new TsStringLiteral(tuBean.getDiscriminantLiteral()), Arrays.<TsStatement>asList(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(symbolTable.getSymbol(cls))), "fromData"), new TsIdentifierReference("data"))))));
    }
    final List<TsStatement> body = new ArrayList<>();
    body.add(ifUndefinedThenReturnItStatement("data"));
    body.add(new TsSwitchStatement(new TsMemberExpression(new TsIdentifierReference("data"), bean.getDiscriminantProperty()), caseClauses, null));
    final TsType.ReferenceType unionType = new TsType.ReferenceType(bean.getTaggedUnionAlias().getName());
    return new TsMethodModel("fromDataUnion", TsModifierFlags.None.setStatic(), // typeParameters,
    null, Arrays.asList(new TsParameterModel("data", unionType)), unionType, body, null);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsSwitchStatement(cz.habarta.typescript.generator.emitter.TsSwitchStatement) TsSwitchCaseClause(cz.habarta.typescript.generator.emitter.TsSwitchCaseClause) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Aggregations

TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)7 TsType (cz.habarta.typescript.generator.TsType)5 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)5 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)5 ArrayList (java.util.ArrayList)5 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)4 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)3 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)3 TsModel (cz.habarta.typescript.generator.emitter.TsModel)3 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)3 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)3 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)3 Extension (cz.habarta.typescript.generator.Extension)2 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)2 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)2 Symbol (cz.habarta.typescript.generator.compiler.Symbol)2 SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)2 EmitterExtensionFeatures (cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)2