Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 3fdf7d0

Browse files
authored
Revert "refactor: expose parsed api short name and version as fields in Service (#1075)"
This reverts commit 2ebe948.
1 parent cd8253d commit 3fdf7d0

12 files changed

Lines changed: 106 additions & 238 deletions

src/main/java/com/google/api/generator/gapic/composer/Composer.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import com.google.api.generator.gapic.model.Service;
3838
import com.google.api.generator.gapic.model.Transport;
3939
import com.google.common.annotations.VisibleForTesting;
40+
import com.google.common.base.Splitter;
41+
import com.google.common.collect.Iterables;
4042
import java.util.ArrayList;
4143
import java.util.Arrays;
4244
import java.util.List;
@@ -48,7 +50,8 @@ public static List<GapicClass> composeServiceClasses(GapicContext context) {
4850
clazzes.addAll(generateServiceClasses(context));
4951
clazzes.addAll(generateMockClasses(context, context.mixinServices()));
5052
clazzes.addAll(generateResourceNameHelperClasses(context));
51-
return addApacheLicense(prepareExecutableSamples(clazzes));
53+
return addApacheLicense(
54+
prepareExecutableSamples(clazzes, context.gapicMetadata().getProtoPackage()));
5255
}
5356

5457
public static GapicPackageInfo composePackageInfo(GapicContext context) {
@@ -190,7 +193,16 @@ public static List<GapicClass> generateTestClasses(GapicContext context) {
190193
}
191194

192195
@VisibleForTesting
193-
static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes) {
196+
static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes, String protoPackage) {
197+
// parse protoPackage for apiVersion
198+
String[] pakkage = protoPackage.split("\\.");
199+
String apiVersion;
200+
// e.g. v1, v2, v1beta1
201+
if (pakkage[pakkage.length - 1].matches("v[0-9].*")) {
202+
apiVersion = pakkage[pakkage.length - 1];
203+
} else {
204+
apiVersion = "";
205+
}
194206
// Include license header, apiShortName, and apiVersion
195207
List<GapicClass> clazzesWithSamples = new ArrayList<>();
196208
clazzes.forEach(
@@ -202,12 +214,31 @@ static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes) {
202214
sample ->
203215
samples.add(
204216
addRegionTagAndHeaderToSample(
205-
sample, gapicClass.apiShortName(), gapicClass.apiVersion())));
217+
sample, parseDefaultHost(gapicClass.defaultHost()), apiVersion)));
206218
clazzesWithSamples.add(gapicClass.withSamples(samples));
207219
});
208220
return clazzesWithSamples;
209221
}
210222

223+
// Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default
224+
// endpoints like
225+
// "us-east1-pubsub.googleapis.com".
226+
@VisibleForTesting
227+
protected static String parseDefaultHost(String defaultHost) {
228+
// If the defaultHost is of the format "**.googleapis.com", take the name before the first
229+
// period.
230+
String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost);
231+
// If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the
232+
// first period and after the last dash to follow CSharp's implementation here:
233+
// https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70
234+
apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost);
235+
// `iam-meta-api` service is an exceptional case and is handled as a one-off
236+
if (defaultHost.contains("iam-meta-api")) {
237+
apiShortName = "iam";
238+
}
239+
return apiShortName;
240+
}
241+
211242
@VisibleForTesting
212243
protected static Sample addRegionTagAndHeaderToSample(
213244
Sample sample, String apiShortName, String apiVersion) {

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ public GapicClass generate(GapicContext context, Service service) {
164164

165165
updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames);
166166
return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples))
167-
.withApiShortName(service.apiShortName())
168-
.withApiVersion(service.apiVersion());
167+
.withDefaultHost(service.defaultHost());
169168
}
170169

171170
private static List<AnnotationNode> createClassAnnotations(Service service, TypeStore typeStore) {

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ public GapicClass generate(GapicContext context, Service service) {
127127
.setNestedClasses(Arrays.asList(createNestedBuilderClass(service, typeStore)))
128128
.build();
129129
return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples))
130-
.withApiShortName(service.apiShortName())
131-
.withApiVersion(service.apiVersion());
130+
.withDefaultHost(service.defaultHost());
132131
}
133132

134133
private static List<CommentStatement> createClassHeaderComments(

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ public GapicClass generate(GapicContext context, Service service) {
202202
.build();
203203
return GapicClass.create(
204204
GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples))
205-
.withApiShortName(service.apiShortName())
206-
.withApiVersion(service.apiVersion());
205+
.withDefaultHost(service.defaultHost());
207206
}
208207

209208
protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() {

src/main/java/com/google/api/generator/gapic/model/GapicClass.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ public enum Kind {
3535

3636
public abstract List<Sample> samples();
3737

38-
// Only used for generating the region tag for samples; therefore only used in select Composers.
39-
public abstract String apiShortName();
40-
41-
// Only used for generating the region tag for samples; therefore only used in select Composers.
42-
public abstract String apiVersion();
38+
// Represents the host URL for the service. May or may not contain a regional endpoint. Only used
39+
// for generating the region tag for samples; therefore only used in select Composers.
40+
public abstract String defaultHost();
4341

4442
public static GapicClass create(Kind kind, ClassDefinition classDefinition) {
4543
return builder().setKind(kind).setClassDefinition(classDefinition).build();
@@ -53,8 +51,7 @@ public static GapicClass create(
5351
static Builder builder() {
5452
return new AutoValue_GapicClass.Builder()
5553
.setSamples(Collections.emptyList())
56-
.setApiShortName("")
57-
.setApiVersion("");
54+
.setDefaultHost("");
5855
}
5956

6057
abstract Builder toBuilder();
@@ -63,12 +60,8 @@ public final GapicClass withSamples(List<Sample> samples) {
6360
return toBuilder().setSamples(samples).build();
6461
}
6562

66-
public final GapicClass withApiShortName(String apiShortName) {
67-
return toBuilder().setApiShortName(apiShortName).build();
68-
}
69-
70-
public final GapicClass withApiVersion(String apiVersion) {
71-
return toBuilder().setApiVersion(apiVersion).build();
63+
public final GapicClass withDefaultHost(String defaultHost) {
64+
return toBuilder().setDefaultHost(defaultHost).build();
7265
}
7366

7467
@AutoValue.Builder
@@ -79,9 +72,7 @@ abstract static class Builder {
7972

8073
abstract Builder setSamples(List<Sample> samples);
8174

82-
abstract Builder setApiShortName(String apiShortName);
83-
84-
abstract Builder setApiVersion(String apiVersion);
75+
abstract Builder setDefaultHost(String defaultHost);
8576

8677
abstract GapicClass build();
8778
}

src/main/java/com/google/api/generator/gapic/model/Service.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
import com.google.api.generator.engine.ast.TypeNode;
1818
import com.google.auto.value.AutoValue;
19-
import com.google.common.base.Splitter;
2019
import com.google.common.base.Strings;
2120
import com.google.common.collect.ImmutableList;
22-
import com.google.common.collect.Iterables;
2321
import java.util.List;
2422
import javax.annotation.Nullable;
2523

@@ -52,20 +50,6 @@ public boolean hasDescription() {
5250
return !Strings.isNullOrEmpty(description());
5351
}
5452

55-
public String apiShortName() {
56-
if (!Strings.isNullOrEmpty(defaultHost())) {
57-
return parseApiShortName(defaultHost());
58-
}
59-
return "";
60-
}
61-
62-
public String apiVersion() {
63-
if (!Strings.isNullOrEmpty(protoPakkage())) {
64-
return parseApiVersion(protoPakkage());
65-
}
66-
return "";
67-
}
68-
6953
public Method operationPollingMethod() {
7054
for (Method method : methods()) {
7155
if (method.isOperationPollingMethod()) {
@@ -143,35 +127,4 @@ public abstract static class Builder {
143127

144128
public abstract Service build();
145129
}
146-
147-
private static String parseApiVersion(String protoPackage) {
148-
// parse protoPackage for apiVersion
149-
String[] pakkage = protoPackage.split("\\.");
150-
String apiVersion;
151-
// e.g. v1, v2, v1beta1
152-
if (pakkage[pakkage.length - 1].matches("v[0-9].*")) {
153-
apiVersion = pakkage[pakkage.length - 1];
154-
} else {
155-
apiVersion = "";
156-
}
157-
return apiVersion;
158-
}
159-
160-
// Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default
161-
// endpoints like
162-
// "us-east1-pubsub.googleapis.com".
163-
private static String parseApiShortName(String defaultHost) {
164-
// If the defaultHost is of the format "**.googleapis.com", take the name before the first
165-
// period.
166-
String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost);
167-
// If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the
168-
// first period and after the last dash to follow CSharp's implementation here:
169-
// https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70
170-
apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost);
171-
// `iam-meta-api` service is an exceptional case and is handled as a one-off
172-
if (defaultHost.contains("iam-meta-api")) {
173-
apiShortName = "iam";
174-
}
175-
return apiShortName;
176-
}
177130
}

src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ public class ComposerTest {
4242
private final Service echoProtoService = context.services().get(0);
4343
private final List<GapicClass> clazzes =
4444
Arrays.asList(
45-
GrpcServiceCallableFactoryClassComposer.instance()
46-
.generate(context, echoProtoService)
47-
.withApiShortName(echoProtoService.apiShortName())
48-
.withApiVersion(echoProtoService.apiVersion()));
45+
GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService));
4946
private final Sample sample =
5047
Sample.builder()
5148
.setRegionTag(
5249
RegionTag.builder().setServiceName("serviceName").setRpcName("rpcName").build())
5350
.build();
5451
private List<Sample> ListofSamples = Arrays.asList(new Sample[] {sample});
52+
private final String protoPackage = echoProtoService.protoPakkage();
5553

5654
@Test
5755
public void gapicClass_addApacheLicense() {
@@ -77,22 +75,47 @@ public void composeSamples_showcase() {
7775
List<GapicClass> testClassList = Arrays.asList(new GapicClass[] {testClass});
7876

7977
List<Sample> composedSamples =
80-
Composer.prepareExecutableSamples(testClassList).get(0).samples();
78+
Composer.prepareExecutableSamples(testClassList, protoPackage).get(0).samples();
8179

8280
assertFalse(composedSamples.isEmpty());
8381
for (Sample sample : composedSamples) {
8482
assertEquals(
8583
"File header should be APACHE",
8684
Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT),
8785
sample.fileHeader());
88-
assertEquals(
89-
"ApiShortName should be Localhost7469",
90-
"Localhost7469",
91-
sample.regionTag().apiShortName());
92-
assertEquals("ApiVersion should be V1Beta1", "V1Beta1", sample.regionTag().apiVersion());
86+
assertEquals("ApiShortName should be empty", "", sample.regionTag().apiShortName());
87+
assertEquals("ApiVersion should be V1beta1", "V1Beta1", sample.regionTag().apiVersion());
9388
}
9489
}
9590

91+
@Test
92+
public void parseDefaultHost_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() {
93+
String defaultHost = "us-east1-pubsub.googleapis.com";
94+
String apiShortName = Composer.parseDefaultHost(defaultHost);
95+
assertEquals("pubsub", apiShortName);
96+
}
97+
98+
@Test
99+
public void parseDefaultHost_shouldReturnApiShortName() {
100+
String defaultHost = "logging.googleapis.com";
101+
String apiShortName = Composer.parseDefaultHost(defaultHost);
102+
assertEquals("logging", apiShortName);
103+
}
104+
105+
@Test
106+
public void parseDefaultHost_shouldReturnApiShortNameForIam() {
107+
String defaultHost = "iam-meta-api.googleapis.com";
108+
String apiShortName = Composer.parseDefaultHost(defaultHost);
109+
assertEquals("iam", apiShortName);
110+
}
111+
112+
@Test
113+
public void parseDefaultHost_shouldReturnHostIfNoPeriods() {
114+
String defaultHost = "logging:7469";
115+
String apiShortName = Composer.parseDefaultHost(defaultHost);
116+
assertEquals("logging:7469", apiShortName);
117+
}
118+
96119
@Test
97120
public void gapicClass_addRegionTagAndHeaderToSample() {
98121
Sample testSample;
@@ -106,12 +129,12 @@ public void gapicClass_addRegionTagAndHeaderToSample() {
106129
public void composeSamples_parseProtoPackage() {
107130

108131
String defaultHost = "accessapproval.googleapis.com:443";
132+
GapicClass testClass = clazzes.get(0).withSamples(ListofSamples).withDefaultHost(defaultHost);
133+
List<GapicClass> testClassList = Arrays.asList(new GapicClass[] {testClass});
109134
String protoPack = "google.cloud.accessapproval.v1";
110-
Service testService =
111-
echoProtoService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build();
112-
List<GapicClass> testClassList = getTestClassListFromService(testService);
135+
113136
List<Sample> composedSamples =
114-
Composer.prepareExecutableSamples(testClassList).get(0).samples();
137+
Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples();
115138

116139
// If samples is empty, the test automatically passes without checking.
117140
assertFalse(composedSamples.isEmpty());
@@ -126,10 +149,9 @@ public void composeSamples_parseProtoPackage() {
126149

127150
protoPack = "google.cloud.vision.v1p1beta1";
128151
defaultHost = "vision.googleapis.com";
129-
testService =
130-
testService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build();
131-
testClassList = getTestClassListFromService(testService);
132-
composedSamples = Composer.prepareExecutableSamples(testClassList).get(0).samples();
152+
testClass = clazzes.get(0).withSamples(ListofSamples).withDefaultHost(defaultHost);
153+
testClassList = Arrays.asList(new GapicClass[] {testClass});
154+
composedSamples = Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples();
133155
// If samples is empty, the test automatically passes without checking.
134156
assertFalse(composedSamples.isEmpty());
135157

@@ -139,10 +161,7 @@ public void composeSamples_parseProtoPackage() {
139161
}
140162

141163
protoPack = "google.cloud.vision";
142-
testService =
143-
testService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build();
144-
testClassList = getTestClassListFromService(testService);
145-
composedSamples = Composer.prepareExecutableSamples(testClassList).get(0).samples();
164+
composedSamples = Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples();
146165
// If samples is empty, the test automatically passes without checking.
147166
assertFalse(composedSamples.isEmpty());
148167

@@ -151,15 +170,4 @@ public void composeSamples_parseProtoPackage() {
151170
assertEquals("ApiVersion should be empty", sample.regionTag().apiVersion(), "");
152171
}
153172
}
154-
155-
private List<GapicClass> getTestClassListFromService(Service testService) {
156-
GapicClass testClass =
157-
GrpcServiceCallableFactoryClassComposer.instance()
158-
.generate(context, testService)
159-
.withSamples(ListofSamples)
160-
.withApiShortName(testService.apiShortName())
161-
.withApiVersion(testService.apiVersion());
162-
List<GapicClass> testClassList = Arrays.asList(new GapicClass[] {testClass});
163-
return testClassList;
164-
}
165173
}

0 commit comments

Comments
 (0)