Java Code Examples for com.google.protobuf.Descriptors#EnumDescriptor

The following examples show how to use com.google.protobuf.Descriptors#EnumDescriptor . 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: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Collection<Descriptors.EnumDescriptor> findEnumDescriptorsByOption(String optionName) {
  Descriptors.FieldDescriptor fieldDescriptor = optionsCatalog.getEnumOptionByName(optionName);
  return enumMap.values().stream()
      .filter(
          descriptor -> {
            DescriptorProtos.EnumOptions options = descriptor.getOptions();
            UnknownFieldSet.Field unknown =
                options.getUnknownFields().getField(fieldDescriptor.getNumber());
            if (unknown.getLengthDelimitedList().size()
                    + unknown.getFixed64List().size()
                    + unknown.getFixed32List().size()
                    + unknown.getVarintList().size()
                > 0) {
              return true;
            }
            return options.getAllFields().containsKey(fieldDescriptor);
          })
      .collect(Collectors.toList());
}
 
Example 2
Source File: DataDstPb.java    From xresloader with MIT License 6 votes vote down vote up
static Descriptors.EnumValueDescriptor get_enum_value(PbInfoSet pbs, Descriptors.EnumDescriptor enum_desc,
        Integer val) {
    String name = enum_desc.getFullName();
    while (!name.isEmpty() && '.' == name.charAt(0)) {
        name = name.substring(1);
    }
    HashMap<Integer, Descriptors.EnumValueDescriptor> cache_set = pbs.enum_values_descs.getOrDefault(name, null);
    if (cache_set == null) {
        cache_set = new HashMap<Integer, Descriptors.EnumValueDescriptor>();
        pbs.enum_values_descs.put(name, cache_set);
        for (Descriptors.EnumValueDescriptor enum_val : enum_desc.getValues()) {
            cache_set.put(Integer.valueOf(enum_val.getNumber()), enum_val);
        }
    }

    return cache_set.getOrDefault(val, null);
}
 
Example 3
Source File: ProtoToAvroSchema.java    From metastore with Apache License 2.0 6 votes vote down vote up
private AvroSchemaItem toEnumField(
    List<Descriptors.EnumDescriptor> enumTypes, String enumFullName) {
  AvroSchemaItem enumField = new AvroSchemaItem();

  final Optional<Descriptors.EnumDescriptor> first =
      enumTypes.stream().filter(f -> f.getFullName().startsWith(enumFullName)).findFirst();

  enumField.setType("enum");
  enumField.setName(first.get().getName());

  List<String> values = new ArrayList<>();
  first.get().getValues().forEach(item -> values.add(item.getName()));
  enumField.setSymbols(values);

  return enumField;
}
 
Example 4
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 6 votes vote down vote up
private void writeEnumDescriptor(
    Descriptors.EnumDescriptor enumType, PathLocation parent, int indent) {
  PathLocation location = parent.addEnum(enumType);
  writeLeadingComment(commentIndexer.getLocation(location), indent);
  indent(indent);
  writer.print("enum ");
  writer.print(enumType.getName());
  writer.println(" {");
  writeOptionsForBlock(enumType.getOptions(), indent + 1, "Enum");
  for (Descriptors.EnumValueDescriptor value : enumType.getValues()) {
    indent(indent + 1);
    writer.print(value.getName());
    writer.print(" = ");
    writer.print(value.getNumber());
    writeOptionsForList(value.getOptions(), indent + 1, "EnumValue");
    writer.println(";");
  }
  indent(indent);
  writer.print("}");
  writeTrailingComment(commentIndexer.getLocation(location), indent);
}
 
Example 5
Source File: ProtoDomainTest.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Test
public void findEnumDescriptorsByOption() throws IOException {
  ProtoDomain domain = TestSets.baseMultipleOptions();
  Collection<Descriptors.EnumDescriptor> options =
      domain.findEnumDescriptorsByOption("test.v1.enum_option");
  // TODO Add more sets with enums
  Assert.assertEquals(0, options.size());
}
 
Example 6
Source File: ProtoToJsonSchema.java    From metastore with Apache License 2.0 5 votes vote down vote up
private void addToEnumsMap(
    ObjectMapper mapper, String fieldName, List<Descriptors.EnumDescriptor> enums) {
  enums.forEach(
      item -> {
        addToEnumsMap(mapper, fieldName, item);
      });
}
 
Example 7
Source File: ProtoToJsonSchema.java    From metastore with Apache License 2.0 5 votes vote down vote up
private void addToEnumsMap(
    ObjectMapper mapper, String fieldName, Descriptors.EnumDescriptor item) {
  ObjectNode nodeEnum = mapper.createObjectNode();
  ArrayNode nodeArray = nodeEnum.putArray("enum");
  item.getValues()
      .forEach(
          e -> {
            nodeArray.add(e.getName());
          });
  String key = fieldName == null ? item.getFullName() : fieldName;
  enumMaps.put(key, nodeEnum);
}
 
Example 8
Source File: ProtoMessageConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
/**
 * Fills lookup structure for translating between parquet enum values and Protocol buffer enum values.
 * */
private Map<Binary, Descriptors.EnumValueDescriptor> makeLookupStructure(Descriptors.FieldDescriptor enumFieldType) {
  Descriptors.EnumDescriptor enumType = enumFieldType.getEnumType();
  Map<Binary, Descriptors.EnumValueDescriptor> lookupStructure = new HashMap<Binary, Descriptors.EnumValueDescriptor>();

  List<Descriptors.EnumValueDescriptor> enumValues = enumType.getValues();

  for (Descriptors.EnumValueDescriptor value : enumValues) {
    String name = value.getName();
    lookupStructure.put(Binary.fromString(name), enumType.findValueByName(name));
  }

  return lookupStructure;
}
 
Example 9
Source File: ValidationResults.java    From metastore with Apache License 2.0 5 votes vote down vote up
private EnumResultContainer getOrCreateEnum(Descriptors.EnumDescriptor enumDescriptor) {
  String fileName = enumDescriptor.getFullName();
  EnumResultContainer enumResult = enumMap.get(fileName);
  if (enumResult == null) {
    enumResult = new EnumResultContainer();
    enumResult.packageName = enumDescriptor.getFile().getPackage();
    enumResult.fullName = fileName;
    enumMap.put(fileName, enumResult);
  }
  return enumResult;
}
 
Example 10
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static void addEnums(
    Map<String, Descriptors.GenericDescriptor> descriptors,
    Descriptors.FileDescriptor descriptor,
    String packageName) {
  for (Descriptors.EnumDescriptor message : descriptor.getEnumTypes()) {
    String fullName = packageName + message.getName();
    descriptors.put(fullName, message);
  }
}
 
Example 11
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 4 votes vote down vote up
private void write() {
  PathLocation location = new PathLocation();
  writeLeadingComment(commentIndexer.getSyntaxLocation(), 0);
  switch (fd.getSyntax()) {
    case PROTO2:
      writer.print("syntax = \"proto2\";");
      break;
    case PROTO3:
      writer.print("syntax = \"proto3\";");
      break;
    default:
      break;
  }
  writeTrailingComment(commentIndexer.getSyntaxLocation(), 0);
  writer.println();

  if (!fd.getPackage().isEmpty()) {
    writeLeadingComment(commentIndexer.getPackageLocation(), 0);
    writer.print("package ");
    writer.print(fd.getPackage());
    writer.print(";");
    writeTrailingComment(commentIndexer.getPackageLocation(), 0);
    writer.println();
  }

  List<Descriptors.FileDescriptor> dependencies = fd.getDependencies();
  if (dependencies.size() > 0) {
    int index = 0;
    for (Descriptors.FileDescriptor dependency : dependencies) {
      writeLeadingComment(commentIndexer.importLocations.get(index++), 0);
      writer.print("import \"");
      writer.print(dependency.getName());
      writer.print("\";");
      writeTrailingComment(commentIndexer.importLocations.get(index++), 0);
    }
    writer.println();
  }

  writeOptionsForBlock(fd.getOptions(), 0, "File");

  extensions();

  for (Descriptors.EnumDescriptor enumDescriptor : fd.getEnumTypes()) {
    writer.println();
    writeEnumDescriptor(enumDescriptor, location, 0);
  }

  for (Descriptors.ServiceDescriptor serviceDescriptor : fd.getServices()) {
    writer.println();
    writeServiceDescriptor(serviceDescriptor, location);
  }

  for (Descriptors.Descriptor messageType : fd.getMessageTypes()) {
    writer.println();
    writeMessageDescriptor(messageType, location, 0);
  }
}
 
Example 12
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 4 votes vote down vote up
PathLocation addEnum(Descriptors.EnumDescriptor type) {
  return add('E', type.getIndex());
}
 
Example 13
Source File: ProtoLanguageFileWriter.java    From metastore with Apache License 2.0 4 votes vote down vote up
private void writeFieldType(Descriptors.FieldDescriptor field) {
  if (field.isRepeated() && !isMap(field)) {
    writer.print("repeated ");
  }
  switch (field.getType()) {
    case UINT64:
      writer.print("uint64");
      break;
    case INT32:
      writer.print("int32");
      break;
    case INT64:
      writer.print("int64");
      break;
    case FIXED64:
      writer.print("fixed64");
      break;
    case FIXED32:
      writer.print("fixed32");
      break;
    case BOOL:
      writer.print("bool");
      break;
    case STRING:
      writer.print("string");
      break;
    case GROUP:
      // TODO figure out if we need to support this (proto2)
      writer.print("GROUP");
      break;
    case MESSAGE:
      {
        Descriptors.Descriptor messageType = field.getMessageType();
        if (messageType.getFile() == fd) {
          if (isMap(field)) {
            writer.print("map<");
            writeFieldType(messageType.findFieldByNumber(1));
            writer.print(", ");
            writeFieldType(messageType.findFieldByNumber(2));
            writer.print(">");
          } else {
            writer.print(messageType.getName());
          }
        } else {
          writer.print(messageType.getFullName());
        }
        break;
      }
    case BYTES:
      writer.print("bytes");
      break;
    case UINT32:
      writer.print("uint32");
      break;
    case ENUM:
      {
        Descriptors.EnumDescriptor enumType = field.getEnumType();
        if (enumType.getFile() == fd) {
          writer.print(enumType.getName());
        } else {
          writer.print(enumType.getFullName());
        }
        break;
      }
    case SFIXED32:
      writer.print("sfixed32");
      break;
    case SFIXED64:
      writer.print("sfixed64");
      break;
    case SINT32:
      writer.print("sint32");
      break;
    case SINT64:
      writer.print("sint64");
      break;
    case DOUBLE:
      writer.print("double");
      break;
    case FLOAT:
      writer.print("float");
      break;
  }
}
 
Example 14
Source File: ProtoDomain.java    From metastore with Apache License 2.0 4 votes vote down vote up
public Descriptors.EnumDescriptor getEnumDescriptorByName(String messageName) {
  return enumMap.get(messageName);
}
 
Example 15
Source File: XmlFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field) throws ParseException {
    Object value = null;
    switch (field.getType()) {
        case INT32:
        case SINT32:
        case SFIXED32:
            value = tokenizer.consumeInt32();
            break;

        case INT64:
        case SINT64:
        case SFIXED64:
            value = tokenizer.consumeInt64();
            break;

        case UINT32:
        case FIXED32:
            value = tokenizer.consumeUInt32();
            break;

        case UINT64:
        case FIXED64:
            value = tokenizer.consumeUInt64();
            break;

        case FLOAT:
            value = tokenizer.consumeFloat();
            break;

        case DOUBLE:
            value = tokenizer.consumeDouble();
            break;

        case BOOL:
            value = tokenizer.consumeBoolean();
            break;

        case STRING:
            value = tokenizer.consumeString();
            break;

        case BYTES:
            value = tokenizer.consumeByteString();
            break;

        case ENUM: {
            Descriptors.EnumDescriptor enumType = field.getEnumType();

            if (tokenizer.lookingAtInteger()) {
                int number = tokenizer.consumeInt32();
                value = enumType.findValueByNumber(number);
                if (value == null) {
                    throw tokenizer.parseExceptionPreviousToken("Enum type \""
                                                                + enumType.getFullName()
                                                                + "\" has no value with number "
                                                                + number + ".");
                }
            } else {
                String id = tokenizer.consumeIdentifier();
                value = enumType.findValueByName(id);
                if (value == null) {
                    throw tokenizer.parseExceptionPreviousToken("Enum type \""
                                                                + enumType.getFullName()
                                                                + "\" has no value named \""
                                                                + id + "\".");
                }
            }

            break;
        }

        case MESSAGE:
        case GROUP:
            throw new RuntimeException("Can't get here.");
    }
    return value;
}
 
Example 16
Source File: ValidationResults.java    From metastore with Apache License 2.0 4 votes vote down vote up
void setPatch(Descriptors.EnumDescriptor fd, ChangeInfo patch) {
  EnumResultContainer resultContainer = getOrCreateEnum(fd);
  resultContainer.setPatch(patch);
}
 
Example 17
Source File: ProtoDescriptorsToJs.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private static Map<String, ?> asRecord(Descriptors.EnumDescriptor descriptor) {
  return ImmutableMap.of(
      "enum", descriptor.toProto(),
      "name", descriptor.getFullName());
}
 
Example 18
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
Object convertToProtoValue(FieldDescriptor fieldDescriptor, Object value) {
  Descriptors.EnumDescriptor enumType = fieldDescriptor.getEnumType();
  return enumType.findValueByNumber(((EnumerationType.Value) value).getValue());
}
 
Example 19
Source File: OpenApiDefinitionHandler.java    From grpc-swagger with MIT License 4 votes vote down vote up
private FieldProperty parseFieldProperty(Descriptors.FieldDescriptor fieldDescriptor) {
    Descriptors.FieldDescriptor.Type type = fieldDescriptor.getType();
    
    FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getByFieldType(type);
    
    FieldProperty fieldProperty = new FieldProperty();
    if (fieldDescriptor.isRepeated()) {
        // map
        if (type == Descriptors.FieldDescriptor.Type.MESSAGE
                && fieldDescriptor.getMessageType().getOptions().getMapEntry()) {
            fieldProperty.setType(FieldTypeEnum.OBJECT.getType());
            Descriptor messageType = fieldDescriptor.getMessageType();
            Descriptors.FieldDescriptor mapValueType = messageType.getFields().get(1);
            fieldProperty.setAdditionalProperties(parseFieldProperty(mapValueType));
        } else { // array
            fieldProperty.setType(FieldTypeEnum.ARRAY.getType());
            Items items = new Items();
            items.setType(fieldTypeEnum.getType());
            items.setFormat(fieldTypeEnum.getFormat());
            if (fieldTypeEnum == FieldTypeEnum.OBJECT) {
                items.setRef(findRefByType(fieldDescriptor.getMessageType()));
            }
            fieldProperty.setItems(items);
        }
    }
    // object reference
    else if (fieldTypeEnum == FieldTypeEnum.OBJECT) {
        fieldProperty.setRef(findRefByType(fieldDescriptor.getMessageType()));
    }
    // enum
    else if (fieldTypeEnum == FieldTypeEnum.ENUM) {
        fieldProperty.setType(FieldTypeEnum.ENUM.getType());
        List<String> enums = new ArrayList<>();
        Descriptors.EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType();
        enumDescriptor.getValues().forEach(enumValueDescriptor -> enums.add(enumValueDescriptor.getName()));
        fieldProperty.setEnums(enums);
    }
    // other simple types
    else {
        fieldProperty.setType(fieldTypeEnum.getType());
        fieldProperty.setFormat(fieldTypeEnum.getFormat());
    }
    return fieldProperty;
}
 
Example 20
Source File: ProtoLint.java    From metastore with Apache License 2.0 votes vote down vote up
private void lintOnEnum(Descriptors.EnumDescriptor enu) {}