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

Commit c61364b

Browse files
authored
[ggj][codegen] fix: handle non-last version package components (#634)
* fix: fix dep ordering in Bazel dedupe rules * feat: Add alpha/beta API detection and fix per-class annotations * fix: update integration tests * fix: handle non-lastversion package components
1 parent c5a32cc commit c61364b

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/utils/PackageChecker.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.regex.Pattern;
2121

2222
public class PackageChecker {
23+
24+
// Nothing to initialize.
2325
private PackageChecker() {}
2426

2527
/**
@@ -30,13 +32,18 @@ public static boolean isGaApi(String pakkage) {
3032
String[] packageComponents = pakkage.split("\\.");
3133
Preconditions.checkState(
3234
packageComponents.length > 0, "No subcomponents found in Java package %s", pakkage);
33-
String versionComponent = packageComponents[packageComponents.length - 1];
34-
Matcher matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent);
35-
Preconditions.checkState(
36-
matcher.find(),
37-
"No version component found in last subpackage %s of %s",
38-
versionComponent,
39-
pakkage);
35+
String versionComponent = null;
36+
Matcher matcher = null;
37+
boolean isFound = false;
38+
for (int i = packageComponents.length - 1; i >= 0; i--) {
39+
versionComponent = packageComponents[i];
40+
matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent);
41+
isFound = matcher.find();
42+
if (isFound) {
43+
break;
44+
}
45+
}
46+
Preconditions.checkState(isFound, "No version component found in package %s", pakkage);
4047
String versionSubstr = versionComponent.replace(matcher.group(), "");
4148
return Strings.isNullOrEmpty(versionSubstr)
4249
|| (!versionSubstr.contains("alpha") && !versionSubstr.contains("beta"));

src/test/java/com/google/api/generator/gapic/composer/utils/PackageCheckerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
public class PackageCheckerTest {
2424
@Test
2525
public void isGaApi_normalExpectedPackageStructure() {
26+
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1.services"));
27+
assertTrue(PackageChecker.isGaApi("com.google.cloud.v1.foobar.services"));
2628
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1"));
2729
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v999"));
2830
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v12345a"));
@@ -40,7 +42,7 @@ public void isGaApi_normalExpectedPackageStructure() {
4042
public void isGaApi_invalidPackageStructure() {
4143
assertThrows(
4244
IllegalStateException.class,
43-
() -> PackageChecker.isGaApi("com.google.cloud.dataproc.v1.foobar"));
45+
() -> PackageChecker.isGaApi("com.google.cloud.dataproc.foo.bar"));
4446
assertThrows(IllegalStateException.class, () -> PackageChecker.isGaApi(""));
4547
}
4648
}

0 commit comments

Comments
 (0)