下面列出了com.google.protobuf.DescriptorProtos#EnumDescriptorProto ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void writeEnum() throws Exception {
DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
DescriptorProtos.FileDescriptorProto.newBuilder()
.setName("test")
.setSyntax("proto3")
.addDependency("google/protobuf/descriptor.proto");
DescriptorProtos.EnumDescriptorProto enumDescriptor =
DescriptorProtos.EnumDescriptorProto.newBuilder()
.setName("WriteEnum")
.setOptions(TestProto.ENUM_OPTIONS)
.addValue(
DescriptorProtos.EnumValueDescriptorProto.newBuilder()
.setNumber(0)
.setName("WRITE_ENUM_UNSET")
.setOptions(TestProto.ENUM_VALUE_OPTIONS)
.build())
.build();
fileDescriptorProtoBuilder.addEnumType(enumDescriptor);
assertEnum(fileDescriptorProtoBuilder.build(), null);
}
/**
* Iterate through a component's path inside a proto descriptor.
*
* <p>The path is a tuple of component type and a relative position For example: [4, 1, 3, 2, 2,
* 1] or [MESSAGE_TYPE_FIELD_NUMBER, 1, NESTED_TYPE_FIELD_NUMBER, 2, FIELD_FIELD_NUMBER, 1] is
* representing the second field of the third nested message in the second message in the file
*
* @see DescriptorProtos.SourceCodeInfoOrBuilder for more info
* @param descriptor proto file descriptor
* @param path path of the element
* @return full element's path as a string
*/
private static Optional<String> getFullName(
DescriptorProtos.FileDescriptorProto descriptor, List<Integer> path) {
String fullName = descriptor.getPackage();
switch (path.get(0)) {
case DescriptorProtos.FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER:
DescriptorProtos.EnumDescriptorProto enumDescriptor = descriptor.getEnumType(path.get(1));
return Optional.of(appendEnum(enumDescriptor, path, fullName));
case DescriptorProtos.FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER:
DescriptorProtos.DescriptorProto message = descriptor.getMessageType(path.get(1));
return appendMessage(message, path, fullName);
case DescriptorProtos.FileDescriptorProto.SERVICE_FIELD_NUMBER:
DescriptorProtos.ServiceDescriptorProto serviceDescriptor =
descriptor.getService(path.get(1));
fullName += appendNameComponent(serviceDescriptor.getName());
if (path.size() > 2) {
fullName += appendNameComponent(serviceDescriptor.getMethod(path.get(3)).getName());
}
return Optional.of(fullName);
default:
return Optional.empty();
}
}
private static Optional<String> append(
DescriptorProtos.DescriptorProto messageDescriptor, List<Integer> path, String fullName) {
switch (path.get(0)) {
case DescriptorProtos.DescriptorProto.NESTED_TYPE_FIELD_NUMBER:
DescriptorProtos.DescriptorProto nestedMessage =
messageDescriptor.getNestedType(path.get(1));
return appendMessage(nestedMessage, path, fullName);
case DescriptorProtos.DescriptorProto.ENUM_TYPE_FIELD_NUMBER:
DescriptorProtos.EnumDescriptorProto enumDescriptor =
messageDescriptor.getEnumType(path.get(1));
return Optional.of(appendEnum(enumDescriptor, path, fullName));
case DescriptorProtos.DescriptorProto.FIELD_FIELD_NUMBER:
DescriptorProtos.FieldDescriptorProto fieldDescriptor =
messageDescriptor.getField(path.get(1));
return Optional.of(fullName + appendNameComponent(fieldDescriptor.getName()));
default:
return Optional.empty();
}
}
public DataVerifyPbEnum(DescriptorProtos.EnumDescriptorProto desc) {
super(desc.getName());
for (DescriptorProtos.EnumValueDescriptorProto val_desc : desc.getValueList()) {
all_names.put(val_desc.getName(), (long) val_desc.getNumber());
all_numbers.add((long) val_desc.getNumber());
// alias extension
if (val_desc.getOptions().hasExtension(Xresloader.enumAlias)) {
String alias_name = val_desc.getOptions().getExtension(Xresloader.enumAlias);
if (!alias_name.isEmpty()) {
all_names.put(alias_name, (long) val_desc.getNumber());
}
}
}
}
@Test
public void writeFile() throws Exception {
DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
DescriptorProtos.FileDescriptorProto.newBuilder()
.setName("test")
.setSyntax("proto3")
.addDependency("google/protobuf/descriptor.proto")
.setOptions(TestProto.FILE_OPTIONS);
DescriptorProtos.EnumDescriptorProto enumDescriptor =
DescriptorProtos.EnumDescriptorProto.newBuilder()
.setName("Proto3FileEnum")
.addValue(
DescriptorProtos.EnumValueDescriptorProto.newBuilder()
.setNumber(0)
.setName("PROTO3_FILE_ENUM_UNSET")
.build())
.build();
DescriptorProtos.DescriptorProto.Builder descriptor =
DescriptorProtos.DescriptorProto.newBuilder();
descriptor.setName("Proto3FileMessage");
fileDescriptorProtoBuilder.addEnumType(enumDescriptor);
fileDescriptorProtoBuilder.addMessageType(descriptor);
assertFile(fileDescriptorProtoBuilder.build(), null);
}
private static String appendEnum(
DescriptorProtos.EnumDescriptorProto enumDescriptor, List<Integer> path, String fullName) {
fullName += appendNameComponent(enumDescriptor.getName());
if (path.size() > 2) {
fullName += appendNameComponent(enumDescriptor.getValue(path.get(3)).getName());
}
return fullName;
}
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);