Search in sources :

Example 31 with MethodInvocationExpr

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

the class ServiceClientHeaderSampleComposer method composeSetEndpointSample.

public static Sample composeSetEndpointSample(TypeNode clientType, TypeNode settingsType) {
    // Initialize client settings with builder() method.
    // e.g. EchoSettings echoSettings = EchoSettings.newBuilder().setEndpoint("myEndpoint").build();
    String settingsName = JavaStyle.toLowerCamelCase(settingsType.reference().name());
    String clientName = JavaStyle.toLowerCamelCase(clientType.reference().name());
    TypeNode myEndpointType = TypeNode.withReference(VaporReference.builder().setName("myEndpoint").setPakkage(clientType.reference().pakkage()).build());
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(settingsName).setType(settingsType).build());
    MethodInvocationExpr newBuilderMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
    MethodInvocationExpr credentialsMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderMethodExpr).setArguments(VariableExpr.withVariable(Variable.builder().setName("myEndpoint").setType(myEndpointType).build())).setMethodName("setEndpoint").build();
    MethodInvocationExpr buildMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(credentialsMethodExpr).setReturnType(settingsType).setMethodName("build").build();
    Expr initSettingsVarExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(buildMethodExpr).build();
    // Initialize client with create() method.
    // e.g. EchoClient echoClient = EchoClient.create(echoSettings);
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(clientName).setType(clientType).build());
    MethodInvocationExpr createMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(clientType).setArguments(settingsVarExpr).setMethodName("create").setReturnType(clientType).build();
    String rpcName = createMethodExpr.methodIdentifier().name();
    Expr initClientVarExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createMethodExpr).build();
    RegionTag regionTag = RegionTag.builder().setServiceName(clientName).setRpcName(rpcName).setOverloadDisambiguation("setEndpoint").build();
    List<Statement> sampleBody = Arrays.asList(ExprStatement.withExpr(initSettingsVarExpr), ExprStatement.withExpr(initClientVarExpr));
    return Sample.builder().setBody(sampleBody).setRegionTag(regionTag).build();
}
Also used : 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) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 32 with MethodInvocationExpr

use of com.google.api.generator.engine.ast.MethodInvocationExpr 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 33 with MethodInvocationExpr

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

the class ServiceClientMethodSampleComposer method composeSample.

static Sample composeSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs) {
    // Invoke current method based on return type.
    // e.g. if return void, echoClient.echo(..); or,
    // e.g. if return other type, EchoResponse response = echoClient.echo(...);
    boolean returnsVoid = method.outputType().isProtoEmptyType();
    MethodInvocationExpr clientRpcMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).setReturnType(method.outputType()).build();
    if (returnsVoid) {
        bodyExprs.add(clientRpcMethodInvocationExpr);
    } else {
        VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(method.outputType()).build());
        bodyExprs.add(AssignmentExpr.builder().setVariableExpr(responseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientRpcMethodInvocationExpr).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 : 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 34 with MethodInvocationExpr

use of com.google.api.generator.engine.ast.MethodInvocationExpr 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 35 with MethodInvocationExpr

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

the class HttpJsonServiceStubClassComposer method getRequestFormatterExpr.

private List<Expr> getRequestFormatterExpr(Method protoMethod) {
    BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMaker = getMethodMaker();
    MethodInvocationExpr expr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoMessageRequestFormatter.class.getSimpleName())).setMethodName("newBuilder").setGenerics(Collections.singletonList(protoMethod.inputType().reference())).build();
    TypeNode extractorVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
    expr = methodMaker.apply("setPath", Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(protoMethod.httpBindings().lowerCamelPattern())), createFieldsExtractorClassInstance(protoMethod, extractorVarType, protoMethod.httpBindings().pathParameters(), "putPathParam"))).apply(expr);
    TypeNode fieldsVarGenericType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(TypeNode.STRING.reference()).build());
    extractorVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(TypeNode.STRING.reference(), fieldsVarGenericType.reference()).build());
    expr = methodMaker.apply("setQueryParamsExtractor", Arrays.asList(createFieldsExtractorClassInstance(protoMethod, extractorVarType, protoMethod.httpBindings().queryParameters(), "putQueryParam"))).apply(expr);
    extractorVarType = TypeNode.STRING;
    boolean asteriskBody = protoMethod.httpBindings().isAsteriskBody();
    expr = methodMaker.apply("setRequestBodyExtractor", Arrays.asList(createBodyFieldsExtractorClassInstance(protoMethod, extractorVarType, asteriskBody ? protoMethod.httpBindings().pathParameters() : protoMethod.httpBindings().bodyParameters(), "toBody", asteriskBody))).apply(expr);
    expr = methodMaker.apply("build", Collections.emptyList()).apply(expr);
    return Collections.singletonList(expr);
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ProtoMessageRequestFormatter(com.google.api.gax.httpjson.ProtoMessageRequestFormatter) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TypeNode(com.google.api.generator.engine.ast.TypeNode) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap)

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