use of com.google.api.RoutingRule 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