Skip to content

Commit

Permalink
chore: do minor code simplification (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Nov 11, 2021
1 parent d1bb7bd commit a1fe20f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 96 deletions.
6 changes: 2 additions & 4 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_gapic//:gapic.bzl", "proto_custom_library", "unzipped_srcjar")
load("@rules_gapic//:gapic.bzl", "proto_custom_library")

NO_GRPC_CONFIG_ALLOWLIST = ["library"]

Expand Down Expand Up @@ -129,7 +129,6 @@ def _java_gapic_srcjar(
# Can be used to provide a java_library with a customized generator,
# like the one which dumps descriptor to a file for future debugging.
java_generator_name = "java_gapic",
output_suffix = ".srcjar",
**kwargs):
file_args_dict = {}

Expand All @@ -146,7 +145,6 @@ def _java_gapic_srcjar(
if service_yaml:
file_args_dict[service_yaml] = "api-service-config"

output_suffix = ".srcjar"
opt_args = []

if transport:
Expand All @@ -164,7 +162,7 @@ def _java_gapic_srcjar(
plugin_file_args = {},
opt_file_args = file_args_dict,
output_type = java_generator_name,
output_suffix = output_suffix,
output_suffix = ".srcjar",
opt_args = opt_args,
**kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.StreamingCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
Expand Down Expand Up @@ -58,7 +54,6 @@
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.MethodArgument;
import com.google.api.generator.gapic.model.OperationResponse;
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
Expand Down Expand Up @@ -913,46 +908,6 @@ private void addDynamicTypes(GapicContext context, Service service, TypeStore ty
}
}

private static TypeNode getCallSettingsTypeHelper(
Method protoMethod, TypeStore typeStore, boolean isBuilder) {
Class callSettingsClazz = isBuilder ? UnaryCallSettings.Builder.class : UnaryCallSettings.class;
if (protoMethod.isPaged()) {
callSettingsClazz = isBuilder ? PagedCallSettings.Builder.class : PagedCallSettings.class;
} else {
switch (protoMethod.stream()) {
case CLIENT:
// Fall through.
case BIDI:
callSettingsClazz =
isBuilder ? StreamingCallSettings.Builder.class : StreamingCallSettings.class;
break;
case SERVER:
callSettingsClazz =
isBuilder
? ServerStreamingCallSettings.Builder.class
: ServerStreamingCallSettings.class;
break;
case NONE:
// Fall through
default:
// Fall through
}
}

List<Reference> generics = new ArrayList<>();
generics.add(protoMethod.inputType().reference());
generics.add(protoMethod.outputType().reference());
if (protoMethod.isPaged()) {
generics.add(
typeStore
.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, protoMethod.name()))
.reference());
}

return TypeNode.withReference(
ConcreteReference.builder().setClazz(callSettingsClazz).setGenerics(generics).build());
}

protected static TypeNode getCallableType(Method protoMethod) {
Preconditions.checkState(
!protoMethod.stream().equals(Method.Stream.NONE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.ThrowExpr;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.composer.comment.StubCommentComposer;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.composer.utils.ClassNames;
Expand All @@ -57,7 +54,6 @@
import javax.annotation.Generated;

public abstract class AbstractServiceStubClassComposer implements ClassComposer {
private static final String DOT = ".";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";

private final TransportContext transportContext;
Expand Down Expand Up @@ -126,7 +122,7 @@ private List<MethodDefinition> createClassMethods(

if (service.operationPollingMethod() != null) {
methods.addAll(createLongRunningClientGetters(typeStore));
}
}
methods.addAll(createCallableGetters(service, messageTypes, typeStore));
methods.addAll(createBackgroundResourceMethodOverrides());
return methods;
Expand All @@ -149,8 +145,7 @@ private List<MethodDefinition> createCallableGetters(
return javaMethods;
}

private MethodDefinition createOperationCallableGetter(
Method method, TypeStore typeStore) {
private MethodDefinition createOperationCallableGetter(Method method, TypeStore typeStore) {
return createCallableGetterHelper(method, typeStore, true, false);
}

Expand Down Expand Up @@ -210,7 +205,8 @@ private MethodDefinition createCallableGetterHelper(
return createCallableGetterMethodDefinition(returnType, methodName, annotations, typeStore);
}

private List<MethodDefinition> createOperationsStubGetters(TypeStore typeStore, TypeNode operationsStubType) {
private List<MethodDefinition> createOperationsStubGetters(
TypeStore typeStore, TypeNode operationsStubType) {
List<MethodDefinition> getters = new ArrayList<>();

Iterator<String> operationStubNameIt =
Expand All @@ -220,24 +216,27 @@ private List<MethodDefinition> createOperationsStubGetters(TypeStore typeStore,
while (operationStubNameIt.hasNext() && operationStubTypeIt.hasNext()) {
String methodName =
String.format("get%s", JavaStyle.toUpperCamelCase(operationStubNameIt.next()));
//TODO: refactor this
// TODO: refactor this
TypeNode actualOperationsStubType = operationStubTypeIt.next();
if (operationsStubType != null) {
actualOperationsStubType = operationsStubType;
}

getters.add(createOperationsStubGetterMethodDefinition(actualOperationsStubType, methodName, typeStore));
getters.add(
createOperationsStubGetterMethodDefinition(
actualOperationsStubType, methodName, typeStore));
}

return getters;
}

private List<MethodDefinition> createLongRunningClientGetters(TypeStore typeStore) {
return ImmutableList.of(createCallableGetterMethodDefinition(
TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class)),
"longRunningClient",
ImmutableList.of(AnnotationNode.withType(typeStore.get("BetaApi"))),
typeStore));
return ImmutableList.of(
createCallableGetterMethodDefinition(
TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class)),
"longRunningClient",
ImmutableList.of(AnnotationNode.withType(typeStore.get("BetaApi"))),
typeStore));
}

private static List<MethodDefinition> createBackgroundResourceMethodOverrides() {
Expand Down Expand Up @@ -283,7 +282,10 @@ private static TypeStore createTypes(Service service, Map<String, Message> messa
}

protected MethodDefinition createCallableGetterMethodDefinition(
TypeNode returnType, String methodName, List<AnnotationNode> annotations, TypeStore typeStore) {
TypeNode returnType,
String methodName,
List<AnnotationNode> annotations,
TypeStore typeStore) {
return MethodDefinition.builder()
.setScope(ScopeNode.PUBLIC)
.setAnnotations(annotations)
Expand Down Expand Up @@ -314,8 +316,4 @@ protected MethodDefinition createOperationsStubGetterMethodDefinition(
.build())))
.build();
}

private static String getClientClassName(Service service) {
return String.format("%sClient", service.overriddenName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand Down Expand Up @@ -534,7 +533,7 @@ private List<Expr> setOperationSnapshotFactoryExpr(
if (!protoMethod.isOperationPollingMethod()) {
// TODO: Change to ordered map
Map<String, String> requestFields = inputOperationMessage.operationRequestFields();
List<String> fieldAnnotationNames = new ArrayList<String>(requestFields.keySet());
List<String> fieldAnnotationNames = new ArrayList<>(requestFields.keySet());
Collections.sort(fieldAnnotationNames);
for (String fieldName : fieldAnnotationNames) {
createBody.add(appendField(opNameVarExpr, requestVarExpr, requestFields.get(fieldName)));
Expand Down Expand Up @@ -644,7 +643,7 @@ private List<Expr> setPollingRequestFactoryExpr(
Message inputOperationMessage =
messageTypes.get(protoMethod.inputType().reference().fullName());

List<Statement> createBody = new ArrayList<Statement>(1);
List<Statement> createBody = new ArrayList<>(1);

// Generate input variables for create
VariableExpr compoundOperationIdVarExpr =
Expand Down Expand Up @@ -699,11 +698,11 @@ private List<Expr> setPollingRequestFactoryExpr(
.setMethodName("newBuilder")
.build();
BiMap<String, String> responseFieldsMap = inputOperationMessage.operationResponseFields();
List<String> responseFieldAnnotationNames = new ArrayList<String>(responseFieldsMap.keySet());
List<String> responseFieldAnnotationNames = new ArrayList<>(responseFieldsMap.keySet());
Collections.sort(responseFieldAnnotationNames);
Set<String> responseFieldsNames = responseFieldsMap.inverse().keySet();
Set<String> allFieldsNames = inputOperationMessage.fieldMap().keySet();
ArrayList<String> nonResponseFieldsNames = new ArrayList<String>();
ArrayList<String> nonResponseFieldsNames = new ArrayList<>();
for (String fieldName : allFieldsNames) {
if (!responseFieldsNames.contains(fieldName)) {
nonResponseFieldsNames.add(fieldName);
Expand Down Expand Up @@ -1092,28 +1091,6 @@ protected VariableExpr declareLongRunningClient() {
.build());
}

protected Optional<String> getCallableCreatorMethodName(TypeNode callableVarExprType) {
final String typeName = callableVarExprType.reference().name();
String streamName = "Unary";

// Special handling for pagination methods.
if (callableVarExprType.reference().generics().size() == 2
&& callableVarExprType.reference().generics().get(1).name().endsWith("PagedResponse")) {
streamName = "Paged";
} else {
if (typeName.startsWith("Client")) {
return Optional.empty(); // not supported in REST transport
} else if (typeName.startsWith("Server")) {
return Optional.empty(); // not supported in REST transport (for now)
} else if (typeName.startsWith("Bidi")) {
return Optional.empty(); // not supported in REST transport
} else if (typeName.startsWith("Operation")) {
streamName = "Operation";
}
}
return Optional.of(String.format("create%sCallable", streamName));
}

@Override
protected List<Statement> createTypeRegistry(Service service) {
TypeNode typeRegistryType = FIXED_REST_TYPESTORE.get(TypeRegistry.class.getSimpleName());
Expand Down

0 comments on commit a1fe20f

Please sign in to comment.