Search in sources :

Example 6 with TsExpression

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

the class JsonDeserializationExtension method addCopyFnToJaxrsMethod.

private static TsMethodModel addCopyFnToJaxrsMethod(SymbolTable symbolTable, TsModel tsModel, TsMethodModel method) {
    final TsType returnType = method.getReturnType();
    if (!(returnType instanceof TsType.GenericReferenceType))
        return null;
    final TsType.GenericReferenceType genericReferenceReturnType = (TsType.GenericReferenceType) returnType;
    if (genericReferenceReturnType.symbol != symbolTable.getSyntheticSymbol("RestResponse"))
        return null;
    final List<TsType> typeArguments = genericReferenceReturnType.typeArguments;
    if (typeArguments == null || typeArguments.size() != 1)
        return null;
    final TsType returnDataType = typeArguments.get(0);
    final List<TsStatement> body = method.getBody();
    if (body == null || body.size() != 1)
        return null;
    final TsStatement statement = body.get(0);
    if (!(statement instanceof TsReturnStatement))
        return null;
    final TsReturnStatement returnStatement = (TsReturnStatement) statement;
    final TsExpression returnExpression = returnStatement.getExpression();
    if (returnExpression == null)
        return null;
    if (!(returnExpression instanceof TsCallExpression))
        return null;
    final TsCallExpression callExpression = (TsCallExpression) returnExpression;
    final List<TsExpression> arguments = callExpression.getArguments();
    if (arguments == null || arguments.isEmpty())
        return null;
    final TsExpression firstArgument = arguments.get(0);
    if (!(firstArgument instanceof TsObjectLiteral))
        return null;
    final TsObjectLiteral objectLiteral = (TsObjectLiteral) firstArgument;
    // todo create changed method instead of modifying existing
    final int index = Math.max(objectLiteral.getPropertyDefinitions().size() - 1, 0);
    final TsExpression copyFunction = returnDataType == TsType.Void ? TsIdentifierReference.Undefined : getCopyFunctionForTsType(symbolTable, tsModel, returnDataType);
    objectLiteral.getPropertyDefinitions().add(index, new TsPropertyDefinition("copyFn", copyFunction));
    return method;
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsPropertyDefinition(cz.habarta.typescript.generator.emitter.TsPropertyDefinition)

Example 7 with TsExpression

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

the class OnePossiblePropertyValueAssigningExtension method transformBean.

private static TsBeanModel transformBean(TsBeanModel bean, TsModel model) {
    if (!bean.isClass() || bean.getConstructor() != null) {
        return bean;
    }
    List<TsPropertyModel> newProperties = new ArrayList<>();
    Collection<TsExpressionStatement> valueAssignmentStatements = new ArrayList<>();
    for (TsPropertyModel property : bean.getProperties()) {
        TsPropertyModel newProperty = property;
        Optional<TsExpression> onlyValue = findOnlyValueForProperty(property, model);
        if (onlyValue.isPresent()) {
            newProperty = new TsPropertyModel(property.name, property.tsType, TsModifierFlags.None.setReadonly(), property.ownProperty, property.comments);
            TsExpressionStatement assignmentStatement = createValueAssignmentStatement(newProperty, onlyValue.get());
            valueAssignmentStatements.add(assignmentStatement);
        }
        newProperties.add(newProperty);
    }
    TsBeanModel newBean = bean.withProperties(newProperties);
    if (!valueAssignmentStatements.isEmpty()) {
        TsConstructorModel constructor = createConstructor(bean, valueAssignmentStatements);
        newBean = newBean.withConstructor(constructor);
    }
    return newBean;
}
Also used : TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) ArrayList(java.util.ArrayList) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 8 with TsExpression

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

the class OnePossiblePropertyValueAssigningExtension method createValueAssignmentStatement.

private static TsExpressionStatement createValueAssignmentStatement(TsPropertyModel property, TsExpression value) {
    TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
    TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, value);
    return new TsExpressionStatement(assignment);
}
Also used : TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression)

Example 9 with TsExpression

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

the class ModelCompiler method processPathTemplate.

private static TsTemplateLiteral processPathTemplate(PathTemplate pathTemplate) {
    final List<TsExpression> spans = new ArrayList<>();
    for (PathTemplate.Part part : pathTemplate.getParts()) {
        if (part instanceof PathTemplate.Literal) {
            final PathTemplate.Literal literal = (PathTemplate.Literal) part;
            spans.add(new TsStringLiteral(literal.getLiteral()));
        }
        if (part instanceof PathTemplate.Parameter) {
            final PathTemplate.Parameter parameter = (PathTemplate.Parameter) part;
            spans.add(new TsIdentifierReference(parameter.getValidName()));
        }
    }
    return new TsTaggedTemplateLiteral(new TsIdentifierReference("uriEncoding"), spans);
}
Also used : TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsTaggedTemplateLiteral(cz.habarta.typescript.generator.emitter.TsTaggedTemplateLiteral) TsTaggedTemplateLiteral(cz.habarta.typescript.generator.emitter.TsTaggedTemplateLiteral) TsTemplateLiteral(cz.habarta.typescript.generator.emitter.TsTemplateLiteral) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsObjectLiteral(cz.habarta.typescript.generator.emitter.TsObjectLiteral) ArrayList(java.util.ArrayList) TsParameter(cz.habarta.typescript.generator.TsParameter) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate)

Aggregations

TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)9 ArrayList (java.util.ArrayList)7 TsType (cz.habarta.typescript.generator.TsType)6 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)6 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)6 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)5 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)5 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)5 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)4 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)4 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)4 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)4 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)4 Symbol (cz.habarta.typescript.generator.compiler.Symbol)3 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)3 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)3 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)3 Extension (cz.habarta.typescript.generator.Extension)2 TsParameter (cz.habarta.typescript.generator.TsParameter)2 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)2