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

The following examples show how to use com.google.protobuf.Descriptors#Descriptor . 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: ShadowTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void addMessageOptionDeltaTest() throws Exception {
  ProtoDomain baseAddMessageOption = TestSets.baseAddMessageOption();
  ProtoDomain base = TestSets.baseKnownOption();

  ValidationResults results = new ValidationResults();
  ProtoDiff diff = new ProtoDiff(base, baseAddMessageOption, results);
  diff.diffOnMessage("test.v1.ProtoBeamBasicMessage");

  Report result = results.createProto();
  System.out.println(result);

  //    ShadowRegistry shadowRegistry = new ShadowRegistry(base, result);
  //    shadowRegistry.setDelta(result);
  ProtoDomain shadow = new ShadowApply().applyDelta(base, result);

  Descriptors.Descriptor expectedDescriptor =
      baseAddMessageOption.getDescriptorByName("test.v1.ProtoBeamBasicMessage");
  Descriptors.Descriptor actualDescriptor =
      shadow.getDescriptorByName("test.v1.ProtoBeamBasicMessage");
  Assert.assertArrayEquals(
      expectedDescriptor.toProto().toByteArray(), actualDescriptor.toProto().toByteArray());
}
 
Example 2
Source File: ProtoConcatenatorTest.java    From garmadon with Apache License 2.0 6 votes vote down vote up
@Test
public void concatenateDifferentMessages() throws Descriptors.DescriptorValidationException {
    long kafkaOffset = 0L;
    DynamicMessage.Builder headerMessageBuilder = createHeaderMessageBuilder();
    Descriptors.Descriptor headerMsgDesc = headerMessageBuilder.getDescriptorForType();

    headerMessageBuilder.setField(headerMsgDesc.findFieldByName("id"), 1)
            .setField(headerMsgDesc.findFieldByName("name"), "one");

    DynamicMessage.Builder bodyMessageBuilder = createBodyBuilder();
    Descriptors.Descriptor bodyMsgDesc = bodyMessageBuilder.getDescriptorForType();

    bodyMessageBuilder.setField(bodyMsgDesc.findFieldByName("bodyInt"), 2)
            .setField(bodyMsgDesc.findFieldByName("bodyString"), "two");

    Map<String, Object> expectedValues = new HashMap<>();
    expectedValues.put("id", 1);
    expectedValues.put("name", "one");
    expectedValues.put("bodyInt", 2);
    expectedValues.put("bodyString", "two");
    expectedValues.put(ProtoConcatenator.TIMESTAMP_FIELD_NAME, 0L);
    expectedValues.put(ProtoConcatenator.KAFKA_OFFSET, kafkaOffset);

    testAllOutTypesWith(0L, Arrays.asList(headerMessageBuilder.build(), bodyMessageBuilder.build()), expectedValues, kafkaOffset);
}
 
Example 3
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static void handleMapField(
    Record record,
    Field field,
    String fieldPath,
    Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
    Map<String, Object> defaultValueMap,
    Descriptors.FieldDescriptor fieldDescriptor,
    DynamicMessage.Builder builder
) throws DataGeneratorException {
  Descriptors.Descriptor mapEntryDescriptor = fieldDescriptor.getMessageType();
  // MapEntry contains key and value fields
  Map<String, Field> sdcMapField = field.getValueAsMap();
  for (Map.Entry<String, Field> entry : sdcMapField.entrySet()) {
    builder.addRepeatedField(fieldDescriptor, DynamicMessage.newBuilder(mapEntryDescriptor)
        .setField(mapEntryDescriptor.findFieldByName(KEY), entry.getKey())
        .setField(
            mapEntryDescriptor.findFieldByName(VALUE),
            getValue(
                mapEntryDescriptor.findFieldByName(VALUE),
                entry.getValue(),
                record,
                fieldPath + FORWARD_SLASH + entry.getKey(),
                messageTypeToExtensionMap,
                defaultValueMap
            )
        )
        .build()
    );
  }
}
 
Example 4
Source File: ProtoLint.java    From metastore with Apache License 2.0 5 votes vote down vote up
private void lintMessage(Descriptors.Descriptor dp) {
  String name = dp.getName();
  if (!isPascalCase(name)) {
    results.addResult(
        dp,
        RuleInfo.newBuilder()
            .setLintRule(LintRule.LINT_MESSAGE_NAME_SHOULD_BE_PASCAL)
            .setCode(String.format("L%d/00", LintRule.LINT_MESSAGE_NAME_SHOULD_BE_PASCAL_VALUE))
            .build());
  }

  dp.getFields().forEach(fd -> lintField(fd));
}
 
Example 5
Source File: GoogleDataCatalog.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Override
public void updateResourceBinding(String resourceUrn, Descriptors.Descriptor descriptor) {
  try (Scope scope =
      TRACER
          .spanBuilder("GoogleDataCatalog.updateResourceBinding")
          .setRecordEvents(true)
          .startScopedSpan()) {}
}
 
Example 6
Source File: BeanToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
private static void objToMap(Builder b, Descriptors.FieldDescriptor field, Object map0)   {

        if( !(map0 instanceof Map) ) return;

        Descriptors.Descriptor type = field.getMessageType();
        Descriptors.FieldDescriptor keyField = type.findFieldByName("key");
        Descriptors.FieldDescriptor valueField = type.findFieldByName("value");
        if (keyField != null && valueField != null) {

            Map map  = (Map)map0;
            for(Object e: map.entrySet() ) {
                Map.Entry entry = (Map.Entry)e;
                Object key = entry.getKey();
                Object value = entry.getValue();

                com.google.protobuf.Message.Builder entryBuilder = b.newBuilderForField(field);

                Object k = objToMessageObjInner(entryBuilder,key,keyField,false);
                Object v = objToMessageObjInner(entryBuilder,value,valueField,false);

                if(k == null || v == null ) continue;

                entryBuilder.setField(keyField, k);
                entryBuilder.setField(valueField, v);
                b.addRepeatedField(field, entryBuilder.build());
            }

        } else {
            throw new RuntimeException("Invalid map field");
        }
    }
 
Example 7
Source File: ShadowApply.java    From metastore with Apache License 2.0 5 votes vote down vote up
private HashMap<String, Integer> getMessageNameToIndexMap(
    Descriptors.FileDescriptor fileDescriptor) {
  HashMap<String, Integer> indexMap = new HashMap<>();
  for (int i = 0; i < fileDescriptor.getMessageTypes().size(); i++) {
    Descriptors.Descriptor descriptor = fileDescriptor.getMessageTypes().get(i);
    indexMap.put(descriptor.getFullName(), i);
  }
  return indexMap;
}
 
Example 8
Source File: DescriptorCache.java    From beast with Apache License 2.0 5 votes vote down vote up
public Descriptors.Descriptor fetch(Map<String, Descriptors.Descriptor> allDescriptors, Map<String, String> typeNameToPackageNameMap, String protoName) {
    if (allDescriptors.get(protoName) != null) {
        return allDescriptors.get(protoName);
    }
    String packageName = typeNameToPackageNameMap.get(protoName);
    if (packageName == null) {
        return null;
    }
    return allDescriptors.get(packageName);
}
 
Example 9
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static void addMessages(
    Map<String, Descriptors.GenericDescriptor> descriptors,
    Descriptors.FileDescriptor proto,
    String packageName) {
  for (Descriptors.Descriptor message : proto.getMessageTypes()) {
    String fullName = packageName + message.getName();
    descriptors.put(fullName, message);
  }
}
 
Example 10
Source File: GrpcSchemaModule.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
/** Returns a GraphQLOutputType for type T for an input of ListenableFuture<T>. */
private GraphQLOutputType getReturnType(ParameterizedType parameterizedType)
    throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  Class<? extends Message> responseClass =
      (Class<? extends Message>) parameterizedType.getActualTypeArguments()[0];
  Descriptors.Descriptor responseDescriptor =
      (Descriptors.Descriptor) responseClass.getMethod("getDescriptor").invoke(null);
  addExtraType(responseDescriptor);
  return ProtoToGql.getReference(responseDescriptor);
}
 
Example 11
Source File: QueryKeyExpressionWithComparison.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@Override
public void validate(@Nonnull Descriptors.Descriptor descriptor) {
    keyExpression.validate(descriptor);
}
 
Example 12
Source File: MetaDataProtoEditorTest.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@Test
public void fieldIsTypeUnqualified() throws Descriptors.DescriptorValidationException {
    final DescriptorProtos.FileDescriptorProto.Builder fileBuilder = TestRecords1Proto.getDescriptor().toProto().toBuilder();
    final DescriptorProtos.FieldDescriptorProto.Builder fieldBuilder = fileBuilder.getMessageTypeBuilderList().stream()
            .filter(message -> message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME))
            .flatMap(message -> message.getFieldBuilderList().stream())
            .filter(field -> field.getName().equals("_MySimpleRecord"))
            .findAny()
            .get();

    // Unqualify the field in the union descriptor
    fieldBuilder.setTypeName("MySimpleRecord");

    // Ensure that the field still resolves to the same type
    Descriptors.FileDescriptor modifiedFileDescriptor = Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), TestRecords1Proto.getDescriptor().getDependencies().toArray(new Descriptors.FileDescriptor[0]));
    Descriptors.Descriptor simpleRecordDescriptor = modifiedFileDescriptor.findMessageTypeByName("MySimpleRecord");
    assertNotNull(simpleRecordDescriptor);
    assertSame(simpleRecordDescriptor, modifiedFileDescriptor.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MySimpleRecord").getMessageType());

    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test2.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MyOtherRecord"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.MySimpleRecord"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH_AS_NESTED,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.MySimpleRecord.InnerRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord.MyNestedRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord.MyNestedRecord"));

    fieldBuilder.setTypeName("test1.MySimpleRecord");
    modifiedFileDescriptor = Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), TestRecords1Proto.getDescriptor().getDependencies().toArray(new Descriptors.FileDescriptor[0]));
    simpleRecordDescriptor = modifiedFileDescriptor.findMessageTypeByName("MySimpleRecord");
    assertNotNull(simpleRecordDescriptor);
    assertSame(simpleRecordDescriptor, modifiedFileDescriptor.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MySimpleRecord").getMessageType());

    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test2.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MyOtherRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.MySimpleRecord"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH_AS_NESTED,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH_AS_NESTED,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.test1"));
    assertEquals(FieldTypeMatch.MIGHT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.test1.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.RecordTypeUnion.MySimpleRecord.InnerRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord.MyNestedRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(fileBuilder, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord.MyNestedRecord"));
}
 
Example 13
Source File: EventsWithHeader.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static Descriptors.Descriptor getDescriptor() throws Descriptors.DescriptorValidationException {
    return descriptorForTypeWithHeader(FlinkEventProtos.TaskManagerEvent.getDescriptor());
}
 
Example 14
Source File: ThenKeyExpression.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@Override
public List<Descriptors.FieldDescriptor> validate(@Nonnull Descriptors.Descriptor descriptor) {
    return children.stream().flatMap(child -> child.validate(descriptor).stream()).collect(Collectors.toList());
}
 
Example 15
Source File: ProtoDomain.java    From beam with Apache License 2.0 4 votes vote down vote up
public static ProtoDomain buildFrom(Descriptors.Descriptor descriptor) {
  return buildFrom(descriptor.getFile());
}
 
Example 16
Source File: EventsWithHeader.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static Descriptors.Descriptor getDescriptor() throws Descriptors.DescriptorValidationException {
    return descriptorForTypeWithHeader(FlinkEventProtos.KafkaConsumerEvent.getDescriptor());
}
 
Example 17
Source File: DataDstPbHelper.java    From xresloader with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
static public void dumpConstIntoHashMap(String package_name, HashMap<String, Object> parent,
        Descriptors.Descriptor msg_desc) {
    String msg_seg = msg_desc.getName();
    HashMap<String, Object> msg_root = null;
    Object msg_node = parent.getOrDefault(msg_seg, null);
    String msg_full_name = String.format("%s.%s", package_name, msg_seg);
    if (msg_node != null) {
        if (msg_node instanceof HashMap) {
            msg_root = (HashMap<String, Object>) msg_node;
        } else {
            ProgramOptions.getLoger().error("message name %s conflict.", msg_full_name);
            return;
        }
    }

    // enum in message.
    for (Descriptors.EnumDescriptor enum_desc : msg_desc.getEnumTypes()) {
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, enum_desc);
    }

    // if has oneof in message, dump all fields's number.
    for (Descriptors.OneofDescriptor oneof_desc : msg_desc.getOneofs()) {
        if (oneof_desc.getFieldCount() <= 0) {
            continue;
        }
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, oneof_desc);
    }

    // nested message
    for (Descriptors.Descriptor sub_msg_desc : msg_desc.getNestedTypes()) {
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, sub_msg_desc);
    }
}
 
Example 18
Source File: ProtoDomain.java    From beam with Apache License 2.0 4 votes vote down vote up
public Descriptors.Descriptor getDescriptor(String fullName) {
  return descriptorMap.get(fullName);
}
 
Example 19
Source File: MessageBuilderRecordSerializerBase.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@Nonnull
protected abstract M getUnionField(@Nonnull Descriptors.Descriptor unionDescriptor,
                                   @Nonnull U storedRecord);
 
Example 20
Source File: ProtoDomain.java    From metastore with Apache License 2.0 4 votes vote down vote up
public Descriptors.Descriptor getDescriptor(String fullName) {
  return descriptorMap.get(fullName);
}