Skip to content

Commit

Permalink
[ggj][codegen] fix: handle non-last version package components (#634)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
miraleung authored Jan 8, 2021
1 parent c5a32cc commit c61364b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.regex.Pattern;

public class PackageChecker {

// Nothing to initialize.
private PackageChecker() {}

/**
Expand All @@ -30,13 +32,18 @@ public static boolean isGaApi(String pakkage) {
String[] packageComponents = pakkage.split("\\.");
Preconditions.checkState(
packageComponents.length > 0, "No subcomponents found in Java package %s", pakkage);
String versionComponent = packageComponents[packageComponents.length - 1];
Matcher matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent);
Preconditions.checkState(
matcher.find(),
"No version component found in last subpackage %s of %s",
versionComponent,
pakkage);
String versionComponent = null;
Matcher matcher = null;
boolean isFound = false;
for (int i = packageComponents.length - 1; i >= 0; i--) {
versionComponent = packageComponents[i];
matcher = Pattern.compile("^v[0-9]+").matcher(versionComponent);
isFound = matcher.find();
if (isFound) {
break;
}
}
Preconditions.checkState(isFound, "No version component found in package %s", pakkage);
String versionSubstr = versionComponent.replace(matcher.group(), "");
return Strings.isNullOrEmpty(versionSubstr)
|| (!versionSubstr.contains("alpha") && !versionSubstr.contains("beta"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
public class PackageCheckerTest {
@Test
public void isGaApi_normalExpectedPackageStructure() {
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1.services"));
assertTrue(PackageChecker.isGaApi("com.google.cloud.v1.foobar.services"));
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v1"));
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v999"));
assertTrue(PackageChecker.isGaApi("com.google.cloud.dataproc.v12345a"));
Expand All @@ -40,7 +42,7 @@ public void isGaApi_normalExpectedPackageStructure() {
public void isGaApi_invalidPackageStructure() {
assertThrows(
IllegalStateException.class,
() -> PackageChecker.isGaApi("com.google.cloud.dataproc.v1.foobar"));
() -> PackageChecker.isGaApi("com.google.cloud.dataproc.foo.bar"));
assertThrows(IllegalStateException.class, () -> PackageChecker.isGaApi(""));
}
}

0 comments on commit c61364b

Please sign in to comment.