Search in sources :

Example 51 with Expr

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

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

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

the class HttpJsonServiceStubClassComposer method createOperationsStubInitExpr.

@Override
protected List<Expr> createOperationsStubInitExpr(Service service, Expr thisExpr, VariableExpr operationsStubClassVarExpr, VariableExpr clientContextVarExpr, VariableExpr callableFactoryVarExpr) {
    TypeNode operationsStubType = getTransportOperationsStubType(service);
    String standardOpStub = HttpJsonOperationsStub.class.getName();
    List<Expr> arguments = new ArrayList<>(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr));
    if (standardOpStub.equals(operationsStubType.reference().fullName())) {
        arguments.add(TYPE_REGISTRY_VAR_EXPR);
    }
    return Collections.singletonList(AssignmentExpr.builder().setVariableExpr(operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(operationsStubType).setMethodName("create").setArguments(arguments).setReturnType(operationsStubClassVarExpr.type()).build()).build());
}
Also used : 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) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 54 with Expr

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

the class ServiceClientTestClassComposer method createRpcExceptionTestMethod.

@Override
protected MethodDefinition createRpcExceptionTestMethod(Method method, Service service, List<MethodArgument> methodSignature, int variantIndex, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr exceptionVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_REST_TYPESTORE.get("ApiException")).setName("exception").build());
    // First two assignment lines.
    Expr exceptionAssignExpr = AssignmentExpr.builder().setVariableExpr(exceptionVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("createException").setStaticReferenceType(FIXED_REST_TYPESTORE.get("ApiExceptionFactory")).setArguments(NewObjectExpr.withType(FIXED_REST_TYPESTORE.get("Exception")), MethodInvocationExpr.builder().setMethodName("of").setStaticReferenceType(FIXED_REST_TYPESTORE.get("FakeStatusCode")).setArguments(EnumRefExpr.builder().setName("INVALID_ARGUMENT").setType(FIXED_REST_TYPESTORE.get("Code")).build()).build(), ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build())).setReturnType(exceptionVarExpr.type()).build()).build();
    Expr addExceptionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("addException").setArguments(exceptionVarExpr).build();
    // Try-catch block. Build the method call.
    String exceptionTestMethodName = String.format("%sExceptionTest%s", JavaStyle.toLowerCamelCase(method.name()), variantIndex > 0 ? variantIndex + 1 : "");
    boolean isStreaming = !method.stream().equals(Method.Stream.NONE);
    List<Statement> methodBody = new ArrayList<>();
    methodBody.add(ExprStatement.withExpr(exceptionAssignExpr));
    methodBody.add(ExprStatement.withExpr(addExceptionExpr));
    if (isStreaming) {
        methodBody.addAll(createStreamingRpcExceptionTestStatements(method, classMemberVarExprs, resourceNames, messageTypes));
    } else {
        methodBody.addAll(createRpcExceptionTestStatements(method, methodSignature, classMemberVarExprs, resourceNames, messageTypes));
    }
    return MethodDefinition.builder().setAnnotations(Arrays.asList(TEST_ANNOTATION)).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(exceptionTestMethodName).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))).setBody(methodBody).build();
}
Also used : 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 55 with Expr

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

the class ServiceClientTestClassComposer method createStartStaticServerMethod.

@Override
protected MethodDefinition createStartStaticServerMethod(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore, String newBuilderMethod) {
    VariableExpr mockServiceVarExpr = classMemberVarExprs.get(MOCK_SERVICE_VAR_NAME);
    VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
    TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
    TypeNode transportStubType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
    Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
    Expr initMockServiceExpr = AssignmentExpr.builder().setVariableExpr(mockServiceVarExpr).setValueExpr(NewObjectExpr.builder().setType(mockServiceVarExpr.type()).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(transportStubType).setMethodName("getMethodDescriptors").build(), MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("getDefaultEndpoint").build()).build()).build();
    VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
    Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName(newBuilderMethod).build();
    Expr transportChannelProviderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("defaultHttpJsonTransportProviderBuilder").build();
    transportChannelProviderExpr = methodBuilderFn.apply(transportChannelProviderExpr).apply("setHttpTransport", mockServiceVarExpr);
    transportChannelProviderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(transportChannelProviderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
    settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", transportChannelProviderExpr);
    settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
    settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
    Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
    Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
    return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("BeforeClass")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("startStaticServer").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setIsStatic(true).setBody(Arrays.asList(initMockServiceExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
Also used : Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) GaxHttpJsonProperties(com.google.api.gax.httpjson.GaxHttpJsonProperties) FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ApiExceptionFactory(com.google.api.gax.rpc.ApiExceptionFactory) BiFunction(java.util.function.BiFunction) AbstractServiceClientTestClassComposer(com.google.api.generator.gapic.composer.common.AbstractServiceClientTestClassComposer) ApiMethodDescriptor(com.google.api.gax.httpjson.ApiMethodDescriptor) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) LinkedHashMap(java.util.LinkedHashMap) Expr(com.google.api.generator.engine.ast.Expr) 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) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ImmutableList(com.google.common.collect.ImmutableList) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ApiClientHeaderProvider(com.google.api.gax.rpc.ApiClientHeaderProvider) MockHttpService(com.google.api.gax.httpjson.testing.MockHttpService) 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) ApiException(com.google.api.gax.rpc.ApiException) ResourceName(com.google.api.generator.gapic.model.ResourceName) 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) Code(com.google.api.gax.rpc.StatusCode.Code) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) GapicContext(com.google.api.generator.gapic.model.GapicContext) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) BiFunction(java.util.function.BiFunction) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) 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