Skip to content

Commit

Permalink
chore: support child stub packages
Browse files Browse the repository at this point in the history
  • Loading branch information
burkedavison committed Sep 25, 2023
1 parent 089678e commit dc03ae9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ private TocItem buildPackage(PackageElement element) {

// build stubs
packageLookup
.findStubPackage(element, allPackages)
.ifPresent((PackageElement stub) -> packageTocItem.getItems().add(buildPackage(stub)));
.findStubPackages(element, allPackages)
.forEach((PackageElement stub) -> packageTocItem.getItems().add(buildPackage(stub)));

return packageTocItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import com.google.docfx.doclet.ApiVersion;
import com.microsoft.lookup.model.ExtendedMetadataFileItem;
import com.microsoft.model.Status;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.PackageElement;
import jdk.javadoc.doclet.DocletEnvironment;

Expand Down Expand Up @@ -79,16 +81,20 @@ public boolean isApiVersionStubPackage(PackageElement pkg) {
*/
@VisibleForTesting
boolean isApiVersionStubPackageName(String name) {
return getLeafPackage(name).equals("stub")
&& extractApiVersion(withoutLeafPackage(name)).isPresent();
List<String> packagePath = Arrays.asList(name.split("\\."));
int stubIndex = packagePath.indexOf("stub");
if (stubIndex < 1) {
return false;
}
return ApiVersion.parse(packagePath.get(stubIndex - 1)).isPresent();
}

public Optional<PackageElement> findStubPackage(
public List<PackageElement> findStubPackages(
PackageElement pkg, Collection<PackageElement> packages) {
String expectedStubPackage = pkg.getQualifiedName() + ".stub";
String expectedStubPackageBase = pkg.getQualifiedName() + ".stub";
return packages.stream()
.filter(p -> String.valueOf(p.getQualifiedName()).equals(expectedStubPackage))
.findFirst();
.filter(p -> String.valueOf(p.getQualifiedName()).startsWith(expectedStubPackageBase))
.collect(Collectors.toList());
}

/** Compare PackageElements by their parsed ApiVersion */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.microsoft.lookup.PackageLookup.PackageGroup;
import com.microsoft.model.Status;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.PackageElement;
import javax.lang.model.util.Elements;
Expand Down Expand Up @@ -188,22 +187,23 @@ public void testFindStubPackage() {
elements.getPackageElement("com.microsoft.samples.google.v1beta"),
elements.getPackageElement("com.microsoft.samples.google"));

Optional<PackageElement> foundStubPackage =
packageLookup.findStubPackage(
List<PackageElement> foundStubPackages =
packageLookup.findStubPackages(
elements.getPackageElement("com.microsoft.samples.google.v1"), packages);
assertThat(foundStubPackage.isPresent()).isTrue();
assertThat(toPackageName(foundStubPackage.get()))
assertThat(foundStubPackages).isNotEmpty();
assertThat(foundStubPackages).hasSize(1);
assertThat(toPackageName(foundStubPackages.get(0)))
.isEqualTo("com.microsoft.samples.google.v1.stub");

Optional<PackageElement> notFoundStubPackageOfStubPackage =
packageLookup.findStubPackage(
List<PackageElement> notFoundStubPackageOfStubPackage =
packageLookup.findStubPackages(
elements.getPackageElement("com.microsoft.samples.google.v1.stub"), packages);
assertThat(notFoundStubPackageOfStubPackage.isPresent()).isFalse();
assertThat(notFoundStubPackageOfStubPackage).isEmpty();

Optional<PackageElement> notFoundStubPackage =
packageLookup.findStubPackage(
List<PackageElement> notFoundStubPackage =
packageLookup.findStubPackages(
elements.getPackageElement("com.microsoft.samples.google"), packages);
assertThat(notFoundStubPackage.isPresent()).isFalse();
assertThat(notFoundStubPackage).isEmpty();
}

@Test
Expand All @@ -216,10 +216,13 @@ public void testIsApiStubPackage() {
packageLookup.isApiVersionStubPackage(
elements.getPackageElement("com.microsoft.samples.google.v1.stub")))
.isTrue();
assertThat(
packageLookup.isApiVersionStubPackageName("com.microsoft.samples.google.v1.stub.child"))
.isTrue();

assertThat(packageLookup.isApiVersionStubPackageName("a")).isFalse();
// False due to not being an API version package, even though it ends in .stub
assertThat(packageLookup.isApiVersionStubPackageName("com.microsoft.samples.google.stub"))
.isFalse();
assertThat(packageLookup.isApiVersionStubPackageName("a.stub")).isFalse();
}

private List<String> toPackageNames(List<PackageElement> packages) {
Expand Down

0 comments on commit dc03ae9

Please sign in to comment.