Skip to content

Commit

Permalink
disambiguate between operationsClient (grpc) and httpJsonOperationsCl…
Browse files Browse the repository at this point in the history
…ient
  • Loading branch information
vam-google committed Aug 25, 2021
1 parent 58daebb commit a7daee6
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 56 deletions.
Expand Up @@ -235,7 +235,9 @@ private List<Statement> createFieldDeclarations(
"settings", typeStore.get(ClassNames.getServiceSettingsClassName(service)));
fieldNameToTypes.put("stub", typeStore.get(ClassNames.getServiceStubClassName(service)));
if (hasLroClient) {
fieldNameToTypes.put("operationsClient", getTransportContext().operationsClientType());
fieldNameToTypes.put(
getTransportContext().operationsClientName(),
getTransportContext().operationsClientType());
}

return fieldNameToTypes.entrySet().stream()
Expand Down Expand Up @@ -377,7 +379,10 @@ private List<MethodDefinition> createConstructorMethods(
.build());
VariableExpr operationsClientVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(operationsClientType).setName("operationsClient").build());
Variable.builder()
.setType(operationsClientType)
.setName(getTransportContext().operationsClientName())
.build());

// Create the ServiceClient(ServiceSettings settings) ctor.
List<Expr> ctorAssignmentExprs = new ArrayList<>();
Expand Down Expand Up @@ -407,10 +412,15 @@ private List<MethodDefinition> createConstructorMethods(
.build())
.build());

String operationsStubGetterName =
String.format(
"get%s",
JavaStyle.toUpperCamelCase(getTransportContext().transportOperationsStubName()));

Expr clientArgExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
.setMethodName("getOperationsStub")
.setMethodName(operationsStubGetterName)
.build();
AssignmentExpr operationsClientAssignExpr =
AssignmentExpr.builder()
Expand Down Expand Up @@ -485,9 +495,13 @@ private List<MethodDefinition> createGetterMethods(
methodNameToTypes.put(
"getSettings", typeStore.get(ClassNames.getServiceSettingsClassName(service)));
methodNameToTypes.put("getStub", typeStore.get(ClassNames.getServiceStubClassName(service)));
String getOperationsClientMethodName = "getOperationsClient";
String getOperationsClientMethodName =
String.format(
"get%s",
JavaStyle.toUpperCamelCase(getTransportContext().operationsClientName()));
if (hasLroClient) {
methodNameToTypes.put(getOperationsClientMethodName, getTransportContext().operationsClientType());
methodNameToTypes.put(
getOperationsClientMethodName, getTransportContext().operationsClientType());
}
AnnotationNode betaStubAnnotation =
AnnotationNode.builder()
Expand Down
Expand Up @@ -112,7 +112,7 @@ private static List<TypeNode> createClassImplements(TypeStore typeStore) {

private List<MethodDefinition> createClassMethods(
Service service, Map<String, Message> messageTypes, TypeStore typeStore) {
boolean hasLroClient = hasLroMethods(service);
boolean hasLroClient = service.hasLroMethods();
List<MethodDefinition> methods = new ArrayList<>();
if (hasLroClient) {
methods.add(createOperationsStubGetter(typeStore));
Expand Down Expand Up @@ -207,7 +207,11 @@ private static MethodDefinition createCallableGetterHelper(
}

private MethodDefinition createOperationsStubGetter(TypeStore typeStore) {
String methodName = "getOperationsStub";
String methodName =
String.format(
"get%s",
JavaStyle.toUpperCamelCase(getTransportContext().transportOperationsStubName()));

return MethodDefinition.builder()
.setScope(ScopeNode.PUBLIC)
.setReturnType(getTransportContext().operationsStubType())
Expand All @@ -228,15 +232,6 @@ private static List<MethodDefinition> createBackgroundResourceMethodOverrides()
return Arrays.asList(closeMethod);
}

private static boolean hasLroMethods(Service service) {
for (Method method : service.methods()) {
if (method.hasLro()) {
return true;
}
}
return false;
}

private static TypeStore createTypes(Service service, Map<String, Message> messageTypes) {
List<Class> concreteClazzes =
Arrays.asList(
Expand Down
Expand Up @@ -61,6 +61,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -84,7 +85,7 @@ public abstract class AbstractTransportServiceStubClassComposer implements Class
private static final String CALLABLE_CLASS_MEMBER_PATTERN = "%sCallable";
private static final String OPERATION_CALLABLE_CLASS_MEMBER_PATTERN = "%sOperationCallable";
private static final String OPERATION_CALLABLE_NAME = "OperationCallable";
private static final String OPERATIONS_STUB_MEMBER_NAME = "operationsStub";
// private static final String OPERATIONS_STUB_MEMBER_NAME = "operationsStub";
private static final String PAGED_CALLABLE_NAME = "PagedCallable";

protected static final TypeStore FIXED_TYPESTORE = createStaticTypes();
Expand Down Expand Up @@ -143,12 +144,12 @@ public GapicClass generate(GapicContext context, Service service) {
.setName(BACKGROUND_RESOURCES_MEMBER_NAME)
.setType(FIXED_TYPESTORE.get("BackgroundResource"))
.build()));
if (getTransportContext().transportOperationsStubType() != null) {
if (generateOperationsStubLogic(service)) {
classMemberVarExprs.put(
OPERATIONS_STUB_MEMBER_NAME,
getTransportContext().transportOperationsStubName(),
VariableExpr.withVariable(
Variable.builder()
.setName(OPERATIONS_STUB_MEMBER_NAME)
.setName(getTransportContext().transportOperationsStubName())
.setType(getTransportContext().transportOperationsStubType())
.build()));
}
Expand Down Expand Up @@ -196,13 +197,26 @@ public GapicClass generate(GapicContext context, Service service) {
protected abstract Statement createMethodDescriptorVariableDecl(
Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr);

protected boolean generateOperationsStubLogic(Service service) {
return true;
}

protected List<MethodDefinition> createOperationsStubGetterMethod(
VariableExpr operationsStubVarExpr) {
Service service, VariableExpr operationsStubVarExpr) {
if (!generateOperationsStubLogic(service)) {
return Collections.emptyList();
}

String methodName =
String.format(
"get%s",
JavaStyle.toUpperCamelCase(getTransportContext().transportOperationsStubName()));

return Arrays.asList(
MethodDefinition.builder()
.setScope(ScopeNode.PUBLIC)
.setReturnType(operationsStubVarExpr.type())
.setName("getOperationsStub")
.setName(methodName)
.setReturnExpr(operationsStubVarExpr)
.build());
}
Expand Down Expand Up @@ -385,7 +399,8 @@ protected List<MethodDefinition> createClassMethods(
javaMethods.addAll(
createGetMethodDescriptorsMethod(service, typeStore, protoMethodNameToDescriptorVarExprs));
javaMethods.addAll(
createOperationsStubGetterMethod(classMemberVarExprs.get(OPERATIONS_STUB_MEMBER_NAME)));
createOperationsStubGetterMethod(
service, classMemberVarExprs.get(getTransportContext().transportOperationsStubName())));
javaMethods.addAll(createCallableGetterMethods(callableClassMemberVarExprs));
javaMethods.addAll(
createStubOverrideMethods(classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME)));
Expand Down Expand Up @@ -551,8 +566,8 @@ protected List<MethodDefinition> createConstructorMethods(
.build())
.setValueExpr(callableFactoryVarExpr)
.build());
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(OPERATIONS_STUB_MEMBER_NAME);
if (getTransportContext().transportOperationsStubType() != null) {
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(getTransportContext().transportOperationsStubName());
if (generateOperationsStubLogic(service)) {
secondCtorExprs.add(
AssignmentExpr.builder()
.setVariableExpr(
Expand Down Expand Up @@ -716,17 +731,18 @@ private Expr createCallableInitExpr(
clientContextVarExpr);
}

Optional<String> callableCreatorMethodName = getCallableCreatorMethodName(callableVarExpr.type());

Optional<String> callableCreatorMethodName =
getCallableCreatorMethodName(callableVarExpr.type());

Expr initExpr;
if (callableCreatorMethodName.isPresent()) {
initExpr = MethodInvocationExpr.builder()
.setExprReferenceExpr(callableFactoryVarExpr)
.setMethodName(callableCreatorMethodName.get())
.setArguments(creatorMethodArgVarExprs)
.setReturnType(callableVarExpr.type())
.build();
initExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(callableFactoryVarExpr)
.setMethodName(callableCreatorMethodName.get())
.setArguments(creatorMethodArgVarExprs)
.setReturnType(callableVarExpr.type())
.build();
} else {
initExpr = ValueExpr.createNullExpr();
}
Expand Down
Expand Up @@ -19,7 +19,6 @@
import com.google.api.generator.gapic.composer.utils.ClassNames;
import com.google.api.generator.gapic.model.Transport;
import com.google.auto.value.AutoValue;
import javax.annotation.Nullable;

@AutoValue
public abstract class TransportContext {
Expand All @@ -37,9 +36,10 @@ public abstract class TransportContext {

public abstract Class<?> methodDescriptorClass();

@Nullable
public abstract TypeNode transportOperationsStubType();

public abstract String transportOperationsStubName();

// For AbstractServiceSettingsClassComposer
public abstract Class<?> instantiatingChannelProviderClass();

Expand All @@ -66,6 +66,8 @@ public abstract class TransportContext {

public abstract TypeNode operationsClientType();

public abstract String operationsClientName();

protected static TypeNode classToType(Class<?> clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}
Expand Down Expand Up @@ -107,6 +109,8 @@ public abstract Builder setDefaultTransportProviderBuilderName(

public abstract Builder setTransportOperationsStubType(TypeNode transportOperationsStubType);

public abstract Builder setTransportOperationsStubName(String transportOperationsStubName);

public abstract Builder setOperationsStubType(TypeNode operationsStubType);

public abstract Builder setOperationResponseTransformerType(TypeNode operationResponseTransformerType);
Expand All @@ -115,6 +119,8 @@ public abstract Builder setDefaultTransportProviderBuilderName(

public abstract Builder setOperationsClientType(TypeNode operationsClientType);

public abstract Builder setOperationsClientName(String operationsClientName);

public abstract TransportContext build();
}
}
Expand Up @@ -39,6 +39,7 @@ public abstract class GrpcContext extends TransportContext {
.setStubCallableFactoryType(classToType(GrpcStubCallableFactory.class))
.setMethodDescriptorClass(MethodDescriptor.class)
.setTransportOperationsStubType(classToType(GrpcOperationsStub.class))
.setTransportOperationsStubName("operationsStub")
// For grpc.ServiceSettingsClassComposer
.setInstantiatingChannelProviderClass(InstantiatingGrpcChannelProvider.Builder.class)
.setDefaultTransportProviderBuilderName("defaultGrpcTransportProviderBuilder")
Expand All @@ -57,6 +58,7 @@ public abstract class GrpcContext extends TransportContext {
classToType(ProtoOperationTransformers.MetadataTransformer.class))
// For ServiceClientClassComposer
.setOperationsClientType(classToType(OperationsClient.class))
.setOperationsClientName("operationsClient")
.build();

public static TransportContext instance() {
Expand Down
Expand Up @@ -90,6 +90,11 @@ private static TypeStore createStaticTypes() {
ProtoRestSerializer.class));
}

@Override
protected boolean generateOperationsStubLogic(Service service) {
return service.hasLroMethods();
}

@Override
protected Statement createMethodDescriptorVariableDecl(
Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr) {
Expand Down
Expand Up @@ -39,6 +39,7 @@ public abstract class RestContext extends TransportContext {
.setStubCallableFactoryType(classToType(HttpJsonStubCallableFactory.class))
.setMethodDescriptorClass(ApiMethodDescriptor.class)
.setTransportOperationsStubType(classToType(HttpJsonOperationsStub.class))
.setTransportOperationsStubName("httpJsonOperationsStub")
// For httpjson.ServiceSettingsClassComposer
.setInstantiatingChannelProviderClass(InstantiatingHttpJsonChannelProvider.Builder.class)
.setDefaultTransportProviderBuilderName("defaultHttpJsonTransportProviderBuilder")
Expand All @@ -58,6 +59,7 @@ public abstract class RestContext extends TransportContext {
classToType(ProtoOperationTransformers.MetadataTransformer.class))
// For ServiceClientClassComposer
.setOperationsClientType(classToType(OperationsClient.class))
.setOperationsClientName("httpJsonOperationsClient")
.build();

public static TransportContext instance() {
Expand Down
Expand Up @@ -49,6 +49,15 @@ public boolean hasDescription() {
return !Strings.isNullOrEmpty(description());
}

public boolean hasLroMethods() {
for (Method method : methods()) {
if (method.hasLro()) {
return true;
}
}
return false;
}

public abstract Builder toBuilder();

public static Builder builder() {
Expand Down
Expand Up @@ -11,7 +11,6 @@ import com.google.api.gax.httpjson.HttpJsonStubCallableFactory;
import com.google.api.gax.httpjson.ProtoMessageRequestFormatter;
import com.google.api.gax.httpjson.ProtoMessageResponseParser;
import com.google.api.gax.httpjson.ProtoRestSerializer;
import com.google.api.gax.httpjson.longrunning.stub.HttpJsonOperationsStub;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.showcase.v1beta1.RepeatRequest;
Expand Down Expand Up @@ -273,7 +272,6 @@ public class HttpJsonComplianceStub extends ComplianceStub {
private final UnaryCallable<RepeatRequest, RepeatResponse> repeatDataPathTrailingResourceCallable;

private final BackgroundResource backgroundResources;
private final HttpJsonOperationsStub operationsStub;
private final HttpJsonStubCallableFactory callableFactory;

public static final HttpJsonComplianceStub create(ComplianceStubSettings settings)
Expand Down Expand Up @@ -313,7 +311,6 @@ public class HttpJsonComplianceStub extends ComplianceStub {
HttpJsonStubCallableFactory callableFactory)
throws IOException {
this.callableFactory = callableFactory;
this.operationsStub = HttpJsonOperationsStub.create(clientContext, callableFactory);

HttpJsonCallSettings<RepeatRequest, RepeatResponse> repeatDataBodyTransportSettings =
HttpJsonCallSettings.<RepeatRequest, RepeatResponse>newBuilder()
Expand Down Expand Up @@ -384,10 +381,6 @@ public class HttpJsonComplianceStub extends ComplianceStub {
return methodDescriptors;
}

public HttpJsonOperationsStub getOperationsStub() {
return operationsStub;
}

@Override
public UnaryCallable<RepeatRequest, RepeatResponse> repeatDataBodyCallable() {
return repeatDataBodyCallable;
Expand Down
Expand Up @@ -30,7 +30,6 @@
import com.google.api.gax.httpjson.ProtoMessageRequestFormatter;
import com.google.api.gax.httpjson.ProtoMessageResponseParser;
import com.google.api.gax.httpjson.ProtoRestSerializer;
import com.google.api.gax.httpjson.longrunning.stub.HttpJsonOperationsStub;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.cloud.compute.v1.AddressAggregatedList;
Expand Down Expand Up @@ -228,7 +227,6 @@ public class HttpJsonAddressesStub extends AddressesStub {
private final UnaryCallable<ListAddressesRequest, ListPagedResponse> listPagedCallable;

private final BackgroundResource backgroundResources;
private final HttpJsonOperationsStub operationsStub;
private final HttpJsonStubCallableFactory callableFactory;

public static final HttpJsonAddressesStub create(AddressesStubSettings settings)
Expand Down Expand Up @@ -267,7 +265,6 @@ protected HttpJsonAddressesStub(
HttpJsonStubCallableFactory callableFactory)
throws IOException {
this.callableFactory = callableFactory;
this.operationsStub = HttpJsonOperationsStub.create(clientContext, callableFactory);

HttpJsonCallSettings<AggregatedListAddressesRequest, AddressAggregatedList>
aggregatedListTransportSettings =
Expand Down Expand Up @@ -320,10 +317,6 @@ public static List<ApiMethodDescriptor> getMethodDescriptors() {
return methodDescriptors;
}

public HttpJsonOperationsStub getOperationsStub() {
return operationsStub;
}

@Override
public UnaryCallable<AggregatedListAddressesRequest, AddressAggregatedList>
aggregatedListCallable() {
Expand Down

0 comments on commit a7daee6

Please sign in to comment.