Java Code Examples for com.google.protobuf.Descriptors#ServiceDescriptor

The following examples show how to use com.google.protobuf.Descriptors#ServiceDescriptor . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: metastore   File: ProtoDomain.java    License: Apache License 2.0 6 votes vote down vote up
public Collection<Descriptors.ServiceDescriptor> findServiceDescriptorsByOption(
    String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getServiceOptionByName(optionName);
  return serviceMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.ServiceOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example 2
Source Project: quarkus   File: GrpcServerIndex.java    License: Apache License 2.0 6 votes vote down vote up
private void processFileDescriptor(FileDescriptor fd,
        Map<String, FileDescriptor> descriptorsByName,
        Map<String, FileDescriptor> descriptorsBySymbol,
        Map<String, Map<Integer, FileDescriptor>> descriptorsByExtensionAndNumber) {
    String name = fd.getName();
    if (descriptorsByName.containsKey(name)) {
        throw new IllegalStateException("File name already used: " + name);
    }
    descriptorsByName.put(name, fd);
    for (Descriptors.ServiceDescriptor service : fd.getServices()) {
        processService(service, fd, descriptorsBySymbol);
    }
    for (Descriptors.Descriptor type : fd.getMessageTypes()) {
        processType(type, fd, descriptorsBySymbol, descriptorsByExtensionAndNumber);
    }
    for (Descriptors.FieldDescriptor extension : fd.getExtensions()) {
        processExtension(extension, fd, descriptorsByExtensionAndNumber);
    }
}
 
Example 3
Source Project: quarkus   File: GrpcServerIndex.java    License: Apache License 2.0 6 votes vote down vote up
private void processService(Descriptors.ServiceDescriptor service, FileDescriptor fd,
        Map<String, FileDescriptor> descriptorsBySymbol) {
    String fullyQualifiedServiceName = service.getFullName();
    if (descriptorsBySymbol.containsKey(fullyQualifiedServiceName)) {
        throw new IllegalStateException("Service already defined: " + fullyQualifiedServiceName);
    }
    descriptorsBySymbol.put(fullyQualifiedServiceName, fd);
    for (Descriptors.MethodDescriptor method : service.getMethods()) {
        String fullyQualifiedMethodName = method.getFullName();
        if (descriptorsBySymbol.containsKey(fullyQualifiedMethodName)) {
            throw new IllegalStateException(
                    "Method already defined: " + fullyQualifiedMethodName + " in " + fullyQualifiedServiceName);
        }
        descriptorsBySymbol.put(fullyQualifiedMethodName, fd);
    }
}
 
Example 4
Source Project: metastore   File: ProtoDiff.java    License: Apache License 2.0 6 votes vote down vote up
private void diffServices(
    List<Descriptors.ServiceDescriptor> s_ref, List<Descriptors.ServiceDescriptor> s_new) {
  diffGenericDescriptor(
      s_ref,
      s_new,
      d ->
          results.setPatch(
              d,
              ChangeInfo.newBuilder()
                  .setChangeType(ChangeType.REMOVAL)
                  .setFromName(d.getFullName())
                  .build()),
      d ->
          results.setPatch(
              d,
              ChangeInfo.newBuilder()
                  .setChangeType(ChangeType.ADDITION)
                  .setToName(d.getFullName())
                  .build()),
      this::diffServiceDescriptor);
}
 
Example 5
Source Project: incubator-retired-wave   File: RpcTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the streaming RPC option is being parsed correctly.
 */
public void testIsStreamingRpc() throws Exception {
  Descriptors.ServiceDescriptor serviceDescriptor =
      WaveClientRpc.ProtocolWaveClientRpc.getDescriptor();
  assertTrue(serviceDescriptor.findMethodByName("Open").getOptions()
      .getExtension(Rpc.isStreamingRpc));
  assertFalse(serviceDescriptor.findMethodByName("Submit").getOptions()
      .getExtension(Rpc.isStreamingRpc));
}
 
Example 6
private void writeServiceDescriptor(
    Descriptors.ServiceDescriptor serviceDescriptor, PathLocation parent) {
  PathLocation location = parent.addService(serviceDescriptor);
  writeLeadingComment(commentIndexer.getLocation(location), 0);
  writer.print("service ");
  writer.print(serviceDescriptor.getName());
  writer.println(" {");
  writeOptionsForBlock(serviceDescriptor.getOptions(), 1, "Service");
  for (Descriptors.MethodDescriptor method : serviceDescriptor.getMethods()) {
    PathLocation methodLocation = location.addMethod(method);
    writeLeadingComment(commentIndexer.getLocation(methodLocation), 1);
    indent(1);
    writer.print("rpc ");
    writer.print(method.getName());
    writer.print("(");
    if (method.isClientStreaming()) {
      writer.print("stream ");
    }
    writer.print(method.getInputType().getFullName());
    writer.print(") returns (");
    if (method.isServerStreaming()) {
      writer.print("stream ");
    }
    writer.print(method.getOutputType().getFullName());
    DescriptorProtos.MethodOptions options = method.getOptions();
    if (options.getAllFields().size() == 0 && options.getUnknownFields().asMap().size() == 0) {
      writer.print(") {}");
    } else {
      writer.println(") {");
      writeOptionsForBlock(options, 2, "Method");
      indent(1);
      writer.print("}");
    }
    writeTrailingComment(commentIndexer.getLocation(methodLocation), 1);
  }
  writer.print("}");
  writeTrailingComment(commentIndexer.getLocation(location), 0);
}
 
Example 7
Source Project: metastore   File: ProtoDomainTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void findServiceDescriptorsByOption() throws IOException {
  ProtoDomain domain = TestSets.baseMultipleOptions();
  Collection<Descriptors.ServiceDescriptor> options =
      domain.findServiceDescriptorsByOption("test.v1.service_option");
  // TODO Add more sets with services
  Assert.assertEquals(0, options.size());
}
 
Example 8
Source Project: karate-grpc   File: ServiceResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Lists all of the services found in the file descriptors.
 */
public Iterable<Descriptors.ServiceDescriptor> listServices() {
    ArrayList<Descriptors.ServiceDescriptor> serviceDescriptors = new ArrayList<>();
    fileDescriptors.forEach(fileDescriptor -> serviceDescriptors.addAll(fileDescriptor.getServices()));

    return serviceDescriptors;
}
 
Example 9
Source Project: karate-grpc   File: GrpcList.java    License: MIT License 5 votes vote down vote up
/**
 * List the methods on the service (the mothodFilter will be applied if non empty.
 */
private static void listMethods(
        Map<String, Object> output,
        Descriptors.ServiceDescriptor descriptor,
        String methodFilter,
        Boolean withMessage,
        Boolean saveOutputInfo) {
    List<Descriptors.MethodDescriptor> methodDescriptors = descriptor.getMethods();

    methodDescriptors.forEach(method -> {
        if (methodFilter.isEmpty() || method.getName().contains(methodFilter)) {
            String key = descriptor.getFullName() + "/" + method.getName();

            Map<String, Object> res = new HashMap<>();
            res.put("file", descriptor.getFile().getName());

            // If requested, add the message definition
            if (withMessage) {
                Map<String, Object> o = new HashMap<>();
                o.put(method.getInputType().getName(), renderDescriptor(method.getInputType()));
                res.put("input", o);
                if (saveOutputInfo) {
                    Map<String, Object> oo = new HashMap<>();
                    oo.put(method.getOutputType().getName(), renderDescriptor(method.getOutputType()));
                    res.put("output", oo);
                }
            }
            output.put(key, res);
        }
    });
}
 
Example 10
Source Project: metastore   File: GoogleDataCatalog.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void updateServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor) {
  try (Scope scope =
      TRACER
          .spanBuilder("GoogleDataCatalog.updateResourceBinding")
          .setRecordEvents(true)
          .startScopedSpan()) {}
}
 
Example 11
Source Project: metastore   File: GoogleCloudStorage.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void createServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor) {
  try (Scope scope =
      TRACER
          .spanBuilder("GoogleCloudStorage.createServiceBinding")
          .setRecordEvents(true)
          .startScopedSpan()) {
    bindDatabase.bindService(resourceUrn, descriptor.getFullName());
    saveBind();
  }
}
 
Example 12
Source Project: xresloader   File: DataDstPbHelper.java    License: MIT License 5 votes vote down vote up
static public HashMap<String, Object> dumpOptionsIntoHashMap(Descriptors.ServiceDescriptor msg_desc,
        com.google.protobuf.ExtensionRegistry custom_extensions) {
    HashMap<String, Object> msg_root = new HashMap<String, Object>();
    boolean has_data = false;

    HashMap<String, Object> options_data = dumpMessageExtensions(msg_desc.getOptions(),
            msg_desc.getOptions().getAllFields(), custom_extensions);
    if (options_data != null && !options_data.isEmpty()) {
        has_data = true;
        msg_root.put("options", options_data);
    }

    // method
    HashMap<String, Object> child_message = null;
    for (Descriptors.MethodDescriptor sub_desc : msg_desc.getMethods()) {
        HashMap<String, Object> subset = dumpOptionsIntoHashMap(sub_desc, custom_extensions);
        if (subset == null) {
            continue;
        }

        if (child_message == null) {
            child_message = new HashMap<String, Object>();
            msg_root.put("method", child_message);
        }

        has_data = true;
        child_message.put(sub_desc.getName(), subset);
    }

    if (has_data) {
        msg_root.put("name", msg_desc.getName());
        return msg_root;
    }

    return null;
}
 
Example 13
Source Project: karate-grpc   File: ServiceResolver.java    License: MIT License 5 votes vote down vote up
private Descriptors.ServiceDescriptor findService(String serviceName, String packageName) {
    for (Descriptors.FileDescriptor fileDescriptor : fileDescriptors) {
        if (!fileDescriptor.getPackage().equals(packageName)) {
            // Package does not match this file, ignore.
            continue;
        }

        Descriptors.ServiceDescriptor serviceDescriptor = fileDescriptor.findServiceByName(serviceName);
        if (serviceDescriptor != null) {
            return serviceDescriptor;
        }
    }
    throw new IllegalArgumentException("Can't find service with name: " + serviceName);
}
 
Example 14
Source Project: metastore   File: ValidationResults.java    License: Apache License 2.0 5 votes vote down vote up
private ServiceResultContainer getOrCreateService(
    Descriptors.ServiceDescriptor serviceDescriptor) {
  String serviceName = serviceDescriptor.getFullName();
  ServiceResultContainer serviceResult = serviceMap.get(serviceName);
  if (serviceResult == null) {
    serviceResult = new ServiceResultContainer();
    serviceResult.packageName = serviceDescriptor.getFile().getPackage();
    serviceResult.fullName = serviceName;
    serviceMap.put(serviceName, serviceResult);
  }
  return serviceResult;
}
 
Example 15
private void write() {
  PathLocation location = new PathLocation();
  writeLeadingComment(commentIndexer.getSyntaxLocation(), 0);
  switch (fd.getSyntax()) {
    case PROTO2:
      writer.print("syntax = \"proto2\";");
      break;
    case PROTO3:
      writer.print("syntax = \"proto3\";");
      break;
    default:
      break;
  }
  writeTrailingComment(commentIndexer.getSyntaxLocation(), 0);
  writer.println();

  if (!fd.getPackage().isEmpty()) {
    writeLeadingComment(commentIndexer.getPackageLocation(), 0);
    writer.print("package ");
    writer.print(fd.getPackage());
    writer.print(";");
    writeTrailingComment(commentIndexer.getPackageLocation(), 0);
    writer.println();
  }

  List<Descriptors.FileDescriptor> dependencies = fd.getDependencies();
  if (dependencies.size() > 0) {
    int index = 0;
    for (Descriptors.FileDescriptor dependency : dependencies) {
      writeLeadingComment(commentIndexer.importLocations.get(index++), 0);
      writer.print("import \"");
      writer.print(dependency.getName());
      writer.print("\";");
      writeTrailingComment(commentIndexer.importLocations.get(index++), 0);
    }
    writer.println();
  }

  writeOptionsForBlock(fd.getOptions(), 0, "File");

  extensions();

  for (Descriptors.EnumDescriptor enumDescriptor : fd.getEnumTypes()) {
    writer.println();
    writeEnumDescriptor(enumDescriptor, location, 0);
  }

  for (Descriptors.ServiceDescriptor serviceDescriptor : fd.getServices()) {
    writer.println();
    writeServiceDescriptor(serviceDescriptor, location);
  }

  for (Descriptors.Descriptor messageType : fd.getMessageTypes()) {
    writer.println();
    writeMessageDescriptor(messageType, location, 0);
  }
}
 
Example 16
Source Project: metastore   File: ValidationResults.java    License: Apache License 2.0 4 votes vote down vote up
void addResult(Descriptors.ServiceDescriptor descriptor, RuleInfo ruleInfo) {
  ServiceResultContainer serviceResult = getOrCreateService(descriptor);
  serviceResult.addResult(ruleInfo);
}
 
Example 17
Source Project: metastore   File: InMemoryStorage.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void updateServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor) {
  bindDatabase.bindService(resourceUrn, descriptor.getFullName());
}
 
Example 18
Source Project: metastore   File: ValidationResults.java    License: Apache License 2.0 4 votes vote down vote up
void setPatch(Descriptors.ServiceDescriptor fd, ChangeInfo patch) {
  ServiceResultContainer serviceResult = getOrCreateService(fd);
  serviceResult.setPatch(patch);
}
 
Example 19
Source Project: metastore   File: ProtoDiff.java    License: Apache License 2.0 4 votes vote down vote up
private void diffFileDescriptor(
    @Nullable Descriptors.FileDescriptor fdRef, @Nullable Descriptors.FileDescriptor fdNew) {
  List<Descriptors.Descriptor> refDescriptors;
  List<Descriptors.EnumDescriptor> refEnumDescriptors;
  List<Descriptors.ServiceDescriptor> refServiceDescriptors;
  List<String> refDependencies;
  List<Descriptors.Descriptor> newDescriptors;
  List<Descriptors.EnumDescriptor> newEnumDescriptors;
  List<Descriptors.ServiceDescriptor> newServiceDescriptors;
  List<String> newDependencies;
  String fileName = null;
  if (fdRef != null) {
    fileName = fdRef.getFullName();
    refDescriptors = fdRef.getMessageTypes();
    refEnumDescriptors = fdRef.getEnumTypes();
    refServiceDescriptors = fdRef.getServices();
    refDependencies =
        fdRef.getDependencies().stream()
            .map(Descriptors.FileDescriptor::getFullName)
            .collect(Collectors.toList());
  } else {
    results.setPatch(fdNew, ChangeInfo.newBuilder().setChangeType(ChangeType.ADDITION).build());
    refDescriptors = new ArrayList<>(0);
    refEnumDescriptors = new ArrayList<>(0);
    refServiceDescriptors = new ArrayList<>(0);
    refDependencies = new ArrayList<>(0);
  }

  if (fdNew != null) {
    fileName = fdNew.getFullName();
    newDescriptors = fdNew.getMessageTypes();
    newEnumDescriptors = fdNew.getEnumTypes();
    newServiceDescriptors = fdNew.getServices();
    newDependencies =
        fdNew.getDependencies().stream()
            .map(Descriptors.FileDescriptor::getFullName)
            .collect(Collectors.toList());
  } else {
    results.setPatch(fdRef, ChangeInfo.newBuilder().setChangeType(ChangeType.REMOVAL).build());
    newDescriptors = new ArrayList<>(0);
    newEnumDescriptors = new ArrayList<>(0);
    newServiceDescriptors = new ArrayList<>(0);
    newDependencies = new ArrayList<>(0);
  }

  diffMessageTypes(refDescriptors, newDescriptors);
  diffEnumTypes(refEnumDescriptors, newEnumDescriptors);
  diffServices(refServiceDescriptors, newServiceDescriptors);
  diffImports(fileName, refDependencies, newDependencies);
}
 
Example 20
Source Project: metastore   File: BindProvider.java    License: Apache License 2.0 votes vote down vote up
void createServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor);