下面列出了com.google.protobuf.Descriptors#ServiceDescriptor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
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);
}
}
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);
}
}
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);
}
/**
* 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));
}
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);
}
@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());
}
/**
* 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;
}
/**
* 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);
}
});
}
@Override
public void updateServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor) {
try (Scope scope =
TRACER
.spanBuilder("GoogleDataCatalog.updateResourceBinding")
.setRecordEvents(true)
.startScopedSpan()) {}
}
@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();
}
}
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;
}
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);
}
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;
}
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);
}
}
void addResult(Descriptors.ServiceDescriptor descriptor, RuleInfo ruleInfo) {
ServiceResultContainer serviceResult = getOrCreateService(descriptor);
serviceResult.addResult(ruleInfo);
}
@Override
public void updateServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor) {
bindDatabase.bindService(resourceUrn, descriptor.getFullName());
}
void setPatch(Descriptors.ServiceDescriptor fd, ChangeInfo patch) {
ServiceResultContainer serviceResult = getOrCreateService(fd);
serviceResult.setPatch(patch);
}
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);
}
void createServiceBinding(String resourceUrn, Descriptors.ServiceDescriptor descriptor);