From 0ba86e9b99e5122edbf34f29387203636d7de369 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Tue, 29 Jun 2021 14:50:53 -0700 Subject: [PATCH] fix(resnames)!: Do not generate unused resname classes --- .../generator/gapic/protoparser/Parser.java | 30 +- .../iam/com/google/iam/v1/package-info.java | 2 +- .../v1/KeyManagementServiceClientTest.java | 18 +- .../google/cloud/kms/v1/PublicKeyName.java | 298 ------------------ .../cloud/logging/v2/ConfigClientTest.java | 22 +- 5 files changed, 17 insertions(+), 353 deletions(-) delete mode 100644 test/integration/goldens/kms/com/google/cloud/kms/v1/PublicKeyName.java diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java index 3344141c4e..8f5286e636 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java @@ -142,6 +142,9 @@ public static GapicContext parse(CodeGeneratorRequest request) { Map resourceNames = parseResourceNames(request); messages = updateResourceNamesInMessages(messages, resourceNames.values()); + // Contains only resource names that are actually used. That is, resource name definitions + // or references that are simply defined, but not used, will not have corresponding Java helper + // classes generated. Set outputArgResourceNames = new HashSet<>(); List mixinServices = new ArrayList<>(); Transport transport = Transport.parse(transportOpt.orElse(Transport.GRPC.toString())); @@ -157,33 +160,6 @@ public static GapicContext parse(CodeGeneratorRequest request) { transport); Preconditions.checkState(!services.isEmpty(), "No services found to generate"); - Function typeNameFn = - r -> r.resourceTypeString().substring(r.resourceTypeString().indexOf("/") + 1); - Function, Set> typeStringSetFn = - sr -> sr.stream().map(r -> typeNameFn.apply(r)).collect(Collectors.toSet()); - - // Include all resource names present in message types for backwards-compatibility with the - // monolith. In the future, this should be removed on a client library major semver update. - // Resolve type name collisions with the ones present in the method arguments. - final Set typeStringSet = typeStringSetFn.apply(outputArgResourceNames); - outputArgResourceNames.addAll( - resourceNames.values().stream() - .filter(r -> r.hasParentMessageName() && !typeStringSet.contains(typeNameFn.apply(r))) - .collect(Collectors.toSet())); - - String servicePackage = services.get(0).pakkage(); - Map patternsToResourceNames = - ResourceParserHelpers.createPatternResourceNameMap(resourceNames); - for (ResourceReference resourceReference : outputResourceReferencesSeen) { - final Set interimTypeStringSet = typeStringSetFn.apply(outputArgResourceNames); - outputArgResourceNames.addAll( - ResourceReferenceParser.parseResourceNames( - resourceReference, servicePackage, null, resourceNames, patternsToResourceNames) - .stream() - .filter(r -> !interimTypeStringSet.contains(typeNameFn.apply(r))) - .collect(Collectors.toSet())); - } - return GapicContext.builder() .setServices(services) .setMixinServices(mixinServices) diff --git a/test/integration/goldens/iam/com/google/iam/v1/package-info.java b/test/integration/goldens/iam/com/google/iam/v1/package-info.java index 136353c3e3..8f87748c4b 100644 --- a/test/integration/goldens/iam/com/google/iam/v1/package-info.java +++ b/test/integration/goldens/iam/com/google/iam/v1/package-info.java @@ -48,7 +48,7 @@ * try (IAMPolicyClient iAMPolicyClient = IAMPolicyClient.create()) { * SetIamPolicyRequest request = * SetIamPolicyRequest.newBuilder() - * .setResource("SetIamPolicyRequest1223629066".toString()) + * .setResource("resource-341064690") * .setPolicy(Policy.newBuilder().build()) * .build(); * Policy response = iAMPolicyClient.setIamPolicy(request); diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/KeyManagementServiceClientTest.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/KeyManagementServiceClientTest.java index 0b603417ae..29a0c79b67 100644 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/KeyManagementServiceClientTest.java +++ b/test/integration/goldens/kms/com/google/cloud/kms/v1/KeyManagementServiceClientTest.java @@ -752,14 +752,7 @@ public void getPublicKeyTest() throws Exception { PublicKey.newBuilder() .setPem("pem110872") .setPemCrc32C(Int64Value.newBuilder().build()) - .setName( - PublicKeyName.of( - "[PROJECT]", - "[LOCATION]", - "[KEY_RING]", - "[CRYPTO_KEY]", - "[CRYPTO_KEY_VERSION]") - .toString()) + .setName("name3373707") .build(); mockKeyManagementService.addResponse(expectedResponse); @@ -803,14 +796,7 @@ public void getPublicKeyTest2() throws Exception { PublicKey.newBuilder() .setPem("pem110872") .setPemCrc32C(Int64Value.newBuilder().build()) - .setName( - PublicKeyName.of( - "[PROJECT]", - "[LOCATION]", - "[KEY_RING]", - "[CRYPTO_KEY]", - "[CRYPTO_KEY_VERSION]") - .toString()) + .setName("name3373707") .build(); mockKeyManagementService.addResponse(expectedResponse); diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/PublicKeyName.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/PublicKeyName.java deleted file mode 100644 index 4a1e0a3031..0000000000 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/PublicKeyName.java +++ /dev/null @@ -1,298 +0,0 @@ -/* - * Copyright 2021 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.kms.v1; - -import com.google.api.pathtemplate.PathTemplate; -import com.google.api.resourcenames.ResourceName; -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import javax.annotation.Generated; - -// AUTO-GENERATED DOCUMENTATION AND CLASS. -@Generated("by gapic-generator-java") -public class PublicKeyName implements ResourceName { - private static final PathTemplate PROJECT_LOCATION_KEY_RING_CRYPTO_KEY_CRYPTO_KEY_VERSION = - PathTemplate.createWithoutUrlEncoding( - "projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}/cryptoKeyVersions/{crypto_key_version}/publicKey"); - private volatile Map fieldValuesMap; - private final String project; - private final String location; - private final String keyRing; - private final String cryptoKey; - private final String cryptoKeyVersion; - - @Deprecated - protected PublicKeyName() { - project = null; - location = null; - keyRing = null; - cryptoKey = null; - cryptoKeyVersion = null; - } - - private PublicKeyName(Builder builder) { - project = Preconditions.checkNotNull(builder.getProject()); - location = Preconditions.checkNotNull(builder.getLocation()); - keyRing = Preconditions.checkNotNull(builder.getKeyRing()); - cryptoKey = Preconditions.checkNotNull(builder.getCryptoKey()); - cryptoKeyVersion = Preconditions.checkNotNull(builder.getCryptoKeyVersion()); - } - - public String getProject() { - return project; - } - - public String getLocation() { - return location; - } - - public String getKeyRing() { - return keyRing; - } - - public String getCryptoKey() { - return cryptoKey; - } - - public String getCryptoKeyVersion() { - return cryptoKeyVersion; - } - - public static Builder newBuilder() { - return new Builder(); - } - - public Builder toBuilder() { - return new Builder(this); - } - - public static PublicKeyName of( - String project, String location, String keyRing, String cryptoKey, String cryptoKeyVersion) { - return newBuilder() - .setProject(project) - .setLocation(location) - .setKeyRing(keyRing) - .setCryptoKey(cryptoKey) - .setCryptoKeyVersion(cryptoKeyVersion) - .build(); - } - - public static String format( - String project, String location, String keyRing, String cryptoKey, String cryptoKeyVersion) { - return newBuilder() - .setProject(project) - .setLocation(location) - .setKeyRing(keyRing) - .setCryptoKey(cryptoKey) - .setCryptoKeyVersion(cryptoKeyVersion) - .build() - .toString(); - } - - public static PublicKeyName parse(String formattedString) { - if (formattedString.isEmpty()) { - return null; - } - Map matchMap = - PROJECT_LOCATION_KEY_RING_CRYPTO_KEY_CRYPTO_KEY_VERSION.validatedMatch( - formattedString, "PublicKeyName.parse: formattedString not in valid format"); - return of( - matchMap.get("project"), - matchMap.get("location"), - matchMap.get("key_ring"), - matchMap.get("crypto_key"), - matchMap.get("crypto_key_version")); - } - - public static List parseList(List formattedStrings) { - List list = new ArrayList<>(formattedStrings.size()); - for (String formattedString : formattedStrings) { - list.add(parse(formattedString)); - } - return list; - } - - public static List toStringList(List values) { - List list = new ArrayList<>(values.size()); - for (PublicKeyName value : values) { - if (value == null) { - list.add(""); - } else { - list.add(value.toString()); - } - } - return list; - } - - public static boolean isParsableFrom(String formattedString) { - return PROJECT_LOCATION_KEY_RING_CRYPTO_KEY_CRYPTO_KEY_VERSION.matches(formattedString); - } - - @Override - public Map getFieldValuesMap() { - if (fieldValuesMap == null) { - synchronized (this) { - if (fieldValuesMap == null) { - ImmutableMap.Builder fieldMapBuilder = ImmutableMap.builder(); - if (project != null) { - fieldMapBuilder.put("project", project); - } - if (location != null) { - fieldMapBuilder.put("location", location); - } - if (keyRing != null) { - fieldMapBuilder.put("key_ring", keyRing); - } - if (cryptoKey != null) { - fieldMapBuilder.put("crypto_key", cryptoKey); - } - if (cryptoKeyVersion != null) { - fieldMapBuilder.put("crypto_key_version", cryptoKeyVersion); - } - fieldValuesMap = fieldMapBuilder.build(); - } - } - } - return fieldValuesMap; - } - - public String getFieldValue(String fieldName) { - return getFieldValuesMap().get(fieldName); - } - - @Override - public String toString() { - return PROJECT_LOCATION_KEY_RING_CRYPTO_KEY_CRYPTO_KEY_VERSION.instantiate( - "project", - project, - "location", - location, - "key_ring", - keyRing, - "crypto_key", - cryptoKey, - "crypto_key_version", - cryptoKeyVersion); - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - if (o != null || getClass() == o.getClass()) { - PublicKeyName that = ((PublicKeyName) o); - return Objects.equals(this.project, that.project) - && Objects.equals(this.location, that.location) - && Objects.equals(this.keyRing, that.keyRing) - && Objects.equals(this.cryptoKey, that.cryptoKey) - && Objects.equals(this.cryptoKeyVersion, that.cryptoKeyVersion); - } - return false; - } - - @Override - public int hashCode() { - int h = 1; - h *= 1000003; - h ^= Objects.hashCode(project); - h *= 1000003; - h ^= Objects.hashCode(location); - h *= 1000003; - h ^= Objects.hashCode(keyRing); - h *= 1000003; - h ^= Objects.hashCode(cryptoKey); - h *= 1000003; - h ^= Objects.hashCode(cryptoKeyVersion); - return h; - } - - /** - * Builder for - * projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}/cryptoKeyVersions/{crypto_key_version}/publicKey. - */ - public static class Builder { - private String project; - private String location; - private String keyRing; - private String cryptoKey; - private String cryptoKeyVersion; - - protected Builder() {} - - public String getProject() { - return project; - } - - public String getLocation() { - return location; - } - - public String getKeyRing() { - return keyRing; - } - - public String getCryptoKey() { - return cryptoKey; - } - - public String getCryptoKeyVersion() { - return cryptoKeyVersion; - } - - public Builder setProject(String project) { - this.project = project; - return this; - } - - public Builder setLocation(String location) { - this.location = location; - return this; - } - - public Builder setKeyRing(String keyRing) { - this.keyRing = keyRing; - return this; - } - - public Builder setCryptoKey(String cryptoKey) { - this.cryptoKey = cryptoKey; - return this; - } - - public Builder setCryptoKeyVersion(String cryptoKeyVersion) { - this.cryptoKeyVersion = cryptoKeyVersion; - return this; - } - - private Builder(PublicKeyName publicKeyName) { - this.project = publicKeyName.project; - this.location = publicKeyName.location; - this.keyRing = publicKeyName.keyRing; - this.cryptoKey = publicKeyName.cryptoKey; - this.cryptoKeyVersion = publicKeyName.cryptoKeyVersion; - } - - public PublicKeyName build() { - return new PublicKeyName(this); - } - } -} diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/ConfigClientTest.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/ConfigClientTest.java index 22a872780c..f38bbc6eb5 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/ConfigClientTest.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/ConfigClientTest.java @@ -1107,7 +1107,7 @@ public void getSinkTest() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1154,7 +1154,7 @@ public void getSinkTest2() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1201,7 +1201,7 @@ public void createSinkTest() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1251,7 +1251,7 @@ public void createSinkTest2() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1301,7 +1301,7 @@ public void createSinkTest3() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1351,7 +1351,7 @@ public void createSinkTest4() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1401,7 +1401,7 @@ public void createSinkTest5() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1451,7 +1451,7 @@ public void updateSinkTest() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1501,7 +1501,7 @@ public void updateSinkTest2() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1551,7 +1551,7 @@ public void updateSinkTest3() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true) @@ -1604,7 +1604,7 @@ public void updateSinkTest4() throws Exception { LogSink expectedResponse = LogSink.newBuilder() .setName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString()) - .setDestination(FolderLocationName.of("[FOLDER]", "[LOCATION]").toString()) + .setDestination("destination-1429847026") .setFilter("filter-1274492040") .setDescription("description-1724546052") .setDisabled(true)