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 Project: metastore   File: ShadowTest.java    License: 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 Project: garmadon   File: ProtoConcatenatorTest.java    License: 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 Project: rejoiner   File: GrpcSchemaModule.java    License: 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 4
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 5
Source Project: datacollector   File: ProtobufTypeUtil.java    License: 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 6
Source Project: beast   File: DescriptorCache.java    License: 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 7
Source Project: metastore   File: ShadowApply.java    License: 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 Project: krpc   File: BeanToMessage.java    License: 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 9
Source Project: metastore   File: GoogleDataCatalog.java    License: 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 10
Source Project: metastore   File: ProtoLint.java    License: 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 11
@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 12
Source Project: metastore   File: ProtoDomain.java    License: Apache License 2.0 4 votes vote down vote up
public Descriptors.Descriptor getDescriptor(String fullName) {
  return descriptorMap.get(fullName);
}
 
Example 13
@Nonnull
protected abstract M getUnionField(@Nonnull Descriptors.Descriptor unionDescriptor,
                                   @Nonnull U storedRecord);
 
Example 14
Source Project: beam   File: ProtoDomain.java    License: Apache License 2.0 4 votes vote down vote up
public Descriptors.Descriptor getDescriptor(String fullName) {
  return descriptorMap.get(fullName);
}
 
Example 15
Source Project: garmadon   File: EventsWithHeader.java    License: Apache License 2.0 4 votes vote down vote up
public static Descriptors.Descriptor getDescriptor() throws Descriptors.DescriptorValidationException {
    return descriptorForTypeWithHeader(FlinkEventProtos.KafkaConsumerEvent.getDescriptor());
}
 
Example 16
@Override
public void validate(@Nonnull Descriptors.Descriptor descriptor) {
    keyExpression.validate(descriptor);
}
 
Example 17
Source Project: garmadon   File: EventsWithHeader.java    License: Apache License 2.0 4 votes vote down vote up
public static Descriptors.Descriptor getDescriptor() throws Descriptors.DescriptorValidationException {
    return descriptorForTypeWithHeader(FlinkEventProtos.TaskManagerEvent.getDescriptor());
}
 
Example 18
@Override
public List<Descriptors.FieldDescriptor> validate(@Nonnull Descriptors.Descriptor descriptor) {
    return children.stream().flatMap(child -> child.validate(descriptor).stream()).collect(Collectors.toList());
}
 
Example 19
Source Project: beam   File: ProtoDomain.java    License: Apache License 2.0 4 votes vote down vote up
public static ProtoDomain buildFrom(Descriptors.Descriptor descriptor) {
  return buildFrom(descriptor.getFile());
}
 
Example 20
Source Project: xresloader   File: DataDstPbHelper.java    License: 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);
    }
}