com.google.protobuf.DescriptorProtos.EnumDescriptorProto Java Examples

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
Source File: GrpcDocStringExtractor.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String appendToFullName(
        DescriptorProto messageDescriptor, List<Integer> path, String fullNameSoFar) {
    switch (path.get(0)) {
        case DescriptorProto.NESTED_TYPE_FIELD_NUMBER:
            final DescriptorProto nestedMessage = messageDescriptor.getNestedType(path.get(1));
            return appendMessageToFullName(nestedMessage, path, fullNameSoFar);
        case DescriptorProto.ENUM_TYPE_FIELD_NUMBER:
            final EnumDescriptorProto enumDescriptor = messageDescriptor.getEnumType(path.get(1));
            return appendEnumToFullName(enumDescriptor, path, fullNameSoFar);
        case DescriptorProto.FIELD_FIELD_NUMBER:
            final FieldDescriptorProto fieldDescriptor = messageDescriptor.getField(path.get(1));
            return appendFieldComponent(fullNameSoFar, fieldDescriptor.getName());
        default:
            return null;
    }
}
 
Example #2
Source File: GrpcDocStringExtractor.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String getFullName(FileDescriptorProto descriptor, List<Integer> path) {
    String fullNameSoFar = descriptor.getPackage();
    switch (path.get(0)) {
        case FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER:
            final DescriptorProto message = descriptor.getMessageType(path.get(1));
            return appendMessageToFullName(message, path, fullNameSoFar);
        case FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER:
            final EnumDescriptorProto enumDescriptor = descriptor.getEnumType(path.get(1));
            return appendEnumToFullName(enumDescriptor, path, fullNameSoFar);
        case FileDescriptorProto.SERVICE_FIELD_NUMBER:
            final ServiceDescriptorProto serviceDescriptor = descriptor.getService(path.get(1));
            fullNameSoFar = appendNameComponent(fullNameSoFar, serviceDescriptor.getName());
            if (path.size() > 2) {
                fullNameSoFar = appendFieldComponent(
                        fullNameSoFar, serviceDescriptor.getMethod(path.get(3)).getName());
            }
            return fullNameSoFar;
        default:
            return null;
    }
}
 
Example #3
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
private EnumDescriptor(final EnumDescriptorProto proto,
                       final FileDescriptor file,
                       final Descriptor parent,
                       final int index)
                throws DescriptorValidationException {
  this.index = index;
  this.proto = proto;
  fullName = computeFullName(file, parent, proto.getName());
  this.file = file;
  containingType = parent;

  if (proto.getValueCount() == 0) {
    // We cannot allow enums with no values because this would mean there
    // would be no valid default value for fields of this type.
    throw new DescriptorValidationException(this,
      "Enums must contain at least one value.");
  }

  values = new EnumValueDescriptor[proto.getValueCount()];
  for (int i = 0; i < proto.getValueCount(); i++) {
    values[i] = new EnumValueDescriptor(
      proto.getValue(i), file, this, i);
  }

  file.pool.addSymbol(this);
}
 
Example #4
Source File: TypesBuilderFromDescriptor.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * TODO (guptasu): Only needed to create NullValue enum.  Check if this can be removed.
 * Create the Protobuf.Enum instance from enumDescriptorProto.
 */
private static Enum createEnum(String enumName,
    EnumDescriptorProto enumDescriptorProto, String fileName) {

  com.google.protobuf.Enum.Builder coreEnumBuilder =
      com.google.protobuf.Enum.newBuilder().setName(enumName);

  coreEnumBuilder.setSyntax(Syntax.SYNTAX_PROTO3);

  for (EnumValueDescriptorProto value : enumDescriptorProto.getValueList()) {
    com.google.protobuf.EnumValue.Builder coreEnumValueBuilder =
        com.google.protobuf.EnumValue.newBuilder();
    coreEnumValueBuilder.setName(value.getName()).setNumber(value.getNumber());
    coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build());
  }

  coreEnumBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName));
  return coreEnumBuilder.build();
}
 
Example #5
Source File: GrpcDocStringExtractor.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String appendEnumToFullName(
        EnumDescriptorProto enumDescriptor, List<Integer> path, String fullNameSoFar) {
    fullNameSoFar = appendNameComponent(fullNameSoFar, enumDescriptor.getName());
    if (path.size() > 2) {
        fullNameSoFar = appendFieldComponent(fullNameSoFar, enumDescriptor.getValue(path.get(3)).getName());
    }
    return fullNameSoFar;
}
 
Example #6
Source File: CommonProto2Java.java    From saluki with Apache License 2.0 5 votes vote down vote up
private void doPrint(FileDescriptorProto fdp, String javaPackage, String outerClassName) {
  List<DescriptorProto> messageDescList = Lists.newArrayList(fdp.getMessageTypeList());
  List<ServiceDescriptorProto> serviceDescList = Lists.newArrayList(fdp.getServiceList());
  List<EnumDescriptorProto> enumDescList = Lists.newArrayList(fdp.getEnumTypeList());
  messageDescList.stream().filter(temp -> temp.getEnumTypeList() != null)
      .forEach(temp -> enumDescList.addAll(temp.getEnumTypeList()));
  printEnum(enumDescList, javaPackage, outerClassName);
  printMessage(messageDescList, javaPackage, outerClassName);
  printService(serviceDescList, javaPackage);
}
 
Example #7
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void decompile(EnumDescriptorProto enumDescriptor) throws IOException {
    indentedFormat("enum %s {", enumDescriptor.getName());
    indent++;
    if (enumDescriptor.hasOptions()) {
        decompileOptions(enumDescriptor.getOptions());
    }
    for (EnumValueDescriptorProto value : enumDescriptor.getValueList()) {
        indentedFormat("%s = %d%s;", value.getName(), value.getNumber(),
                       value.hasOptions() ? bracketedOptions(value.getOptions()) : "");
    }
    indent--;
    indentedFormat("}");
}
 
Example #8
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/** See {@link FileDescriptor#setProto}. */
private void setProto(final EnumDescriptorProto proto) {
  this.proto = proto;

  for (int i = 0; i < values.length; i++) {
    values[i].setProto(proto.getValue(i));
  }
}
 
Example #9
Source File: BuilderVisitor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Accepts
protected void accept(EnumDescriptorProto.Builder enumType) {
  pushParent(BuilderVisitorNodeInfo.create(enumType, currentFile));
  visitRepeated(EnumDescriptorProto.VALUE_FIELD_NUMBER);
  visit(enumType.getOptionsBuilder());
  popExpectedParent(enumType);
}
 
Example #10
Source File: EnumType.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private EnumType(ProtoContainerElement parent, EnumDescriptorProto proto, String path) {
  super(parent, proto.getName(), path);
  this.proto = proto;

  // Build values.
  ImmutableList.Builder<EnumValue> valuesBuilder = ImmutableList.builder();
  List<EnumValueDescriptorProto> valueProtos = proto.getValueList();
  for (int i = 0; i < valueProtos.size(); i++) {
    EnumValueDescriptorProto value = valueProtos.get(i);
    String childPath = buildPath(path, EnumDescriptorProto.VALUE_FIELD_NUMBER, i);
    valuesBuilder.add(EnumValue.create(this, value, childPath));
  }
  values = valuesBuilder.build();
}
 
Example #11
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private EnumDescriptorProto generateEnum(Enum e) {
  EnumDescriptorProto.Builder builder = EnumDescriptorProto.newBuilder();
  builder.setName(getSimpleName(e.getName()));
  for (EnumValue value : e.getEnumvalueList()) {
    EnumValueDescriptorProto.Builder valueBuilder = EnumValueDescriptorProto.newBuilder();
    valueBuilder.setName(value.getName());
    valueBuilder.setNumber(value.getNumber());
    valueBuilder.setOptions(generateEnumValueOptions(value));
    builder.addValue(valueBuilder.build());
  }
  builder.setOptions(generateEnumOptions(e));
  return builder.build();
}
 
Example #12
Source File: CommonProto2Java.java    From saluki with Apache License 2.0 5 votes vote down vote up
private void printEnum(List<EnumDescriptorProto> enumDescList, String javaPackage,
    String outerClassName) {
  for (EnumDescriptorProto enumDesc : enumDescList) {
    String enumClassType = enumDesc.getName();
    String enumPackageName = javaPackage + "." + outerClassName;
    String fullpojoType = enumPackageName.toLowerCase() + "." + enumClassType;
    pojoTypes.put(enumClassType, fullpojoType);
    PrintEnumFile enumFile = new PrintEnumFile(generatePath, enumPackageName, enumClassType);
    try {
      enumFile.setEnumFields(enumDesc.getValueList());
    } finally {
      enumFile.print();
    }
  }
}
 
Example #13
Source File: EnumType.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an enum backed up by the given proto.
 */
public static EnumType create(ProtoContainerElement parent, EnumDescriptorProto proto,
    String path) {
  return new EnumType(parent, proto, path);
}
 
Example #14
Source File: EnumType.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the underlying proto representation.
 */
public EnumDescriptorProto getProto() {
  return proto;
}
 
Example #15
Source File: DescriptorNormalization.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
public static List<Option> getOptions(EnumDescriptorProto descriptor, boolean withDefaults) {
  return toCoreOptions(maybeCombineOptionsWithDefault(withDefaults,
      descriptor.getOptions().getAllFields(), DEFAULT_ENUM_OPTIONS));
}
 
Example #16
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
/** Convert the descriptor to its protocol message representation. */
@Override
public EnumDescriptorProto toProto() {
  return proto;
}
 
Example #17
Source File: DescriptorNormalization.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
public static List<Option> getOptions(EnumDescriptorProto descriptor) {
  return getOptions(descriptor, true);
}
 
Example #18
Source File: TestRPC.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public EnumDescriptorProto exchangeProto(EnumDescriptorProto arg) {
  return arg;
}
 
Example #19
Source File: TestRPC.java    From big-c with Apache License 2.0 4 votes vote down vote up
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);
 
Example #20
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the translate/crosslink for an example with a more complex namespace
 * referencing.
 */
public void testComplexNamespacePublicDependency() throws Exception {
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("bar.proto")
      .setPackage("a.b.c.d.bar.shared")
      .addEnumType(EnumDescriptorProto.newBuilder()
          .setName("MyEnum")
          .addValue(EnumValueDescriptorProto.newBuilder()
              .setName("BLAH")
              .setNumber(1)))
      .build();
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto")
      .addDependency("bar.proto")
      .setPackage("a.b.c.d.foo.shared")
      .addMessageType(DescriptorProto.newBuilder()
          .setName("MyMessage")
          .addField(FieldDescriptorProto.newBuilder()
              .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED)
              .setTypeName("bar.shared.MyEnum")
              .setName("MyField")
              .setNumber(1)))
      .build();
  // translate and crosslink
  FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(
      fooProto, new FileDescriptor[0]);
  FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(
      barProto, new FileDescriptor[]{fooFile});
  // verify resulting descriptors
  assertNotNull(barFile);
  List<Descriptor> msglist = barFile.getMessageTypes();
  assertNotNull(msglist);
  assertTrue(msglist.size() == 1);
  Descriptor desc = msglist.get(0);
  if (desc.getName().equals("MyMessage")) {
    assertNotNull(desc.getFields());
    List<FieldDescriptor> fieldlist = desc.getFields();
    assertNotNull(fieldlist);
    assertTrue(fieldlist.size() == 1);
    FieldDescriptor field = fieldlist.get(0);
    assertTrue(field.getType() == FieldDescriptor.Type.ENUM);
    assertTrue(field.getEnumType().getName().equals("MyEnum"));
    assertTrue(field.getEnumType().getFile().getName().equals("bar.proto"));
    assertTrue(field.getEnumType().getFile().getPackage().equals(
        "a.b.c.d.bar.shared"));
  }   
}
 
Example #21
Source File: EnumDefinition.java    From protobuf-dynamic with Apache License 2.0 4 votes vote down vote up
EnumDescriptorProto getEnumType() {
	return mEnumType;
}
 
Example #22
Source File: EnumDefinition.java    From protobuf-dynamic with Apache License 2.0 4 votes vote down vote up
private EnumDefinition(EnumDescriptorProto enumType) {
	mEnumType = enumType;
}
 
Example #23
Source File: EnumDefinition.java    From protobuf-dynamic with Apache License 2.0 4 votes vote down vote up
private Builder(String enumName) {
	mEnumTypeBuilder = EnumDescriptorProto.newBuilder();
	mEnumTypeBuilder.setName(enumName);
}
 
Example #24
Source File: TestRPC.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public EnumDescriptorProto exchangeProto(EnumDescriptorProto arg) {
  return arg;
}
 
Example #25
Source File: TestRPC.java    From hadoop with Apache License 2.0 4 votes vote down vote up
DescriptorProtos.EnumDescriptorProto exchangeProto(
DescriptorProtos.EnumDescriptorProto arg);