Search in sources :

Example 1 with RoutingRule

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();
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingParameter(com.google.api.RoutingParameter) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) RoutingRule(com.google.api.RoutingRule)

Aggregations

RoutingParameter (com.google.api.RoutingParameter)1 RoutingRule (com.google.api.RoutingRule)1 RoutingHeaderRule (com.google.api.generator.gapic.model.RoutingHeaderRule)1 RoutingHeaderParam (com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam)1 MethodOptions (com.google.protobuf.DescriptorProtos.MethodOptions)1