Search in sources :

Example 1 with VaporReference

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

the class HttpJsonServiceCallableFactoryClassComposer method createOperationCallableMethod.

@Override
protected MethodDefinition createOperationCallableMethod(Service service, TypeStore typeStore) {
    String methodVariantName = "Operation";
    String requestTemplateName = "RequestT";
    String responseTemplateName = "ResponseT";
    List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName, "MetadataT");
    // Always add @BetaApi annotation to the generated createOperationCallable() method for now,
    // until LRO is fully implemented.
    // 
    // Remove the @BetaApi annotation once the LRO feature is fully implemented and stabilized.
    AnnotationNode betaAnnotation = AnnotationNode.withTypeAndDescription(typeStore.get("BetaApi"), "The surface for long-running operations is not stable yet and may change in the" + " future.");
    // Generate generic method without the body
    TypeNode operationType = service.operationType();
    if (operationType == null) {
        operationType = DEFAULT_OPERATION_TYPE;
    }
    MethodDefinition method = createGenericCallableMethod(service, typeStore, /*methodTemplateNames=*/
    methodTemplateNames, /*returnCallableKindName=*/
    methodVariantName, /*returnCallableTemplateNames=*/
    methodTemplateNames, /*methodVariantName=*/
    methodVariantName, /*httpJsonCallSettingsTemplateObjects=*/
    Arrays.asList(requestTemplateName, operationType), /*callSettingsVariantName=*/
    methodVariantName, /*callSettingsTemplateObjects=*/
    methodTemplateNames.stream().map(n -> (Object) n).collect(Collectors.toList()), Arrays.asList(betaAnnotation));
    List<Statement> createOperationCallableBody = new ArrayList<>();
    List<VariableExpr> arguments = new ArrayList<>(method.arguments());
    Variable httpJsonCallSettingsVar = arguments.get(0).variable();
    Variable operationCallSettingsVar = arguments.get(1).variable();
    Variable clientContextVar = arguments.get(2).variable();
    Variable operationsStubVar = arguments.get(3).variable();
    // Generate innerCallable
    VariableExpr innerCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("innerCallable").setType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build()).setTemplateObjects(Arrays.asList(requestTemplateName, methodVariantName)).build();
    MethodInvocationExpr getInitialCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationCallSettingsVar)).setMethodName("getInitialCallSettings").build();
    MethodInvocationExpr createBaseUnaryCallableExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createBaseUnaryCallable").setArguments(VariableExpr.withVariable(httpJsonCallSettingsVar), getInitialCallSettingsExpr, VariableExpr.withVariable(clientContextVar)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build();
    AssignmentExpr innerCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(innerCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createBaseUnaryCallableExpr).build();
    createOperationCallableBody.add(ExprStatement.withExpr(innerCallableAssignExpr));
    // This is a temporary solution
    VaporReference requestT = VaporReference.builder().setName("RequestT").setPakkage(service.pakkage() + ".stub").build();
    TypeNode initialCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
    // Generate initialCallable
    VariableExpr initialCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("initialCallable").setType(initialCallableType).build()).build();
    MethodInvocationExpr getMethodDescriptorExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(httpJsonCallSettingsVar)).setMethodName("getMethodDescriptor").build();
    MethodInvocationExpr getOperationSnapshotFactoryExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getMethodDescriptorExpr).setMethodName("getOperationSnapshotFactory").build();
    TypeNode operationSnapshotCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
    NewObjectExpr initialCallableObject = NewObjectExpr.builder().setType(operationSnapshotCallableType).setIsGeneric(true).setArguments(innerCallableVarExpr, getOperationSnapshotFactoryExpr).build();
    AssignmentExpr initialCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(initialCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(initialCallableObject).build();
    createOperationCallableBody.add(ExprStatement.withExpr(initialCallableAssignExpr));
    // Generate return statement
    MethodInvocationExpr longRunningClient = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationsStubVar)).setMethodName("longRunningClient").build();
    MethodInvocationExpr createOperationCallable = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createOperationCallable").setArguments(VariableExpr.withVariable(operationCallSettingsVar), VariableExpr.withVariable(clientContextVar), longRunningClient, initialCallableVarExpr).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(OperationCallable.class))).build();
    // Add body and return statement to method
    return method.toBuilder().setBody(createOperationCallableBody).setReturnExpr(createOperationCallable).build();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) OperationCallable(com.google.api.gax.rpc.OperationCallable) ArrayList(java.util.ArrayList) VaporReference(com.google.api.generator.engine.ast.VaporReference) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) HttpJsonOperationSnapshotCallable(com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 2 with VaporReference

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

the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.

@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
    ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
    Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
    VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
    assignmentExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) Test(org.junit.Test)

Example 3 with VaporReference

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

the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_withArgs.

@Test
public void writeReferenceConstructorExprImports_withArgs() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    VariableExpr streamVarExpr = VariableExpr.builder().setVariable(createVariable("stream", TypeNode.withReference(ConcreteReference.withClazz(LongStream.class)))).build();
    ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.thisBuilder().setArguments(Arrays.asList(streamVarExpr)).setType(classType).build();
    referenceConstructorExpr.accept(writerVisitor);
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) Test(org.junit.Test)

Example 4 with VaporReference

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

the class ImportWriterVisitorTest method writeSuperObjectValueImports.

@Test
public void writeSuperObjectValueImports() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build();
    TypeNode typeNode = TypeNode.withReference(ref);
    SuperObjectValue superObjectValue = SuperObjectValue.withType(typeNode);
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
    methodExpr.accept(writerVisitor);
    assertEquals("import com.google.example.examples.v1.Student;\n\n", writerVisitor.write());
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) Test(org.junit.Test)

Example 5 with VaporReference

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

the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_basic.

@Test
public void writeReferenceConstructorExprImports_basic() {
    VaporReference ref = VaporReference.builder().setName("Parent").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.superBuilder().setType(classType).build();
    referenceConstructorExpr.accept(writerVisitor);
    assertEquals("import com.google.example.v1.Parent;\n\n", writerVisitor.write());
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) Test(org.junit.Test)

Aggregations

VaporReference (com.google.api.generator.engine.ast.VaporReference)11 TypeNode (com.google.api.generator.engine.ast.TypeNode)10 Test (org.junit.Test)9 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)7 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)5 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)3 Variable (com.google.api.generator.engine.ast.Variable)3 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)2 SuperObjectValue (com.google.api.generator.engine.ast.SuperObjectValue)2 HttpJsonOperationSnapshotCallable (com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable)1 OperationCallable (com.google.api.gax.rpc.OperationCallable)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)1 Statement (com.google.api.generator.engine.ast.Statement)1 ThisObjectValue (com.google.api.generator.engine.ast.ThisObjectValue)1