Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][bazel] feat: add cloud/redis microgenerator rules #383

Merged
merged 14 commits into from
Oct 10, 2020
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ jobs:
name: Build targets for gapic-generator-java
command: |
cd /tmp/gapic-generator-java
bazel --batch build //...
bazel --batch build //src/...
- run:
name: Run unit tests for gapic-generator-java
command: |
cd /tmp/gapic-generator-java
bazel --batch test //... --noshow_progress
bazel --batch test //src/test/... --noshow_progress
find . -type f -regex ".*/bazel-testlogs/.*xml" -exec cp {} ${TEST_REPORTS_DIR} \;
- store_test_results:
path: bazel/reports/gapic-generator-java
Expand Down
4 changes: 2 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fi
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] || [ $NUM_BAZEL_FILES_CHANGED -gt 0 ]
then
echo_status "Checking the build..."
bazel --batch build --disk_cache="$BAZEL_CACHE_DIR" //...
bazel --batch build --disk_cache="$BAZEL_CACHE_DIR" //src/...
BUILD_STATUS=$?
if [ $BUILD_STATUS != 0 ]
then
Expand All @@ -112,7 +112,7 @@ fi
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
then
echo_status "Checking tests..."
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //...
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //src/test/...
TEST_STATUS=$?
if [ $TEST_STATUS != 0 ]
then
Expand Down
4 changes: 2 additions & 2 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def gapic_generator_java_repositories():
_maybe(
http_archive,
name = "com_google_googleapis",
strip_prefix = "googleapis-dbdd8ddeb6d9556952aa8784d9e48f2566c9911a",
strip_prefix = "googleapis-bda7ce951def5ae6e5c4258d0e569188dd4ae02b",
urls = [
"https://github.com/googleapis/googleapis/archive/dbdd8ddeb6d9556952aa8784d9e48f2566c9911a.zip",
"https://github.com/googleapis/googleapis/archive/bda7ce951def5ae6e5c4258d0e569188dd4ae02b.zip",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,14 @@ private static List<Expr> createPagingStaticAssignExprs(
Preconditions.checkState(
field != null,
String.format("Null field found for message %s", pagedResponseMessage.name()));
if (field.isRepeated()) {
if (field.isRepeated() && !field.isMap()) {
// Field is currently a List-type.
Preconditions.checkState(
!field.type().reference().generics().isEmpty(),
String.format("No generics found for field reference %s", field.type().reference()));
repeatedResponseType = TypeNode.withReference(field.type().reference().generics().get(0));
repeatedFieldName = field.name();
break;
}
}
Preconditions.checkNotNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
public class Parser {
private static final String COMMA = ",";
private static final String COLON = ":";
private static final String DOT = ".";
private static final String DEFAULT_PORT = "443";

// Allow other parsers to access this.
Expand Down Expand Up @@ -320,12 +321,23 @@ static LongrunningOperation parseLro(

OperationInfo lroInfo =
methodDescriptor.getOptions().getExtension(OperationsProto.operationInfo);

String responseTypeName = lroInfo.getResponseType();
String responseTypePackage = "";
if (responseTypeName.contains(DOT)) {
responseTypeName = responseTypeName.substring(responseTypeName.lastIndexOf(DOT) + 1);
}

String metadataTypeName = lroInfo.getMetadataType();
if (metadataTypeName.contains(DOT)) {
metadataTypeName = metadataTypeName.substring(metadataTypeName.lastIndexOf(DOT) + 1);
}

Message responseMessage = messageTypes.get(responseTypeName);
Message metadataMessage = messageTypes.get(metadataTypeName);
Preconditions.checkNotNull(
responseMessage, String.format("LRO response message %s not found", responseTypeName));
// TODO(miraleung): Check that the packages are equal if those strings are not empty.
Preconditions.checkNotNull(
metadataMessage, String.format("LRO metadata message %s not found", metadataTypeName));

Expand All @@ -352,9 +364,11 @@ static String sanitizeDefaultHost(String rawDefaultHost) {
}

private static List<Field> parseFields(Descriptor messageDescriptor) {
return messageDescriptor.getFields().stream()
.map(f -> parseField(f, messageDescriptor))
.collect(Collectors.toList());
List<FieldDescriptor> fields = new ArrayList<>(messageDescriptor.getFields());
// Sort by ascending field index order. This is important for paged responses, where the first
// repeated type is taken.
fields.sort((f1, f2) -> f1.getIndex() - f2.getIndex());
return fields.stream().map(f -> parseField(f, messageDescriptor)).collect(Collectors.toList());
}

private static Field parseField(FieldDescriptor fieldDescriptor, Descriptor messageDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ public void parseMethodSignatures_basic() {
ImmutableList.of(),
argument);

// Signature contents: ["name"], String variant.
methodArgs = methodSignatures.get(3);
assertEquals(1, methodArgs.size());
argument = methodArgs.get(0);
assertMethodArgumentEquals("name", TypeNode.STRING, ImmutableList.of(), argument);

// Signature contents: ["name"], resource helper variant.
methodArgs = methodSignatures.get(4);
methodArgs = methodSignatures.get(3);
assertEquals(1, methodArgs.size());
argument = methodArgs.get(0);
TypeNode foobarNameType =
TypeNode.withReference(
VaporReference.builder().setName("FoobarName").setPakkage(ECHO_PACKAGE).build());
assertMethodArgumentEquals("name", foobarNameType, ImmutableList.of(), argument);

// Signature contents: ["name"], String variant.
methodArgs = methodSignatures.get(4);
assertEquals(1, methodArgs.size());
argument = methodArgs.get(0);
assertMethodArgumentEquals("name", TypeNode.STRING, ImmutableList.of(), argument);
}

@Test
Expand Down
52 changes: 52 additions & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
load(
"@com_google_googleapis_imports//:imports.bzl",
"proto_library_with_info",
java_gapic_library = "java_gapic_library2",
)

package(default_visibility = ["//visibility:public"])

####################################################
# API Library Rules
####################################################
# These will eventually go away once more APIs in googleapis have been migrated to the microgenerator.

java_gapic_library(
name = "asset_java_gapic",
srcs = ["@com_google_googleapis//google/cloud/asset/v1:asset_proto_with_info"],
grpc_service_config = "@com_google_googleapis//google/cloud/asset/v1:cloudasset_grpc_service_config.json",
package = "google.cloud.asset.v1",
test_deps = [
"@com_google_googleapis//google/cloud/asset/v1:asset_java_grpc",
"@com_google_googleapis//google/iam/v1:iam_java_grpc",
],
deps = [
"@com_google_googleapis//google/cloud/asset/v1:asset_java_proto",
"@com_google_googleapis//google/iam/v1:iam_java_proto",
],
)

# Redis API.
# Redefine the proto_with_info target so we can include empty_proto.
# This should be removed post-migration.
proto_library_with_info(
name = "redis_proto_with_info",
deps = [
"@com_google_googleapis//google/cloud:common_resources_proto",
"@com_google_googleapis//google/cloud/redis/v1:redis_proto",
"@com_google_protobuf//:empty_proto",
],
)

java_gapic_library(
name = "redis_java_gapic",
srcs = ["redis_proto_with_info"],
grpc_service_config = "@com_google_googleapis//google/cloud/redis/v1:redis_grpc_service_config.json",
package = "google.cloud.redis.v1",
test_deps = [
"@com_google_googleapis//google/cloud/redis/v1:redis_java_grpc",
],
deps = [
"@com_google_googleapis//google/cloud/redis/v1:redis_java_proto",
],
)