Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: expose methods for parsing api short name and version #1066

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/main/java/com/google/api/generator/gapic/composer/Composer.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,25 @@ public static List<GapicClass> generateTestClasses(GapicContext context) {
return clazzes;
}

@VisibleForTesting
static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes, String protoPackage) {
// Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default
// endpoints like
// "us-east1-pubsub.googleapis.com".
public static String parseApiShortName(String defaultHost) {
// If the defaultHost is of the format "**.googleapis.com", take the name before the first
// period.
String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost);
// If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the
// first period and after the last dash to follow CSharp's implementation here:
// https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70
apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost);
// `iam-meta-api` service is an exceptional case and is handled as a one-off
if (defaultHost.contains("iam-meta-api")) {
apiShortName = "iam";
}
return apiShortName;
}

public static String parseApiVersion(String protoPackage) {
// parse protoPackage for apiVersion
String[] pakkage = protoPackage.split("\\.");
String apiVersion;
Expand All @@ -203,6 +220,11 @@ static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes, Strin
} else {
apiVersion = "";
}
return apiVersion;
}

@VisibleForTesting
static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes, String protoPackage) {
// Include license header, apiShortName, and apiVersion
List<GapicClass> clazzesWithSamples = new ArrayList<>();
clazzes.forEach(
Expand All @@ -214,31 +236,14 @@ static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes, Strin
sample ->
samples.add(
addRegionTagAndHeaderToSample(
sample, parseDefaultHost(gapicClass.defaultHost()), apiVersion)));
sample,
parseApiShortName(gapicClass.defaultHost()),
parseApiVersion(protoPackage))));
clazzesWithSamples.add(gapicClass.withSamples(samples));
});
return clazzesWithSamples;
}

// Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default
// endpoints like
// "us-east1-pubsub.googleapis.com".
@VisibleForTesting
protected static String parseDefaultHost(String defaultHost) {
// If the defaultHost is of the format "**.googleapis.com", take the name before the first
// period.
String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost);
// If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the
// first period and after the last dash to follow CSharp's implementation here:
// https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70
apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost);
// `iam-meta-api` service is an exceptional case and is handled as a one-off
if (defaultHost.contains("iam-meta-api")) {
apiShortName = "iam";
}
return apiShortName;
}

@VisibleForTesting
protected static Sample addRegionTagAndHeaderToSample(
Sample sample, String apiShortName, String apiVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,47 @@ public void composeSamples_showcase() {
}

@Test
public void parseDefaultHost_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() {
public void parseApiShortName_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() {
String defaultHost = "us-east1-pubsub.googleapis.com";
String apiShortName = Composer.parseDefaultHost(defaultHost);
String apiShortName = Composer.parseApiShortName(defaultHost);
assertEquals("pubsub", apiShortName);
}

@Test
public void parseDefaultHost_shouldReturnApiShortName() {
public void parseApiShortName_shouldReturnApiShortName() {
String defaultHost = "logging.googleapis.com";
String apiShortName = Composer.parseDefaultHost(defaultHost);
String apiShortName = Composer.parseApiShortName(defaultHost);
assertEquals("logging", apiShortName);
}

@Test
public void parseDefaultHost_shouldReturnApiShortNameForIam() {
public void parseApiShortName_shouldReturnApiShortNameForIam() {
String defaultHost = "iam-meta-api.googleapis.com";
String apiShortName = Composer.parseDefaultHost(defaultHost);
String apiShortName = Composer.parseApiShortName(defaultHost);
assertEquals("iam", apiShortName);
}

@Test
public void parseDefaultHost_shouldReturnHostIfNoPeriods() {
public void parseApiShortName_shouldReturnHostIfNoPeriods() {
String defaultHost = "logging:7469";
String apiShortName = Composer.parseDefaultHost(defaultHost);
String apiShortName = Composer.parseApiShortName(defaultHost);
assertEquals("logging:7469", apiShortName);
}

@Test
public void parseApiVersion_shouldReturnApiVersion() {
String protoPackage = "google.cloud.accessapproval.v1";
String apiVersion = Composer.parseApiVersion(protoPackage);
assertEquals("v1", apiVersion);
}

@Test
public void parseApiVersion_shouldReturnEmptyIfNoMatch() {
String protoPackage = "google.cloud.accessapproval";
String apiVersion = Composer.parseApiVersion(protoPackage);
assertEquals("", apiVersion);
}

@Test
public void gapicClass_addRegionTagAndHeaderToSample() {
Sample testSample;
Expand Down