Search in sources :

Example 71 with MethodInvocationExpr

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

the class ServiceClientCallableMethodSampleComposer method composeUnaryOrLroCallableSample.

private static Sample composeUnaryOrLroCallableSample(Method method, VariableExpr clientVarExpr, VariableExpr requestVarExpr) {
    List<Statement> bodyStatements = new ArrayList<>();
    // Create api future variable expression, and assign it with a value by invoking callable
    // method.
    // e.g. ApiFuture<EchoResponse> future = echoClient.echoCallable().futureCall(request);
    TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(method.hasLro() ? ConcreteReference.withClazz(Operation.class) : method.outputType().reference()).build());
    VariableExpr apiFutureVarExpr = VariableExpr.withVariable(Variable.builder().setName("future").setType(apiFutureType).build());
    MethodInvocationExpr callableMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sCallable", method.name()))).build();
    callableMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(callableMethodInvocationExpr).setMethodName("futureCall").setArguments(requestVarExpr).setReturnType(apiFutureType).build();
    AssignmentExpr futureAssignmentExpr = AssignmentExpr.builder().setVariableExpr(apiFutureVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(callableMethodInvocationExpr).build();
    bodyStatements.add(ExprStatement.withExpr(futureAssignmentExpr));
    bodyStatements.add(CommentStatement.withComment(LineComment.withComment("Do something.")));
    MethodInvocationExpr getMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(apiFutureVarExpr).setMethodName("get").setReturnType(method.outputType()).build();
    TypeNode methodOutputType = method.hasLro() ? method.lro().responseType() : method.outputType();
    boolean returnsVoid = methodOutputType.isProtoEmptyType();
    if (returnsVoid) {
        bodyStatements.add(ExprStatement.withExpr(getMethodInvocationExpr));
    } else {
        VariableExpr responseVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(method.outputType()).setName("response").build()).setIsDecl(true).build();
        AssignmentExpr responseAssignmentExpr = AssignmentExpr.builder().setVariableExpr(responseVarExpr).setValueExpr(getMethodInvocationExpr).build();
        bodyStatements.add(ExprStatement.withExpr(responseAssignmentExpr));
    }
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(bodyStatements).setRegionTag(regionTag).setIsCanonical(true).build();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 72 with MethodInvocationExpr

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

the class ServiceClientCallableMethodSampleComposer method composeStreamClientSample.

private static Sample composeStreamClientSample(Method method, VariableExpr clientVarExpr, AssignmentExpr requestAssignmentExpr) {
    List<Expr> bodyExprs = new ArrayList<>();
    // Create responseObserver variable expression.
    // e.g. ApiStream<EchoResponse> responseObserver
    TypeNode responseObserverType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiStreamObserver.class).setGenerics(method.inputType().reference()).build());
    VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("responseObserver").setType(responseObserverType).build());
    // Create an anonymous class for ApiStreamObserver that contains the methods onNext, onError,
    // and onCompleted.
    MethodDefinition onNextMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onNext").setArguments(VariableExpr.builder().setVariable(Variable.builder().setName("response").setType(method.outputType()).build()).setIsDecl(true).build()).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when a response is received.")))).setReturnType(TypeNode.VOID).build();
    MethodDefinition onErrorMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onError").setArguments(VariableExpr.builder().setVariable(Variable.builder().setName("t").setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build()).setIsDecl(true).build()).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Add error-handling")))).setReturnType(TypeNode.VOID).build();
    MethodDefinition onCompletedMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onCompleted").setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when complete.")))).setReturnType(TypeNode.VOID).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(responseObserverType).setMethods(onNextMethod, onErrorMethod, onCompletedMethod).build();
    AssignmentExpr responseObserverAssignmentExpr = AssignmentExpr.builder().setVariableExpr(responseObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(anonymousClassExpr).build();
    bodyExprs.add(responseObserverAssignmentExpr);
    // Create an assignment expression for request observer variable expression when invoking the
    // client's streaming call.
    // e.g. ApiStreamObserver<EchoRequest> requestObserver =
    // echoClient.collectCallable().clientStreamingCall(responseObserver);
    TypeNode requestObserverType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiStreamObserver.class).setGenerics(method.inputType().reference()).build());
    VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("requestObserver").setType(responseObserverType).build());
    MethodInvocationExpr clientStreamCallMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).build();
    clientStreamCallMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientStreamCallMethodInvocationExpr).setArguments(responseObserverVarExpr).setMethodName("clientStreamingCall").setReturnType(requestObserverType).build();
    AssignmentExpr requestObserverAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientStreamCallMethodInvocationExpr).build();
    bodyExprs.add(requestObserverAssignmentExpr);
    // Add assignment expression of request with its default value.
    bodyExprs.add(requestAssignmentExpr);
    // Invoke onNext method with argument of request variable.
    // e.g. requestObserver.onNext(request)
    MethodInvocationExpr onNextMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestObserverVarExpr).setMethodName("onNext").setArguments(requestAssignmentExpr.variableExpr().toBuilder().setIsDecl(false).build()).build();
    bodyExprs.add(onNextMethodExpr);
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setRegionTag(regionTag).build();
}
Also used : Expr(com.google.api.generator.engine.ast.Expr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) 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) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) RegionTag(com.google.api.generator.gapic.model.RegionTag) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Example 73 with MethodInvocationExpr

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

the class ServiceClientCallableMethodSampleComposer method composePagedCallableMethod.

// Compose sample code for the method where it is CallableMethodKind.PAGED.
public static Sample composePagedCallableMethod(Method method, TypeNode clientType, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
    // Assign method's request variable with the default value.
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
    Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
    Preconditions.checkNotNull(requestMessage, String.format("Could not find the message type %s.", method.inputType().reference().fullName()));
    Expr requestBuilderExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
    AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(requestBuilderExpr).build();
    List<Expr> bodyExprs = new ArrayList<>();
    bodyExprs.add(requestAssignmentExpr);
    // Find the repeated field.
    Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
    Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapPaginatedRepeatedField();
    Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated field found on message %s for method %s", methodOutputMessage.name(), method.name()));
    TypeNode repeatedResponseType = repeatedPagedResultsField.type();
    // Create ApiFuture Variable Expression with assign value by invoking client paged callable
    // method.
    // e.g. ApiFuture<ListExclusionsPagedResponse> future =
    // configServiceV2Client.listExclusionsPagedCallable().futureCall(request);
    TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(repeatedResponseType.reference()).build());
    VariableExpr apiFutureVarExpr = VariableExpr.withVariable(Variable.builder().setName("future").setType(apiFutureType).build());
    MethodInvocationExpr pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(String.format("%sPagedCallable", JavaStyle.toLowerCamelCase(method.name()))).build();
    pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedCallableFutureMethodExpr).setMethodName("futureCall").setArguments(requestVarExpr).setReturnType(apiFutureType).build();
    AssignmentExpr apiFutureAssignmentExpr = AssignmentExpr.builder().setVariableExpr(apiFutureVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(pagedCallableFutureMethodExpr).build();
    bodyExprs.add(apiFutureAssignmentExpr);
    List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    bodyExprs.clear();
    // Add line comment
    bodyStatements.add(CommentStatement.withComment(LineComment.withComment("Do something.")));
    // For-loop on repeated response element
    // e.g. for (ListExclusionsResponse element : future.get().iterateAll()) {
    // // doThingsWith(element);
    // }
    VariableExpr repeatedResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("element").setType(repeatedResponseType).build());
    MethodInvocationExpr futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(apiFutureVarExpr).setMethodName("get").build();
    futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(futureGetIterateAllMethodExpr).setMethodName("iterateAll").setReturnType(repeatedResponseType).build();
    CommentStatement lineCommentStatement = CommentStatement.withComment(LineComment.withComment("doThingsWith(element);"));
    ForStatement repeatedResponseForStatement = ForStatement.builder().setLocalVariableExpr(repeatedResponseVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(futureGetIterateAllMethodExpr).setBody(Arrays.asList(lineCommentStatement)).build();
    bodyStatements.add(repeatedResponseForStatement);
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    RegionTag regionTag = RegionTag.builder().setServiceName(clientType.reference().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) OperationFuture(com.google.api.gax.longrunning.OperationFuture) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) Operation(com.google.longrunning.Operation) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Strings(com.google.common.base.Strings) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) Method(com.google.api.generator.gapic.model.Method) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ServerStream(com.google.api.gax.rpc.ServerStream) LineComment(com.google.api.generator.engine.ast.LineComment) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ApiFuture(com.google.api.core.ApiFuture) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) BidiStream(com.google.api.gax.rpc.BidiStream) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) Message(com.google.api.generator.gapic.model.Message) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Message(com.google.api.generator.gapic.model.Message) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Field(com.google.api.generator.gapic.model.Field) ApiFuture(com.google.api.core.ApiFuture) Expr(com.google.api.generator.engine.ast.Expr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ForStatement(com.google.api.generator.engine.ast.ForStatement)

Example 74 with MethodInvocationExpr

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

the class HttpJsonServiceStubClassComposer method createTransportSettingsInitExpr.

@Override
protected Expr createTransportSettingsInitExpr(Method method, VariableExpr transportSettingsVarExpr, VariableExpr methodDescriptorVarExpr, List<Statement> classStatements) {
    MethodInvocationExpr callSettingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_REST_TYPESTORE.get(HttpJsonCallSettings.class.getSimpleName())).setGenerics(transportSettingsVarExpr.type().reference().generics()).setMethodName("newBuilder").build();
    callSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(callSettingsBuilderExpr).setMethodName("setMethodDescriptor").setArguments(Arrays.asList(methodDescriptorVarExpr)).build();
    callSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(callSettingsBuilderExpr).setMethodName("setTypeRegistry").setArguments(Arrays.asList(TYPE_REGISTRY_VAR_EXPR)).build();
    callSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(callSettingsBuilderExpr).setMethodName("build").setReturnType(transportSettingsVarExpr.type()).build();
    return AssignmentExpr.builder().setVariableExpr(transportSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(callSettingsBuilderExpr).build();
}
Also used : HttpJsonCallSettings(com.google.api.gax.httpjson.HttpJsonCallSettings) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr)

Example 75 with MethodInvocationExpr

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

the class HttpJsonServiceStubClassComposer method createFieldsExtractorClassInstance.

private Expr createFieldsExtractorClassInstance(Method method, TypeNode extractorReturnType, Set<HttpBinding> httpBindingFieldNames, String serializerMethodName) {
    List<Statement> bodyStatements = new ArrayList<>();
    VariableExpr fieldsVarExpr = VariableExpr.withVariable(Variable.builder().setName("fields").setType(extractorReturnType).build());
    Expr fieldsAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_REST_TYPESTORE.get(HashMap.class.getSimpleName())).setIsGeneric(true).build()).build();
    bodyStatements.add(ExprStatement.withExpr(fieldsAssignExpr));
    TypeNode serializerVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ProtoRestSerializer.class).setGenerics(method.inputType().reference()).build());
    VariableExpr serializerVarExpr = VariableExpr.withVariable(Variable.builder().setName("serializer").setType(serializerVarType).build());
    Expr serializerAssignExpr = AssignmentExpr.builder().setVariableExpr(serializerVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoRestSerializer.class.getSimpleName())).setMethodName("create").setReturnType(serializerVarType).build()).build();
    bodyStatements.add(ExprStatement.withExpr(serializerAssignExpr));
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    for (HttpBinding httpBindingFieldName : httpBindingFieldNames) {
        // Handle foo.bar cases by descending into the subfields.
        MethodInvocationExpr.Builder requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
        MethodInvocationExpr.Builder requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
        String[] descendantFields = httpBindingFieldName.name().split("\\.");
        for (int i = 0; i < descendantFields.length; i++) {
            String currFieldName = descendantFields[i];
            String bindingFieldMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
            requestFieldGetterExprBuilder = requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
            String bindingFieldHasMethodName = (i < descendantFields.length - 1) ? bindingFieldMethodName : String.format("has%s", JavaStyle.toUpperCamelCase(currFieldName));
            requestFieldHasExprBuilder = requestFieldHasExprBuilder.setMethodName(bindingFieldHasMethodName).setReturnType(TypeNode.BOOLEAN);
            if (i < descendantFields.length - 1) {
                requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldGetterExprBuilder.build());
                requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldHasExprBuilder.build());
            }
        }
        MethodInvocationExpr requestBuilderExpr = requestFieldGetterExprBuilder.build();
        MethodInvocationExpr requestHasExpr = requestFieldHasExprBuilder.build();
        ImmutableList.Builder<Expr> paramsPutArgs = ImmutableList.builder();
        paramsPutArgs.add(fieldsVarExpr);
        paramsPutArgs.add(ValueExpr.withValue(StringObjectValue.withValue(JavaStyle.toLowerCamelCase(httpBindingFieldName.name()))));
        paramsPutArgs.add(requestBuilderExpr);
        Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serializerVarExpr).setMethodName(serializerMethodName).setArguments(paramsPutArgs.build()).setReturnType(extractorReturnType).build();
        if (httpBindingFieldName.isOptional()) {
            bodyStatements.add(IfStatement.builder().setConditionExpr(requestHasExpr).setBody(Arrays.asList(ExprStatement.withExpr(paramsPutExpr))).build());
        } else {
            bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
        }
    }
    // (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
    return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(fieldsVarExpr).build();
}
Also used : ProtoRestSerializer(com.google.api.gax.httpjson.ProtoRestSerializer) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Aggregations

MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)103 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)76 TypeNode (com.google.api.generator.engine.ast.TypeNode)72 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)59 Expr (com.google.api.generator.engine.ast.Expr)50 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)48 ArrayList (java.util.ArrayList)41 Variable (com.google.api.generator.engine.ast.Variable)39 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)38 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)36 Statement (com.google.api.generator.engine.ast.Statement)33 List (java.util.List)31 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)29 Test (org.junit.Test)29 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)27 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)26 Arrays (java.util.Arrays)26 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)25 Map (java.util.Map)25 Function (java.util.function.Function)24