Search in sources :

Example 21 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class DefaultValueComposer method createSimplePagedResponseValue.

public static Expr createSimplePagedResponseValue(TypeNode responseType, String repeatedFieldName, Expr responseElementVarExpr, boolean isMap) {
    // Code for paginated maps:
    // AggregatedMessageList.newBuilder()
    // .setNextPageToken("")
    // .putAllItems(Collections.singletonMap("items", responsesElement))
    // .build();
    // 
    // Code for paginated arrays:
    // MessageList expectedResponse =
    // AddressList.newBuilder()
    // .setNextPageToken("")
    // .addAllItems(Arrays.asList(responsesElement))
    // .build();
    Expr pagedResponseExpr = MethodInvocationExpr.builder().setStaticReferenceType(responseType).setMethodName("newBuilder").build();
    pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName("setNextPageToken").setArguments(ValueExpr.withValue(StringObjectValue.withValue(""))).build();
    if (isMap) {
        pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName(String.format("putAll%s", JavaStyle.toUpperCamelCase(repeatedFieldName))).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Collections.class))).setMethodName("singletonMap").setArguments(ValueExpr.withValue(StringObjectValue.withValue(JavaStyle.toLowerCamelCase(repeatedFieldName))), responseElementVarExpr).build()).build();
    } else {
        pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName(String.format("addAll%s", JavaStyle.toUpperCamelCase(repeatedFieldName))).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Arrays.class))).setMethodName("asList").setArguments(responseElementVarExpr).build()).build();
    }
    return MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName("build").setReturnType(responseType).build();
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr)

Example 22 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class DefaultValueComposer method createSimpleMessageBuilderValue.

public static Expr createSimpleMessageBuilderValue(Message message, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
    MethodInvocationExpr builderExpr = MethodInvocationExpr.builder().setStaticReferenceType(message.type()).setMethodName("newBuilder").build();
    for (Field field : message.fields()) {
        if (// Avoid colliding fields.
        field.isContainedInOneof() || ((field.isMessage() || (field.isEnum() && // Avoid importing unparsed messages.
        message.operationResponse() == null)) && !field.isRepeated() && !messageTypes.containsKey(field.type().reference().fullName()))) {
            continue;
        }
        String setterMethodNamePattern = "set%s";
        if (field.isRepeated()) {
            setterMethodNamePattern = field.isMap() ? "putAll%s" : "addAll%s";
        }
        Expr defaultExpr = null;
        if (field.hasResourceReference() && resourceNames.get(field.resourceReference().resourceTypeString()) != null) {
            defaultExpr = createResourceHelperValue(resourceNames.get(field.resourceReference().resourceTypeString()), field.resourceReference().isChildType(), resourceNames.values().stream().collect(Collectors.toList()), message.name(), /* allowAnonResourceNameClass = */
            false);
            defaultExpr = MethodInvocationExpr.builder().setExprReferenceExpr(defaultExpr).setMethodName("toString").setReturnType(TypeNode.STRING).build();
        } else {
            if (message.operationResponse() != null) {
                if (field.name().equals(message.operationResponse().statusFieldName())) {
                    String statusTypeName = message.operationResponse().statusFieldTypeName();
                    String statusClassName = statusTypeName.substring(statusTypeName.lastIndexOf('.') + 1);
                    TypeNode statusType = TypeNode.withReference(VaporReference.builder().setName(statusClassName).setPakkage(message.type().reference().fullName()).setIsStaticImport(false).build());
                    defaultExpr = VariableExpr.builder().setVariable(Variable.builder().setName("DONE").setType(statusType).build()).setStaticReferenceType(statusType).build();
                } else if (field.name().equals(message.operationResponse().errorCodeFieldName())) {
                    defaultExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(field.type()).setValue("0").build());
                }
            }
            if (defaultExpr == null) {
                defaultExpr = createValue(field, true, resourceNames, messageTypes, valuePatterns);
            }
        }
        builderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName(String.format(setterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))).setArguments(defaultExpr).build();
    }
    return MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName("build").setReturnType(message.type()).build();
}
Also used : Field(com.google.api.generator.gapic.model.Field) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 23 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class DefaultValueComposer method createMethodArgValue.

public static Expr createMethodArgValue(MethodArgument methodArg, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
    if (methodArg.isResourceNameHelper()) {
        Preconditions.checkState(methodArg.field().hasResourceReference(), String.format("No corresponding resource reference for argument %s found on field %s %s", methodArg.name(), methodArg.field().type(), methodArg.field().name()));
        ResourceName resourceName = resourceNames.get(methodArg.field().resourceReference().resourceTypeString());
        Preconditions.checkNotNull(resourceName, String.format("No resource name found for reference %s", methodArg.field().resourceReference().resourceTypeString()));
        Expr defValue = createResourceHelperValue(resourceName, methodArg.field().resourceReference().isChildType(), resourceNames.values().stream().collect(Collectors.toList()), methodArg.field().name());
        if (!methodArg.isResourceNameHelper() && methodArg.field().hasResourceReference()) {
            defValue = MethodInvocationExpr.builder().setExprReferenceExpr(defValue).setMethodName("toString").setReturnType(TypeNode.STRING).build();
        }
        return defValue;
    }
    if (methodArg.type().equals(methodArg.field().type())) {
        return createValue(methodArg.field(), false, resourceNames, messageTypes, valuePatterns);
    }
    return createValue(Field.builder().setName(methodArg.name()).setType(methodArg.type()).build());
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ResourceName(com.google.api.generator.gapic.model.ResourceName)

Example 24 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class DefaultValueComposer method createSimpleOperationBuilderValue.

public static Expr createSimpleOperationBuilderValue(String name, VariableExpr responseExpr) {
    Expr operationExpr = MethodInvocationExpr.builder().setStaticReferenceType(OPERATION_TYPE).setMethodName("newBuilder").build();
    operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setName").setArguments(ValueExpr.withValue(StringObjectValue.withValue(name))).build();
    operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setDone").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build())).build();
    operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setResponse").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(ANY_TYPE).setMethodName("pack").setArguments(responseExpr).build()).build();
    return MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("build").setReturnType(OPERATION_TYPE).build();
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr)

Example 25 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class MockServiceImplClassComposer method createHandleObjectStatement.

private static Statement createHandleObjectStatement(Method protoMethod, VariableExpr requestArgVarExpr, VariableExpr responseObserverVarExpr, VariableExpr localResponseVarExpr) {
    List<Expr> ifBodyExprs = new ArrayList<>();
    boolean isAnonymousClass = requestArgVarExpr == null;
    if (!isAnonymousClass) {
        ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(requestArgVarExpr)).setExprReferenceExpr(requestsVarExpr).build());
    }
    ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("onNext").setArguments(Arrays.asList(CastExpr.builder().setType(protoMethod.outputType()).setExpr(localResponseVarExpr).build())).setExprReferenceExpr(responseObserverVarExpr).build());
    if (!isAnonymousClass) {
        ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("onCompleted").setExprReferenceExpr(responseObserverVarExpr).build());
    }
    TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class));
    // Constructs `response == null ? "" : response.getClass().getName()`.
    Expr actualResponseTypeString = TernaryExpr.builder().setConditionExpr(RelationalOperationExpr.equalToWithExprs(localResponseVarExpr, ValueExpr.createNullExpr())).setThenExpr(ValueExpr.withValue(StringObjectValue.withValue("null"))).setElseExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(localResponseVarExpr).setMethodName("getClass").build()).setMethodName("getName").setReturnType(TypeNode.STRING).build()).build();
    Function<TypeNode, Expr> typeToStrFn = t -> MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.builder().setStaticReferenceType(t).setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).build()).setMethodName("getName").setReturnType(TypeNode.STRING).build();
    Expr expectedResponseTypeOneString = typeToStrFn.apply(protoMethod.outputType());
    Expr expectedResponseTypeTwoString = typeToStrFn.apply(exceptionType);
    Expr newExceptionExpr = NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(IllegalArgumentException.class))).setArguments(// Operation.class.getName(), Exception.class.getName());
    MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("format").setArguments(ValueExpr.withValue(StringObjectValue.withValue("Unrecognized response type %s for method " + protoMethod.name() + ", expected %s or %s")), actualResponseTypeString, expectedResponseTypeOneString, expectedResponseTypeTwoString).build()).build();
    return IfStatement.builder().setConditionExpr(InstanceofExpr.builder().setExpr(localResponseVarExpr).setCheckType(protoMethod.outputType()).build()).setBody(ifBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).addElseIf(InstanceofExpr.builder().setExpr(localResponseVarExpr).setCheckType(exceptionType).build(), Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("onError").setArguments(Arrays.asList(CastExpr.builder().setType(exceptionType).setExpr(localResponseVarExpr).build())).setExprReferenceExpr(responseObserverVarExpr).build()))).setElseBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("onError").setExprReferenceExpr(responseObserverVarExpr).setArguments(Arrays.asList(newExceptionExpr)).build()))).build();
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Reference(com.google.api.generator.engine.ast.Reference) Operation(com.google.longrunning.Operation) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) StreamObserver(io.grpc.stub.StreamObserver) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) Stream(com.google.api.generator.gapic.model.Method.Stream) LinkedList(java.util.LinkedList) ClassComposer(com.google.api.generator.gapic.composer.common.ClassComposer) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AbstractMessage(com.google.protobuf.AbstractMessage) CastExpr(com.google.api.generator.engine.ast.CastExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) Collectors(java.util.stream.Collectors) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) GapicClass(com.google.api.generator.gapic.model.GapicClass) Statement(com.google.api.generator.engine.ast.Statement) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) Service(com.google.api.generator.gapic.model.Service) List(java.util.List) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) GapicContext(com.google.api.generator.gapic.model.GapicContext) Queue(java.util.Queue) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Aggregations

Expr (com.google.api.generator.engine.ast.Expr)151 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)130 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)126 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)124 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)114 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)96 TypeNode (com.google.api.generator.engine.ast.TypeNode)80 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)72 ArrayList (java.util.ArrayList)72 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)67 Statement (com.google.api.generator.engine.ast.Statement)64 CastExpr (com.google.api.generator.engine.ast.CastExpr)59 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)59 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)58 List (java.util.List)58 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)55 Variable (com.google.api.generator.engine.ast.Variable)54 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)51 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)49 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)47