Search in sources :

Example 1 with MethodOptions

use of com.google.protobuf.DescriptorProtos.MethodOptions in project gapic-generator-java by googleapis.

the class HttpRuleParser method parse.

public static HttpBindings parse(MethodDescriptor protoMethod, Message inputMessage, Map<String, Message> messageTypes) {
    MethodOptions methodOptions = protoMethod.getOptions();
    if (!methodOptions.hasExtension(AnnotationsProto.http)) {
        return null;
    }
    HttpRule httpRule = methodOptions.getExtension(AnnotationsProto.http);
    // Body validation.
    if (!Strings.isNullOrEmpty(httpRule.getBody()) && !httpRule.getBody().equals(ASTERISK)) {
        checkHttpFieldIsValid(httpRule.getBody(), inputMessage, true);
    }
    return parseHttpRuleHelper(httpRule, Optional.of(inputMessage), messageTypes);
}
Also used : MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) HttpRule(com.google.api.HttpRule)

Example 2 with MethodOptions

use of com.google.protobuf.DescriptorProtos.MethodOptions in project yamcs by yamcs.

the class ProtobufRegistry method importDefinitions.

public void importDefinitions(InputStream in) throws IOException {
    if (in == null) {
        throw new NullPointerException("input stream cannot be null");
    }
    FileDescriptorSet proto = FileDescriptorSet.parseFrom(in, extensionRegistry);
    // Index all messages by fully-qualified protobuf name
    for (FileDescriptorProto file : proto.getFileList()) {
        scanComments(file);
        String javaPackage = file.getOptions().getJavaPackage();
        javaPackages.put(file.getName(), javaPackage);
        for (DescriptorProto messageType : file.getMessageTypeList()) {
            String qname = file.getPackage() + "." + messageType.getName();
            messageTypes.put(qname, messageType);
        }
    }
    // Index RPCs
    for (FileDescriptorProto file : proto.getFileList()) {
        for (ServiceDescriptorProto service : file.getServiceList()) {
            for (MethodDescriptorProto method : service.getMethodList()) {
                MethodOptions options = method.getOptions();
                String serviceName = service.getName();
                String methodName = method.getName();
                DescriptorProto inputType = messageTypes.get(method.getInputType().substring(1));
                DescriptorProto outputType = messageTypes.get(method.getOutputType().substring(1));
                if (options.hasExtension(AnnotationsProto.route)) {
                    HttpRoute route = options.getExtension(AnnotationsProto.route);
                    RpcDescriptor descriptor = new RpcDescriptor(serviceName, methodName, inputType, outputType, route);
                    String qname = String.join(".", file.getPackage(), serviceName, methodName);
                    rpcs.put(qname, descriptor);
                } else if (options.hasExtension(AnnotationsProto.websocket)) {
                    WebSocketTopic topic = options.getExtension(AnnotationsProto.websocket);
                    RpcDescriptor descriptor = new RpcDescriptor(serviceName, methodName, inputType, outputType, topic);
                    String qname = String.join(".", file.getPackage(), serviceName, methodName);
                    rpcs.put(qname, descriptor);
                }
            }
        }
    }
}
Also used : HttpRoute(org.yamcs.api.HttpRoute) FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) WebSocketTopic(org.yamcs.api.WebSocketTopic) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto)

Example 3 with MethodOptions

use of com.google.protobuf.DescriptorProtos.MethodOptions in project gapic-generator-java by googleapis.

the class Parser method parseLro.

@VisibleForTesting
static LongrunningOperation parseLro(String servicePackage, MethodDescriptor methodDescriptor, Map<String, Message> messageTypes) {
    MethodOptions methodOptions = methodDescriptor.getOptions();
    TypeNode operationServiceStubType = null;
    String responseTypeName = null;
    String metadataTypeName = null;
    if (methodOptions.hasExtension(OperationsProto.operationInfo)) {
        OperationInfo lroInfo = methodDescriptor.getOptions().getExtension(OperationsProto.operationInfo);
        responseTypeName = lroInfo.getResponseType();
        metadataTypeName = lroInfo.getMetadataType();
    }
    if (methodOptions.hasExtension(ExtendedOperationsProto.operationService)) {
        // TODO: support full package name for operations_service annotation value
        String opServiceName = methodOptions.getExtension(ExtendedOperationsProto.operationService);
        operationServiceStubType = TypeNode.withReference(VaporReference.builder().setName(opServiceName + "Stub").setPakkage(servicePackage + ".stub").build());
        if (responseTypeName == null) {
            responseTypeName = methodDescriptor.getOutputType().getFullName();
        }
        if (metadataTypeName == null) {
            metadataTypeName = methodDescriptor.getOutputType().getFullName();
        }
    }
    if (responseTypeName == null || metadataTypeName == null) {
        return null;
    }
    Message responseMessage = null;
    Message metadataMessage = null;
    int lastDotIndex = responseTypeName.lastIndexOf('.');
    boolean isResponseTypeNameShortOnly = lastDotIndex < 0;
    String responseTypeShortName = lastDotIndex >= 0 ? responseTypeName.substring(lastDotIndex + 1) : responseTypeName;
    lastDotIndex = metadataTypeName.lastIndexOf('.');
    boolean isMetadataTypeNameShortOnly = lastDotIndex < 0;
    String metadataTypeShortName = lastDotIndex >= 0 ? metadataTypeName.substring(lastDotIndex + 1) : metadataTypeName;
    // The messageTypes map keys to the Java fully-qualified name.
    for (Map.Entry<String, Message> messageEntry : messageTypes.entrySet()) {
        String messageKey = messageEntry.getKey();
        int messageLastDotIndex = messageEntry.getKey().lastIndexOf('.');
        String messageShortName = messageLastDotIndex >= 0 ? messageKey.substring(messageLastDotIndex + 1) : messageKey;
        if (responseMessage == null) {
            if (isResponseTypeNameShortOnly && responseTypeName.equals(messageShortName)) {
                responseMessage = messageEntry.getValue();
            } else if (!isResponseTypeNameShortOnly && responseTypeShortName.equals(messageShortName)) {
                // Ensure that the full proto name matches.
                Message candidateMessage = messageEntry.getValue();
                if (candidateMessage.fullProtoName().equals(responseTypeName)) {
                    responseMessage = candidateMessage;
                }
            }
        }
        if (metadataMessage == null) {
            if (isMetadataTypeNameShortOnly && metadataTypeName.equals(messageShortName)) {
                metadataMessage = messageEntry.getValue();
            } else if (!isMetadataTypeNameShortOnly && metadataTypeShortName.equals(messageShortName)) {
                // Ensure that the full proto name matches.
                Message candidateMessage = messageEntry.getValue();
                if (candidateMessage.fullProtoName().equals(metadataTypeName)) {
                    metadataMessage = candidateMessage;
                }
            }
        }
    }
    Preconditions.checkNotNull(responseMessage, String.format("LRO response message %s not found on method %s", responseTypeName, methodDescriptor.getName()));
    // TODO(miraleung): Check that the packages are equal if those strings are not empty.
    Preconditions.checkNotNull(metadataMessage, String.format("LRO metadata message %s not found in method %s", metadataTypeName, methodDescriptor.getName()));
    return LongrunningOperation.builder().setResponseType(responseMessage.type()).setMetadataType(metadataMessage.type()).setOperationServiceStubType(operationServiceStubType).build();
}
Also used : OperationInfo(com.google.longrunning.OperationInfo) Message(com.google.api.generator.gapic.model.Message) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) TypeNode(com.google.api.generator.engine.ast.TypeNode) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with MethodOptions

use of com.google.protobuf.DescriptorProtos.MethodOptions 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

MethodOptions (com.google.protobuf.DescriptorProtos.MethodOptions)4 HttpRule (com.google.api.HttpRule)1 RoutingParameter (com.google.api.RoutingParameter)1 RoutingRule (com.google.api.RoutingRule)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 Message (com.google.api.generator.gapic.model.Message)1 RoutingHeaderRule (com.google.api.generator.gapic.model.RoutingHeaderRule)1 RoutingHeaderParam (com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 OperationInfo (com.google.longrunning.OperationInfo)1 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)1 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)1 FileDescriptorSet (com.google.protobuf.DescriptorProtos.FileDescriptorSet)1 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)1 ServiceDescriptorProto (com.google.protobuf.DescriptorProtos.ServiceDescriptorProto)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpRoute (org.yamcs.api.HttpRoute)1