Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.model.Service;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Generated;
Expand All @@ -37,7 +37,7 @@ public class ClientLibraryPackageInfoComposer {
"The interfaces provided are listed below, along with usage samples.";

private static final String CLIENT_PATTERN = "%sClient";
private static final String PACKAGE_INFO_TITLE_PATTERN = "A client to %s";
private static final String PACKAGE_INFO_TITLE_PATTERN = "Clients for %s.";
private static final String SAMPLE_CODE_HEADER_PATTERN = "Sample for %s:";
private static final String SERVICE_DESCRIPTION_HEADER_PATTERN = "Service Description: %s";

Expand All @@ -61,12 +61,14 @@ public static GapicPackageInfo generatePackageInfo(GapicContext context) {

private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder();
if (context.hasServiceYamlProto()
&& !Strings.isNullOrEmpty(context.serviceYamlProto().getTitle())) {
javaDocCommentBuilder =
javaDocCommentBuilder.addComment(
String.format(PACKAGE_INFO_TITLE_PATTERN, context.serviceYamlProto().getTitle()));
}
javaDocCommentBuilder =
javaDocCommentBuilder.addComment(
String.format(
PACKAGE_INFO_TITLE_PATTERN,
context.services().stream()
.map(s -> s.name())
.collect(
Collectors.collectingAndThen(Collectors.toList(), joiningOxfordComma()))));

javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(PACKAGE_INFO_DESCRIPTION);

Expand Down Expand Up @@ -117,4 +119,13 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {

return CommentStatement.withComment(javaDocCommentBuilder.build());
}

private static Function<List<String>, String> joiningOxfordComma() {
return vals -> {
int last = vals.size() - 1;
if (last < 1) return String.join("", vals);
if (last == 1) return String.join(" and ", vals);
return String.join(", and ", String.join(", ", vals.subList(0, last)), vals.get(last));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ public abstract class GapicContext {
@Nullable
public abstract GapicServiceConfig serviceConfig();

@Nullable
public abstract com.google.api.Service serviceYamlProto();

public boolean hasServiceYamlProto() {
return serviceYamlProto() != null;
}

public static Builder builder() {
return new AutoValue_GapicContext.Builder();
}
Expand All @@ -61,8 +54,6 @@ public abstract static class Builder {

public abstract Builder setServiceConfig(GapicServiceConfig serviceConfig);

public abstract Builder setServiceYamlProto(com.google.api.Service serviceYamlProto);

public abstract GapicContext build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ public static GapicContext parse(CodeGeneratorRequest request) {
String serviceConfigPath = serviceConfigPathOpt.isPresent() ? serviceConfigPathOpt.get() : null;
Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(serviceConfigPath);

Optional<String> serviceYamlConfigPathOpt =
PluginArgumentParser.parseServiceYamlConfigPath(request);
Optional<com.google.api.Service> serviceYamlProtoOpt =
serviceYamlConfigPathOpt.isPresent()
? ServiceYamlParser.parse(serviceYamlConfigPathOpt.get())
: Optional.empty();

// Keep message and resource name parsing separate for cleaner logic.
// While this takes an extra pass through the protobufs, the extra time is relatively trivial
// and is worth the larger reduced maintenance cost.
Expand All @@ -95,24 +88,21 @@ public static GapicContext parse(CodeGeneratorRequest request) {
messages = updateResourceNamesInMessages(messages, resourceNames.values());
Set<ResourceName> outputArgResourceNames = new HashSet<>();
List<Service> services =
parseServices(
request, messages, resourceNames, outputArgResourceNames, serviceYamlProtoOpt);
parseServices(request, messages, resourceNames, outputArgResourceNames);
return GapicContext.builder()
.setServices(services)
.setMessages(messages)
.setResourceNames(resourceNames)
.setHelperResourceNames(outputArgResourceNames)
.setServiceConfig(serviceConfigOpt.isPresent() ? serviceConfigOpt.get() : null)
.setServiceYamlProto(serviceYamlProtoOpt.isPresent() ? serviceYamlProtoOpt.get() : null)
.build();
}

public static List<Service> parseServices(
CodeGeneratorRequest request,
Map<String, Message> messageTypes,
Map<String, ResourceName> resourceNames,
Set<ResourceName> outputArgResourceNames,
Optional<com.google.api.Service> serviceYamlProtoOpt) {
Set<ResourceName> outputArgResourceNames) {
Map<String, FileDescriptor> fileDescriptors = getFilesToGenerate(request);

List<Service> services = new ArrayList<>();
Expand All @@ -124,12 +114,7 @@ public static List<Service> parseServices(
fileToGenerate);

services.addAll(
parseService(
fileDescriptor,
messageTypes,
resourceNames,
serviceYamlProtoOpt,
outputArgResourceNames));
parseService(fileDescriptor, messageTypes, resourceNames, outputArgResourceNames));
}

return services;
Expand All @@ -139,7 +124,6 @@ public static List<Service> parseService(
FileDescriptor fileDescriptor,
Map<String, Message> messageTypes,
Map<String, ResourceName> resourceNames,
Optional<com.google.api.Service> serviceYamlProtoOpt,
Set<ResourceName> outputArgResourceNames) {
String pakkage = TypeParser.getPackage(fileDescriptor);

Expand All @@ -154,9 +138,6 @@ public static List<Service> parseService(
if (serviceOptions.hasExtension(ClientProto.defaultHost)) {
defaultHost =
sanitizeDefaultHost(serviceOptions.getExtension(ClientProto.defaultHost));
} else if (serviceYamlProtoOpt.isPresent()) {
// Fall back to the DNS name supplied in the service .yaml config.
defaultHost = serviceYamlProtoOpt.get().getName();
}
Preconditions.checkState(
!Strings.isNullOrEmpty(defaultHost),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,16 @@ public class PluginArgumentParser {

// Synced to rules_java_gapic/java_gapic.bzl.
@VisibleForTesting static final String KEY_GRPC_SERVICE_CONFIG = "grpc-service-config";
@VisibleForTesting static final String KEY_SERVICE_YAML_CONFIG = "gapic-service-config";

private static final String JSON_FILE_ENDING = "grpc_service_config.json";
private static final String GAPIC_YAML_FILE_ENDING = "gapic.yaml";
private static final String SERVICE_YAML_FILE_ENDING = ".yaml";

static Optional<String> parseJsonConfigPath(CodeGeneratorRequest request) {
return parseJsonConfigPath(request.getParameter());
}

static Optional<String> parseServiceYamlConfigPath(CodeGeneratorRequest request) {
return parseServiceYamlConfigPath(request.getParameter());
}

/** Expects a comma-separated list of file paths. */
@VisibleForTesting
static Optional<String> parseJsonConfigPath(String pluginProtocArgument) {
return parseArgument(pluginProtocArgument, KEY_GRPC_SERVICE_CONFIG, JSON_FILE_ENDING);
}

@VisibleForTesting
static Optional<String> parseServiceYamlConfigPath(String pluginProtocArgument) {
return parseArgument(pluginProtocArgument, KEY_SERVICE_YAML_CONFIG, SERVICE_YAML_FILE_ENDING);
}

private static Optional<String> parseArgument(
String pluginProtocArgument, String key, String fileEnding) {
if (Strings.isNullOrEmpty(pluginProtocArgument)) {
return Optional.<String>empty();
}
Expand All @@ -63,12 +46,7 @@ private static Optional<String> parseArgument(
}
String keyVal = args[0];
String valueVal = args[1];
boolean valueMeetsCriteria = keyVal.equals(key) && valueVal.endsWith(fileEnding);
if (fileEnding.equals(SERVICE_YAML_FILE_ENDING)) {
valueMeetsCriteria &= !valueVal.endsWith(GAPIC_YAML_FILE_ENDING);
}

if (valueMeetsCriteria) {
if (keyVal.equals(KEY_GRPC_SERVICE_CONFIG) && valueVal.endsWith(JSON_FILE_ENDING)) {
return Optional.of(valueVal);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -54,8 +53,7 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;

Expand All @@ -51,8 +50,7 @@ public void generateGrpcServiceStubClass_simple() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Service echoProtoService = services.get(0);
GapicClass clazz =
GrpcServiceStubClassComposer.instance().generate(echoProtoService, messageTypes);
Expand All @@ -75,11 +73,7 @@ public void generateGrpcServiceStubClass_httpBindings() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
testingFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);
testingFileDescriptor, messageTypes, resourceNames, outputResourceNames);
Service testingProtoService = services.get(0);
GapicClass clazz =
GrpcServiceStubClassComposer.instance().generate(testingProtoService, messageTypes);
Expand Down Expand Up @@ -108,11 +102,7 @@ public void generateGrpcServiceStubClass_httpBindingsWithSubMessageFields() {
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);

Service service = services.get(0);
GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(service, messageTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -54,8 +53,7 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz = MockServiceClassComposer.instance().generate(echoProtoService, messageTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -54,8 +53,7 @@ public void generateServiceClasses() {
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames);

Service echoProtoService = services.get(0);
GapicClass clazz =
Expand Down
Loading