Java Code Examples for com.google.protobuf.Descriptors.EnumDescriptor#getValues()

The following examples show how to use com.google.protobuf.Descriptors.EnumDescriptor#getValues() . 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: ProtoTypeAdapter.java    From gson with Apache License 2.0 6 votes vote down vote up
/**
 * Finds an enum value in the given {@link EnumDescriptor} that matches the given JSON element,
 * either by name if the current adapter is using {@link EnumSerialization#NAME}, otherwise by
 * number. If matching by name, it uses the extension value if it is defined, otherwise it uses
 * its default value.
 *
 * @throws IllegalArgumentException if a matching name/number was not found
 */
private EnumValueDescriptor findValueByNameAndExtension(EnumDescriptor desc,
    JsonElement jsonElement) {
  if (enumSerialization == EnumSerialization.NAME) {
    // With enum name
    for (EnumValueDescriptor enumDesc : desc.getValues()) {
      String enumValue = getCustSerializedEnumValue(enumDesc.getOptions(), enumDesc.getName());
      if (enumValue.equals(jsonElement.getAsString())) {
        return enumDesc;
      }
    }
    throw new IllegalArgumentException(
        String.format("Unrecognized enum name: %s", jsonElement.getAsString()));
  } else {
    // With enum value
    EnumValueDescriptor fieldValue = desc.findValueByNumber(jsonElement.getAsInt());
    if (fieldValue == null) {
      throw new IllegalArgumentException(
          String.format("Unrecognized enum value: %s", jsonElement.getAsInt()));
    }
    return fieldValue;
  }
}
 
Example 2
Source File: MetaDataEvolutionValidator.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private void validateEnum(@Nonnull String fieldName, @Nonnull EnumDescriptor oldEnumDescriptor, @Nonnull EnumDescriptor newEnumDescriptor) {
    for (Descriptors.EnumValueDescriptor oldEnumValue : oldEnumDescriptor.getValues()) {
        Descriptors.EnumValueDescriptor newEnumValue = newEnumDescriptor.findValueByNumber(oldEnumValue.getNumber());
        if (newEnumValue == null) {
            throw new MetaDataException("enum removes value",
                    LogMessageKeys.FIELD_NAME, fieldName);
        }
    }
}
 
Example 3
Source File: ProtoToGql.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
static GraphQLEnumType convert(
    EnumDescriptor descriptor, SchemaOptions schemaOptions) {
  GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum().name(getReferenceName(descriptor));
  for (EnumValueDescriptor value : descriptor.getValues()) {
    builder.value(
        value.getName(),
        value.getName(),
        schemaOptions.commentsMap().get(value.getFullName()),
        value.getOptions().getDeprecated() ? "deprecated in proto" : null);
  }
  return builder.build();
}
 
Example 4
Source File: ProtoGenerator.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
private void generateProtoFromDescriptor(EnumDescriptor descriptor,
                                         Appendable out, String indent) throws IOException {
    out.append(indent + "enum " + descriptor.getName() + " {\n");
    for (EnumValueDescriptor valueDescriptor : descriptor.getValues()) {
        generateProtoFromDescriptor(valueDescriptor, out, indent + "    ");
    }

    out.append(indent + "}\n");
}
 
Example 5
Source File: ProtoFuzzer.java    From bundletool with Apache License 2.0 4 votes vote down vote up
private static Object randomEnum(EnumDescriptor enumDescriptor) {
  List<EnumValueDescriptor> enumConstants = enumDescriptor.getValues();
  return enumConstants.get(RAND.nextInt(enumConstants.size()));
}
 
Example 6
Source File: RandomProtobufGenerator.java    From swellrt with Apache License 2.0 4 votes vote down vote up
private EnumValueDescriptor getRandomEnum(EnumDescriptor enumD) {
  List<EnumValueDescriptor> values = enumD.getValues();
  return values.get(random.nextInt(values.size()));
}
 
Example 7
Source File: RandomProtobufGenerator.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
private EnumValueDescriptor getRandomEnum(EnumDescriptor enumD) {
  List<EnumValueDescriptor> values = enumD.getValues();
  return values.get(random.nextInt(values.size()));
}