use of com.google.api.generator.gapic.model.RoutingHeaderRule 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.RoutingHeaderRule in project gapic-generator-java by googleapis.
the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithOneParameter.
@Test
public void parse_shouldParseRoutingRulesWithOneParameter() {
RoutingHeaderRule actual = getRoutingHeaders(4);
RoutingHeaderParam expected = RoutingHeaderParam.create("name", "rename", "/v1beta1/{rename=tests/*}");
assertThat(actual.routingHeaderParams()).containsExactly(expected);
}
use of com.google.api.generator.gapic.model.RoutingHeaderRule in project gapic-generator-java by googleapis.
the class RoutingRuleParserTest method parse_shouldReturnNullRoutingHeadersIfMethodHasNoRoutingRules.
@Test
public void parse_shouldReturnNullRoutingHeadersIfMethodHasNoRoutingRules() {
RoutingHeaderRule actual = getRoutingHeaders(0);
assertThat(actual).isNull();
}
use of com.google.api.generator.gapic.model.RoutingHeaderRule in project gapic-generator-java by googleapis.
the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithNestedFields.
@Test
public void parse_shouldParseRoutingRulesWithNestedFields() {
RoutingHeaderRule actual = getRoutingHeaders(6);
RoutingHeaderParam expectedHeader1 = RoutingHeaderParam.create("account.name", "rename", "/v1beta1/{rename=tests/*}");
assertThat(actual.routingHeaderParams()).containsExactly(expectedHeader1);
}
use of com.google.api.generator.gapic.model.RoutingHeaderRule in project gapic-generator-java by googleapis.
the class RoutingRuleParser method parse.
public static RoutingHeaderRule parse(MethodDescriptor protoMethod, Message inputMessage, Map<String, Message> messageTypes) {
MethodOptions methodOptions = protoMethod.getOptions();
if (!methodOptions.hasExtension(RoutingProto.routing)) {
return null;
}
RoutingHeaderRule.Builder routingHeaderRuleBuilder = RoutingHeaderRule.builder();
RoutingRule routingRule = methodOptions.getExtension(RoutingProto.routing);
for (RoutingParameter routingParameter : routingRule.getRoutingParametersList()) {
String pathTemplate = routingParameter.getPathTemplate();
String fieldName = routingParameter.getField();
// validate if field exist in Message or nested Messages and the type of leaf level field
inputMessage.validateField(fieldName, messageTypes, TypeNode.STRING);
String key;
if (Strings.isNullOrEmpty(pathTemplate)) {
key = fieldName;
pathTemplate = String.format("{%s=**}", key);
} else {
Set<String> namedSegments = PatternParser.getPattenBindings(pathTemplate);
Preconditions.checkArgument(namedSegments.size() == 1, String.format("There needs to be one and only one named segment in path template %s", pathTemplate));
key = namedSegments.iterator().next();
}
RoutingHeaderParam routingHeaderParam = RoutingHeaderParam.create(fieldName, key, pathTemplate);
routingHeaderRuleBuilder.addParam(routingHeaderParam);
}
return routingHeaderRuleBuilder.build();
}
Aggregations