Search in sources :

Example 11 with Sample

use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.

the class ServiceClientCallableMethodSampleComposer method composeStreamBidiSample.

private static Sample composeStreamBidiSample(Method method, VariableExpr clientVarExpr, AssignmentExpr requestAssignmentExpr) {
    List<Expr> bodyExprs = new ArrayList<>();
    // Create bidi stream variable expression and assign it with invoking client's bidi stream
    // method.
    // e.g. BidiStream<EchoRequest, EchoResponse> bidiStream = echoClient.chatCallable().call();
    TypeNode bidiStreamType = TypeNode.withReference(ConcreteReference.builder().setClazz(BidiStream.class).setGenerics(method.inputType().reference(), method.outputType().reference()).build());
    VariableExpr bidiStreamVarExpr = VariableExpr.withVariable(Variable.builder().setName("bidiStream").setType(bidiStreamType).build());
    MethodInvocationExpr clientBidiStreamCallMethodInvoationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sCallable", method.name()))).build();
    clientBidiStreamCallMethodInvoationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientBidiStreamCallMethodInvoationExpr).setMethodName("call").setReturnType(bidiStreamType).build();
    AssignmentExpr bidiStreamAssignmentExpr = AssignmentExpr.builder().setVariableExpr(bidiStreamVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientBidiStreamCallMethodInvoationExpr).build();
    bodyExprs.add(bidiStreamAssignmentExpr);
    // Add request with default value expression.
    bodyExprs.add(requestAssignmentExpr);
    // Invoke send method with argument request.
    // e.g. bidiStream.send(request);
    MethodInvocationExpr sendMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(bidiStreamVarExpr).setArguments(requestAssignmentExpr.variableExpr().toBuilder().setIsDecl(false).build()).setMethodName("send").build();
    bodyExprs.add(sendMethodInvocationExpr);
    List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    // For-loop on bidi stream variable.
    // e.g. for (EchoResponse response : bidiStream) {
    // // Do something when reveive a response.
    // }
    ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(VariableExpr.builder().setVariable(Variable.builder().setType(method.outputType()).setName("response").build()).setIsDecl(true).build()).setCollectionExpr(bidiStreamVarExpr).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when a response is received.")))).build();
    bodyStatements.add(forStatement);
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(bodyStatements).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) 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) BidiStream(com.google.api.gax.rpc.BidiStream) 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 12 with Sample

use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.

the class ServiceClientMethodSampleComposer method composeLroSample.

static Sample composeLroSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs) {
    // Assign response variable with invoking client's LRO method.
    // e.g. if return void, echoClient.waitAsync(ttl).get(); or,
    // e.g. if return other type, WaitResponse response = echoClient.waitAsync(ttl).get();
    MethodInvocationExpr invokeLroGetMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(String.format("%sAsync", JavaStyle.toLowerCamelCase(method.name()))).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
    invokeLroGetMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(invokeLroGetMethodExpr).setMethodName("get").setReturnType(method.lro().responseType()).build();
    boolean returnsVoid = method.lro().responseType().isProtoEmptyType();
    if (returnsVoid) {
        bodyExprs.add(invokeLroGetMethodExpr);
    } else {
        VariableExpr responseVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("response").setType(method.lro().responseType()).build()).setIsDecl(true).build();
        bodyExprs.add(AssignmentExpr.builder().setVariableExpr(responseVarExpr).setValueExpr(invokeLroGetMethodExpr).build());
    }
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setOverloadDisambiguation(SampleComposerUtil.createOverloadDisambiguation(rpcMethodArgVarExprs)).build();
    return Sample.builder().setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setRegionTag(regionTag).build();
}
Also used : Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) Method(com.google.api.generator.gapic.model.Method) Map(java.util.Map) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) LineComment(com.google.api.generator.engine.ast.LineComment) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) 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) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) 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) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) RegionTag(com.google.api.generator.gapic.model.RegionTag) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 13 with Sample

use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.

the class ServiceClientMethodSampleComposer method composePagedSample.

static Sample composePagedSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs, Map<String, Message> messageTypes) {
    // Find the repeated field.
    Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
    Preconditions.checkNotNull(methodOutputMessage, "Output message %s not found, keys: ", method.outputType().reference().fullName(), messageTypes.keySet().toString());
    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();
    // For loop paged response item on iterateAll method.
    // e.g. for (LogEntry element : loggingServiceV2Client.ListLogs(parent).iterateAll()) {
    // //doThingsWith(element);
    // }
    MethodInvocationExpr clientMethodIterateAllExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
    clientMethodIterateAllExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientMethodIterateAllExpr).setMethodName("iterateAll").setReturnType(repeatedResponseType).build();
    ForStatement loopIteratorStatement = ForStatement.builder().setLocalVariableExpr(VariableExpr.builder().setVariable(Variable.builder().setName("element").setType(repeatedResponseType).build()).setIsDecl(true).build()).setCollectionExpr(clientMethodIterateAllExpr).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("doThingsWith(element);")))).build();
    List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    bodyExprs.clear();
    bodyStatements.add(loopIteratorStatement);
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setOverloadDisambiguation(SampleComposerUtil.createOverloadDisambiguation(rpcMethodArgVarExprs)).build();
    return Sample.builder().setBody(bodyStatements).setRegionTag(regionTag).build();
}
Also used : Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) Method(com.google.api.generator.gapic.model.Method) Map(java.util.Map) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) LineComment(com.google.api.generator.engine.ast.LineComment) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) 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) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) 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) Field(com.google.api.generator.gapic.model.Field) Message(com.google.api.generator.gapic.model.Message) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) 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) TypeNode(com.google.api.generator.engine.ast.TypeNode) ForStatement(com.google.api.generator.engine.ast.ForStatement)

Example 14 with Sample

use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.

the class ServiceClientMethodSampleComposer method composeCanonicalSample.

public static Sample composeCanonicalSample(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());
    // Create request variable expression and assign with its default value.
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
    List<VariableExpr> rpcMethodArgVarExprs = Arrays.asList(requestVarExpr);
    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);
    List<Statement> bodyStatements = new ArrayList<>();
    RegionTag regionTag;
    if (method.isPaged()) {
        // e.g. echoClient.pagedExpand(request).iterateAll()
        Sample unaryPagedRpc = composePagedSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes);
        bodyStatements.addAll(unaryPagedRpc.body());
        regionTag = unaryPagedRpc.regionTag();
    } else if (method.hasLro()) {
        Sample unaryLroRpc = composeLroSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryLroRpc.body());
        regionTag = unaryLroRpc.regionTag();
    } else {
        // e.g. echoClient.echo(request)
        Sample unaryRpc = composeSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryRpc.body());
        regionTag = unaryRpc.regionTag();
    }
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    return Sample.builder().setBody(body).setRegionTag(regionTag).setIsCanonical(true).build();
}
Also used : Message(com.google.api.generator.gapic.model.Message) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) 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) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 15 with Sample

use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.

the class Writer method writeSamples.

private static void writeSamples(GapicClass gapicClazz, String pakkage, String clazzPath, JarOutputStream jos) {
    for (Sample sample : gapicClazz.samples()) {
        JarEntry jarEntry = new JarEntry(String.format("samples/snippets/generated/%s/%s/%s/%s.java", clazzPath, sample.regionTag().serviceName().toLowerCase(), sample.regionTag().rpcName().toLowerCase(), sample.name()));
        String executableSampleCode = SampleCodeWriter.writeExecutableSample(sample, pakkage);
        try {
            jos.putNextEntry(jarEntry);
            jos.write(executableSampleCode.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new GapicWriterException(String.format("Could not write sample code for %s/%s.: %s", clazzPath, sample.name(), e.getMessage()), e);
        }
    }
}
Also used : Sample(com.google.api.generator.gapic.model.Sample) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Aggregations

Sample (com.google.api.generator.gapic.model.Sample)34 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)19 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)17 TypeNode (com.google.api.generator.engine.ast.TypeNode)17 ArrayList (java.util.ArrayList)17 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)16 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)16 Statement (com.google.api.generator.engine.ast.Statement)16 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)14 Message (com.google.api.generator.gapic.model.Message)14 List (java.util.List)13 Expr (com.google.api.generator.engine.ast.Expr)12 RegionTag (com.google.api.generator.gapic.model.RegionTag)12 ResourceName (com.google.api.generator.gapic.model.ResourceName)11 Collectors (java.util.stream.Collectors)11 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)10 Variable (com.google.api.generator.engine.ast.Variable)10 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)10 Arrays (java.util.Arrays)10 Test (org.junit.Test)10