Skip to content

Commit

Permalink
fix(resnames): filter out mixin services from resname pkg candidates (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung committed Jun 4, 2021
1 parent eaf4592 commit 72fa76f
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 9 deletions.
Expand Up @@ -87,6 +87,9 @@ public class Parser {
"google.iam.v1.IAMPolicy",
"google.longrunning.Operations",
"google.cloud.location.Locations");
// These must be kept in sync with the above protos' java_package options.
private static final Set<String> MIXIN_JAVA_PACKAGE_ALLOWLIST =
ImmutableSet.of("com.google.iam.v1", "com.google.longrunning", "com.google.cloud.location");

// Allow other parsers to access this.
protected static final SourceCodeInfoParser SOURCE_CODE_INFO_PARSER = new SourceCodeInfoParser();
Expand Down Expand Up @@ -574,7 +577,7 @@ public static Map<String, Message> updateResourceNamesInMessages(
}

public static Map<String, ResourceName> parseResourceNames(CodeGeneratorRequest request) {
@VisibleForTesting String javaPackage = parseServiceJavaPackage(request);
String javaPackage = parseServiceJavaPackage(request);
Map<String, FileDescriptor> fileDescriptors = getFilesToGenerate(request);
Map<String, ResourceName> resourceNames = new HashMap<>();
for (String fileToGenerate : request.getFileToGenerateList()) {
Expand Down Expand Up @@ -917,8 +920,23 @@ private static String parseServiceJavaPackage(CodeGeneratorRequest request) {
}
}

// Filter out mixin packages.
Map<String, Integer> processedJavaPackageCount =
javaPackageCount.entrySet().stream()
.filter(e -> !MIXIN_JAVA_PACKAGE_ALLOWLIST.contains(e.getKey()))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

// An empty map indicates that only mixin packages were present, which means that we're
// generating a standalone client for a mixin.
if (processedJavaPackageCount.isEmpty()) {
processedJavaPackageCount = javaPackageCount;
}

String finalJavaPackage =
javaPackageCount.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();
processedJavaPackageCount.entrySet().stream()
.max(Map.Entry.comparingByValue())
.get()
.getKey();
Preconditions.checkState(
!Strings.isNullOrEmpty(finalJavaPackage), "No service Java package found");
return finalJavaPackage;
Expand Down
Expand Up @@ -31,7 +31,6 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -41,6 +40,7 @@
import com.google.pubsub.v1.GetSchemaRequest;
import com.google.pubsub.v1.ListSchemasRequest;
import com.google.pubsub.v1.ListSchemasResponse;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaName;
import com.google.pubsub.v1.ValidateMessageRequest;
Expand Down
Expand Up @@ -30,7 +30,6 @@
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.GetPolicyOptions;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -43,6 +42,7 @@
import com.google.pubsub.v1.GetSchemaRequest;
import com.google.pubsub.v1.ListSchemasRequest;
import com.google.pubsub.v1.ListSchemasResponse;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaName;
import com.google.pubsub.v1.ValidateMessageRequest;
Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -49,6 +48,7 @@
import com.google.pubsub.v1.ListSubscriptionsResponse;
import com.google.pubsub.v1.ModifyAckDeadlineRequest;
import com.google.pubsub.v1.ModifyPushConfigRequest;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.PushConfig;
Expand Down
Expand Up @@ -35,7 +35,6 @@
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.GetPolicyOptions;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -59,6 +58,7 @@
import com.google.pubsub.v1.ListSubscriptionsResponse;
import com.google.pubsub.v1.ModifyAckDeadlineRequest;
import com.google.pubsub.v1.ModifyPushConfigRequest;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.PushConfig;
Expand Down
Expand Up @@ -31,7 +31,6 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -46,6 +45,7 @@
import com.google.pubsub.v1.ListTopicSubscriptionsResponse;
import com.google.pubsub.v1.ListTopicsRequest;
import com.google.pubsub.v1.ListTopicsResponse;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.PublishRequest;
import com.google.pubsub.v1.PublishResponse;
import com.google.pubsub.v1.PubsubMessage;
Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.GetPolicyOptions;
import com.google.iam.v1.Policy;
import com.google.iam.v1.ProjectName;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
Expand All @@ -51,6 +50,7 @@
import com.google.pubsub.v1.ListTopicsRequest;
import com.google.pubsub.v1.ListTopicsResponse;
import com.google.pubsub.v1.MessageStoragePolicy;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.PublishRequest;
import com.google.pubsub.v1.PublishResponse;
import com.google.pubsub.v1.PubsubMessage;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.iam.v1;
package com.google.pubsub.v1;

import com.google.api.pathtemplate.PathTemplate;
import com.google.api.resourcenames.ResourceName;
Expand Down

0 comments on commit 72fa76f

Please sign in to comment.