use of com.google.api.generator.gapic.model.SourceCodeInfoLocation in project gapic-generator-java by googleapis.
the class Parser method parseMethods.
@VisibleForTesting
static List<Method> parseMethods(ServiceDescriptor serviceDescriptor, String servicePackage, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, Optional<GapicServiceConfig> serviceConfigOpt, Set<ResourceName> outputArgResourceNames, Transport transport) {
List<Method> methods = new ArrayList<>();
for (MethodDescriptor protoMethod : serviceDescriptor.getMethods()) {
// Parse the method.
TypeNode inputType = TypeParser.parseType(protoMethod.getInputType());
Method.Builder methodBuilder = Method.builder();
if (protoMethod.getFile().toProto().hasSourceCodeInfo()) {
SourceCodeInfoLocation protoMethodLocation = SOURCE_CODE_INFO_PARSER.getLocation(protoMethod);
if (!Objects.isNull(protoMethodLocation) && !Strings.isNullOrEmpty(protoMethodLocation.getLeadingComments())) {
methodBuilder.setDescription(protoMethodLocation.getLeadingComments());
}
}
boolean isDeprecated = false;
if (protoMethod.getOptions().hasDeprecated()) {
isDeprecated = protoMethod.getOptions().getDeprecated();
}
Message inputMessage = messageTypes.get(inputType.reference().fullName());
Preconditions.checkNotNull(inputMessage, String.format("No message found for %s", inputType.reference().fullName()));
HttpBindings httpBindings = HttpRuleParser.parse(protoMethod, inputMessage, messageTypes);
boolean isBatching = !serviceConfigOpt.isPresent() ? false : serviceConfigOpt.get().hasBatchingSetting(/* protoPakkage */
protoMethod.getFile().getPackage(), serviceDescriptor.getName(), protoMethod.getName());
boolean operationPollingMethod = protoMethod.getOptions().hasExtension(ExtendedOperationsProto.operationPollingMethod) ? protoMethod.getOptions().getExtension(ExtendedOperationsProto.operationPollingMethod) : false;
RoutingHeaderRule routingHeaderRule = RoutingRuleParser.parse(protoMethod, inputMessage, messageTypes);
methods.add(methodBuilder.setName(protoMethod.getName()).setInputType(inputType).setOutputType(TypeParser.parseType(protoMethod.getOutputType())).setStream(Method.toStream(protoMethod.isClientStreaming(), protoMethod.isServerStreaming())).setLro(parseLro(servicePackage, protoMethod, messageTypes)).setMethodSignatures(MethodSignatureParser.parseMethodSignatures(protoMethod, servicePackage, inputType, messageTypes, resourceNames, outputArgResourceNames)).setHttpBindings(httpBindings).setRoutingHeaderRule(routingHeaderRule).setIsBatching(isBatching).setPageSizeFieldName(parsePageSizeFieldName(protoMethod, messageTypes, transport)).setIsDeprecated(isDeprecated).setOperationPollingMethod(operationPollingMethod).build());
// Any input type that has a resource reference will need a resource name helper class.
for (Field field : inputMessage.fields()) {
if (field.hasResourceReference()) {
String resourceTypeString = field.resourceReference().resourceTypeString();
ResourceName resourceName = null;
// versus example.com/FooBar.
if (resourceTypeString.indexOf(SLASH) < 0) {
Optional<String> actualResourceTypeNameOpt = resourceNames.keySet().stream().filter(k -> k.substring(k.lastIndexOf(SLASH) + 1).equals(resourceTypeString)).findFirst();
if (actualResourceTypeNameOpt.isPresent()) {
resourceName = resourceNames.get(actualResourceTypeNameOpt.get());
}
} else {
resourceName = resourceNames.get(resourceTypeString);
}
if (ResourceNameConstants.WILDCARD_PATTERN.equals(resourceTypeString)) {
resourceName = WILDCARD_RESOURCE_NAME;
} else {
Preconditions.checkNotNull(resourceName, String.format("Resource name %s not found; parsing field %s in message %s in method %s", resourceTypeString, field.name(), inputMessage.name(), protoMethod.getName()));
}
outputArgResourceNames.add(resourceName);
}
}
}
return methods;
}
use of com.google.api.generator.gapic.model.SourceCodeInfoLocation in project gapic-generator-java by googleapis.
the class SourceCodeInfoParser method getLocation.
/**
* Gets the location of a field, if available.
*/
@Nullable
public SourceCodeInfoLocation getLocation(FieldDescriptor field) {
FileDescriptor file = field.getFile();
if (!file.toProto().hasSourceCodeInfo()) {
return null;
}
Location fieldLocation = getLocation(file, buildPath(field));
return SourceCodeInfoLocation.create(fieldLocation);
}
use of com.google.api.generator.gapic.model.SourceCodeInfoLocation in project gapic-generator-java by googleapis.
the class SourceCodeInfoParserTest method getOuterEnumInfo.
@Test
public void getOuterEnumInfo() {
EnumDescriptor protoEnum = protoFile.findEnumTypeByName("OuterEnum");
SourceCodeInfoLocation location = parser.getLocation(protoEnum);
assertEquals("This is an outer enum.", location.getLeadingComments());
// Enum fields.
location = parser.getLocation(protoEnum.findValueByName("VALUE_UNSPECIFIED"));
assertEquals("Another unspecified value.", location.getLeadingComments());
}
use of com.google.api.generator.gapic.model.SourceCodeInfoLocation in project gapic-generator-java by googleapis.
the class SourceCodeInfoParserTest method getInnerMessageInfo.
@Test
public void getInnerMessageInfo() {
Descriptor message = protoFile.findMessageTypeByName("FooMessage");
assertThat(message).isNotNull();
message = message.findNestedTypeByName("BarMessage");
SourceCodeInfoLocation location = parser.getLocation(message);
assertEquals("This is an inner message description for BarMessage.", location.getLeadingComments());
// Fields.
location = parser.getLocation(message.findFieldByName("field_three"));
assertEquals("A third leading comment for field_three.", location.getLeadingComments());
location = parser.getLocation(message.findFieldByName("field_two"));
assertEquals("This is a block comment for field_two.", location.getLeadingComments());
}
use of com.google.api.generator.gapic.model.SourceCodeInfoLocation in project gapic-generator-java by googleapis.
the class SourceCodeInfoParserTest method getServiceInfo.
@Test
public void getServiceInfo() {
SourceCodeInfoLocation location = parser.getLocation(protoFile.findServiceByName("FooService"));
assertEquals("This is a service description.\n It takes up multiple lines, like so.", location.getLeadingComments());
location = parser.getLocation(protoFile.findServiceByName("BarService"));
assertEquals("This is another service description.", location.getLeadingComments());
}
Aggregations