Java Code Examples for com.google.protobuf.DescriptorProtos#EnumDescriptorProto

The following examples show how to use com.google.protobuf.DescriptorProtos#EnumDescriptorProto . 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
@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);
}
 
Example 2
Source Project: rejoiner   File: DescriptorSet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
  }
}
 
Example 3
Source Project: rejoiner   File: DescriptorSet.java    License: Apache License 2.0 6 votes vote down vote up
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();
  }
}
 
Example 4
Source Project: xresloader   File: DataVerifyPbEnum.java    License: MIT License 6 votes vote down vote up
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());
            }
        }
    }
}
 
Example 5
@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);
}
 
Example 6
Source Project: rejoiner   File: DescriptorSet.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source Project: hadoop   File: TestRPC.java    License: Apache License 2.0 4 votes vote down vote up
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);
 
Example 8
Source Project: big-c   File: TestRPC.java    License: Apache License 2.0 4 votes vote down vote up
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);