com.google.protobuf.Enum Java Examples

The following examples show how to use com.google.protobuf.Enum. 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: DescriptorNormalizer.java    From api-compiler with Apache License 2.0 7 votes vote down vote up
@VisitsBefore
void normalize(EnumType enumType) {
  Enum.Builder coreEnumBuilder = Enum.newBuilder().setName(enumType.getFullName());
  coreEnumBuilder.setSourceContext(
      SourceContext.newBuilder()
          .setFileName(enumType.getFile().getLocation().getDisplayString()));
  coreEnumBuilder.setSyntax(enumType.getSyntax());

  for (EnumValue value : enumType.getReachableValues()) {
    com.google.protobuf.EnumValue.Builder coreEnumValueBuilder =
        com.google.protobuf.EnumValue.newBuilder();

    // Use simple name for enum value, as otherwise client has to use
    // fully qualified name in the request.
    coreEnumValueBuilder.setName(value.getSimpleName()).setNumber(value.getNumber());

    coreEnumValueBuilder.addAllOptions(
        DescriptorNormalization.getOptions(value.getProto(), includeDefaults));
    coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build());
  }

  coreEnumBuilder.addAllOptions(
      DescriptorNormalization.getOptions(enumType.getProto(), includeDefaults));

  enums.add(coreEnumBuilder.build());
}
 
Example #2
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorProto generateFile(String name, FileContents contents) {
  FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder();
  fileBuilder.setName(name);
  if (!Strings.isNullOrEmpty(contents.packageName)) {
    fileBuilder.setPackage(contents.packageName);
  }
  for (Api api : contents.apis) {
    fileBuilder.addService(generateApi(api));
  }
  for (Type type : contents.types.values()) {
    fileBuilder.addMessageType(generateType(type, contents));
  }
  for (Enum e : contents.enums) {
    fileBuilder.addEnumType(generateEnum(e));
  }
  if (imports.containsKey(name)) {
    for (String imported : imports.get(name)) {
      fileBuilder.addDependency(imported);
    }
  }
  return fileBuilder.build();
}
 
Example #3
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 #4
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMismatch() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  // Enum's name and Type's name are both strings with tag 1.
  Enum altProto = Enum.newBuilder().setName(proto.getName()).build();
  assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto)));
}
 
Example #5
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #6
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 #7
Source File: TypesBuilderFromDescriptor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Creates additional Enums (NullValue) to be added to the Service config.
 */
// TODO (guptasu): Fix this hack. Find a better way to add the predefined types.
// TODO (guptasu): Add them only when required and not in all cases.
static Iterable<Enum> createAdditionalServiceEnums() {
  // TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws.
  // Needs investigation.
  String fileName = "struct.proto";
  List<Enum> additionalEnums = Lists.newArrayList();
  additionalEnums.add(TypesBuilderFromDescriptor.createEnum(NullValue.getDescriptor().getFullName(),
      NullValue.getDescriptor().toProto(), fileName));
  return additionalEnums;
}
 
Example #8
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMismatch() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  // Enum's name and Type's name are both strings with tag 1.
  Enum altProto = Enum.newBuilder().setName(proto.getName()).build();
  assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto)));
}
 
Example #9
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #10
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
private EnumOptions generateEnumOptions(Enum e) {
  EnumOptions.Builder builder = EnumOptions.newBuilder();
  setOptions(builder, e.getOptionsList(), ENUM_OPTION_NAME_PREFIX);
  return builder.build();
}