Java Code Examples for com.google.protobuf.Message#getField()
The following examples show how to use
com.google.protobuf.Message#getField() .
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: ProtobufCopyTest.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Test public void testNestedCollectionFieldSelection() throws Exception { Message filtered = ProtobufExt.copy(OUTER_VALUE, asSet("objectArrayField.stringField1", "primitiveField")); FieldDescriptor objectField = OUTER_VALUE.getDescriptorForType().findFieldByName("objectField"); FieldDescriptor objectArrayField = OUTER_VALUE.getDescriptorForType().findFieldByName("objectArrayField"); FieldDescriptor stringField1 = ProtoMessageBuilder.getAndAssertField(objectField.getMessageType(), "stringField1"); FieldDescriptor stringField2 = ProtoMessageBuilder.getAndAssertField(objectField.getMessageType(), "stringField2"); assertFieldHasNoValue(filtered, objectField); assertFieldHasValue(filtered, objectArrayField); Collection<Message> collection = (Collection<Message>) filtered.getField(objectArrayField); for (Message inner : collection) { assertFieldHasValue(inner, stringField1); assertFieldHasNoValue(inner, stringField2); } }
Example 2
Source File: ProtobufCopyTest.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Test public void testCollectionFieldSelection() throws Exception { Message filtered = ProtobufExt.copy(OUTER_VALUE, asSet("objectArrayField", "primitiveField")); FieldDescriptor objectField = OUTER_VALUE.getDescriptorForType().findFieldByName("objectField"); FieldDescriptor primitiveField = OUTER_VALUE.getDescriptorForType().findFieldByName("primitiveField"); FieldDescriptor objectArrayField = OUTER_VALUE.getDescriptorForType().findFieldByName("objectArrayField"); FieldDescriptor stringField1 = ProtoMessageBuilder.getAndAssertField(objectField.getMessageType(), "stringField1"); FieldDescriptor stringField2 = ProtoMessageBuilder.getAndAssertField(objectField.getMessageType(), "stringField2"); assertFieldHasNoValue(filtered, objectField); assertFieldHasValue(filtered, primitiveField); assertFieldHasValue(filtered, objectArrayField); Collection<Message> collection = (Collection<Message>) filtered.getField(objectArrayField); assertThat(collection).hasSize(2); for (Message inner : collection) { assertFieldHasValue(inner, stringField1); assertFieldHasValue(inner, stringField2); } }
Example 3
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 6 votes |
@Override Map getFromProtoMessage(Message message) { List<Message> list = (List<Message>) message.getField(getFieldDescriptor(message)); Map<Object, Object> rowMap = new HashMap<>(); if (list.size() == 0) { return rowMap; } list.forEach( entryMessage -> { Descriptors.Descriptor entryDescriptor = entryMessage.getDescriptorForType(); FieldDescriptor keyFieldDescriptor = entryDescriptor.findFieldByName("key"); FieldDescriptor valueFieldDescriptor = entryDescriptor.findFieldByName("value"); rowMap.put( key.convertFromProtoValue(entryMessage.getField(keyFieldDescriptor)), this.value.convertFromProtoValue(entryMessage.getField(valueFieldDescriptor))); }); return rowMap; }
Example 4
Source File: ProtobufSerializer.java From jackson-datatype-protobuf with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") protected void writeMap( FieldDescriptor field, Object entries, JsonGenerator generator, SerializerProvider serializerProvider ) throws IOException { Descriptor entryDescriptor = field.getMessageType(); FieldDescriptor keyDescriptor = entryDescriptor.findFieldByName("key"); FieldDescriptor valueDescriptor = entryDescriptor.findFieldByName("value"); generator.writeStartObject(); for (Message entry : (List<? extends Message>) entries) { // map keys can only be integers or strings so this should be fine generator.writeFieldName(entry.getField(keyDescriptor).toString()); Object value = entry.getField(valueDescriptor); // map values can't be maps or repeated so this should be fine writeValue(valueDescriptor, value, generator, serializerProvider); } generator.writeEndObject(); }
Example 5
Source File: FallbackMessageDataProvider.java From krpc with Apache License 2.0 | 6 votes |
public String get(Message m, String[] keys, int idx) { String name = keys[idx]; FieldDescriptor field = m.getDescriptorForType().findFieldByName(name); if (field == null) return null; Object value = m.getField(field); if (value == null) return ""; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { Message sub = (Message) value; if (idx >= keys.length - 1) { return ""; } else { return get(sub, keys, idx + 1); } } return value.toString(); }
Example 6
Source File: MessageToBean.java From krpc with Apache License 2.0 | 5 votes |
private static void parseMapFieldValue(Descriptors.FieldDescriptor field, Object value, Object bean) { Descriptors.Descriptor type = field.getMessageType(); Descriptors.FieldDescriptor keyField = type.findFieldByName("key"); Descriptors.FieldDescriptor valueField = type.findFieldByName("value"); if (keyField != null && valueField != null) { Iterator listItem = ((List)value).iterator(); Map map = new LinkedHashMap(); while(listItem.hasNext()) { Object element = listItem.next(); Message entry = (Message)element; Object entryKey = entry.getField(keyField); Object entryValue = entry.getField(valueField); if( !isSimpleType(keyField) ) { throw new RuntimeException("not supported map field type, keyField="+keyField.getName()); } if( !isSimpleType(valueField) ) { if( entryValue instanceof Message ) { Class cls = getMapValueCls(bean,field.getName()); if( cls == null ) continue; entryValue = MessageToBean.toBean((Message)entryValue,cls); } else { continue; // throw new RuntimeException("not supported map field type, valueField=" + valueField.getName()); } } map.put(entryKey,entryValue); } addToResults(field.getName(), map, bean, false); } else { throw new RuntimeException("Invalid map field"); } }
Example 7
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 5 votes |
@Override Object getFromProtoMessage(Message message) { if (message.hasField(getFieldDescriptor(message))) { Message wrapper = (Message) message.getField(getFieldDescriptor(message)); return valueConvert.getFromProtoMessage(wrapper); } return null; }
Example 8
Source File: ConfigSource.java From api-compiler with Apache License 2.0 | 5 votes |
/** * Helper method to get an {@link Map} from message builder for the map field. The map returned is * an immutable copy, to add entries to a map field use {@link * ConfigSource#putMapEntry(com.google.protobuf.Message.Builder, FieldDescriptor, Object, Object)} */ @SuppressWarnings("unchecked") private static ImmutableMap<Object, Object> getMapFromProtoMapBuilder( Message.Builder builder, FieldDescriptor field) { List<Message> entries = (List<Message>) builder.getField(field); ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder(); for (Message entry : entries) { Object key = entry.getField(entry.getDescriptorForType().findFieldByName("key")); Object value = entry.getField(entry.getDescriptorForType().findFieldByName("value")); mapBuilder.put(key, value); } return mapBuilder.build(); }
Example 9
Source File: ConfigSource.java From api-compiler with Apache License 2.0 | 5 votes |
private static List<Message> removeEntryWithKeyIfPresent(List<Message> messages, Object key) { List<Message> messagesWithoutKey = Lists.newArrayList(); // This should only ever match at most one element, since the underlying proto adheres // to Map<> semantics. for (Message message : messages) { FieldDescriptor keyField = message.getDescriptorForType().findFieldByName("key"); Object messageKey = message.getField(keyField); if (!messageKey.equals(key)) { messagesWithoutKey.add(message); } } return messagesWithoutKey; }
Example 10
Source File: ProtobufCopyTest.java From titus-control-plane with Apache License 2.0 | 5 votes |
private void assertFieldHasNoValue(Message entity, FieldDescriptor field) { Object value = entity.getField(field); if (value != null) { if (value instanceof DynamicMessage) { assertThat(((DynamicMessage) value).getAllFields()).isEmpty(); } else if (value instanceof String) { assertThat(value).isEqualTo(""); } else if (value instanceof Collection) { assertThat((Collection) value).isEmpty(); } else { fail("Expected null value for field " + field); } } }
Example 11
Source File: ProtobufCopyTest.java From titus-control-plane with Apache License 2.0 | 5 votes |
private void assertFieldHasValue(Message entity, FieldDescriptor field) { Object value = entity.getField(field); assertThat(value).isNotNull(); if (value instanceof DynamicMessage) { assertThat(((DynamicMessage) value).getAllFields()).isNotEmpty(); } else if (value instanceof Collection) { assertThat((Collection) value).isNotEmpty(); } else { assertThat(value).isNotNull(); } }
Example 12
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 5 votes |
@Override Object convertFromProtoValue(Object object) { Message timestamp = (Message) object; Descriptors.Descriptor timestampDescriptor = timestamp.getDescriptorForType(); FieldDescriptor secondField = timestampDescriptor.findFieldByNumber(1); FieldDescriptor nanoField = timestampDescriptor.findFieldByNumber(2); long second = (long) timestamp.getField(secondField); int nano = (int) timestamp.getField(nanoField); return Instant.ofEpochSecond(second, nano); }
Example 13
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 5 votes |
@Override Object getFromProtoMessage(Message message) { FieldDescriptor fieldDescriptor = getFieldDescriptor(message); if (message.hasField(fieldDescriptor)) { Message wrapper = (Message) message.getField(fieldDescriptor); return convertFromProtoValue(wrapper); } return null; }
Example 14
Source File: MessageDifferencer.java From startup-os with Apache License 2.0 | 5 votes |
boolean compareFieldValueUsingParentFields( Message message1, Message message2, FieldDescriptor field, int index1, int index2, @Nullable Reporter reporter, List<SpecificField> stack) { FieldComparator.ComparisonResult result = fieldComparator.compare( message1, message2, field, index1, index2, ImmutableList.copyOf(stack)); if (result == FieldComparator.ComparisonResult.RECURSE) { Preconditions.checkArgument( field.getJavaType() == JavaType.MESSAGE, "FieldComparator should not return RECURSE for fields not being submessages!"); // Get the nested messages and compare them using one of the // methods. Message nextMessage1 = field.isRepeated() ? (Message) message1.getRepeatedField(field, index1) : (Message) message1.getField(field); Message nextMessage2 = field.isRepeated() ? (Message) message2.getRepeatedField(field, index2) : (Message) message2.getField(field); stack.add( field.isRepeated() ? SpecificField.forRepeatedField(field, index1, index2) : SpecificField.forField(field)); boolean isSame = compare(nextMessage1, nextMessage2, reporter, stack); pop(stack); return isSame; } return result == FieldComparator.ComparisonResult.SAME; }
Example 15
Source File: GeoPointWithinDistanceComponent.java From fdb-record-layer with Apache License 2.0 | 5 votes |
@Nullable private Double getCoordinateField(@Nullable Message message, @Nonnull String fieldName) { if (message == null) { return null; } final Descriptors.FieldDescriptor field = message.getDescriptorForType().findFieldByName(fieldName); if (field == null) { throw new Query.InvalidExpressionException("Missing field " + fieldName); } return (Double)message.getField(field); }
Example 16
Source File: FDBCrossRecordQueryTest.java From fdb-record-layer with Apache License 2.0 | 5 votes |
/** * Verify that a query against some record types can use an index on more record types, even when comparing against * a nested field. */ @Test public void testNestedPartialCrossRecordTypeQuery() throws Exception { try (FDBRecordContext context = openContext()) { openUnionRecordStore(context); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord.newBuilder().setRecNo(80).setStrValueIndexed("box").setEtag(1).setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(1)).build()); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord2.newBuilder().setStrValueIndexed("of").setEtag(1).setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(2)).build()); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord3.newBuilder().setStrValueIndexed("fox").setEtag(2).setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(3)).build()); commit(context); } RecordQuery query = RecordQuery.newBuilder() .setRecordTypes(Arrays.asList("MySimpleRecord2", "MySimpleRecord3")) .setFilter(Query.and( Query.field("etag").equalsValue(1), Query.field("nested").matches(Query.field("etag").equalsValue(2)))) .build(); RecordQueryPlan plan = planner.plan(query); MatcherAssert.assertThat(plan, typeFilter(containsInAnyOrder("MySimpleRecord2", "MySimpleRecord3"), indexScan(allOf(indexName("partial_nested_versions"), bounds(hasTupleString("[[2, 1],[2, 1]]")))))); assertEquals(-1448785488, plan.planHash()); try (FDBRecordContext context = openContext()) { openUnionRecordStore(context); try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) { while (cursor.hasNext()) { final Message record = cursor.next().getRecord(); final Message nested = ((Message)record.getField(record.getDescriptorForType().findFieldByName("nested"))); assertEquals(2, nested.getField(nested.getDescriptorForType().findFieldByName("etag"))); } } assertDiscardedNone(context); } }
Example 17
Source File: FDBCrossRecordQueryTest.java From fdb-record-layer with Apache License 2.0 | 5 votes |
/** * Verify that querying on a nested field can scan an appropriate universal index. */ @DualPlannerTest public void testNestedCrossRecordTypeQueryFilteredAndSorted() throws Exception { try (FDBRecordContext context = openContext()) { openUnionRecordStore(context); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord.newBuilder().setRecNo(80).setStrValueIndexed("box").setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(1)).build()); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord2.newBuilder().setStrValueIndexed("of").setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(2)).build()); recordStore.saveRecord(TestRecordsWithUnionProto.MySimpleRecord3.newBuilder().setStrValueIndexed("fox").setNested(TestRecordsWithUnionProto.Nested.newBuilder().setEtag(3)).build()); commit(context); } RecordQuery query = RecordQuery.newBuilder() .setFilter(Query.field("nested").matches(Query.field("etag").greaterThan(1))) .setSort(field("nested").nest("etag")) .build(); RecordQueryPlan plan = planner.plan(query); MatcherAssert.assertThat(plan, indexScan(allOf(indexName("cross_versions"), bounds(hasTupleString("([1],>"))))); assertEquals(552822345, plan.planHash()); List<String> names = new ArrayList<>(); List<Integer> etags = new ArrayList<>(); try (FDBRecordContext context = openContext()) { openUnionRecordStore(context); try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) { while (cursor.hasNext()) { final Message record = cursor.next().getRecord(); names.add((String)record.getField(record.getDescriptorForType().findFieldByName("str_value_indexed"))); final Message nested = ((Message)record.getField(record.getDescriptorForType().findFieldByName("nested"))); etags.add((int)nested.getField(nested.getDescriptorForType().findFieldByName("etag"))); } } assertDiscardedNone(context); } assertEquals(Arrays.asList("of", "fox"), names); assertEquals(Arrays.asList(2, 3), etags); }
Example 18
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 5 votes |
@Override Duration convertFromProtoValue(Object object) { Message timestamp = (Message) object; Descriptors.Descriptor timestampDescriptor = timestamp.getDescriptorForType(); FieldDescriptor secondField = timestampDescriptor.findFieldByNumber(1); FieldDescriptor nanoField = timestampDescriptor.findFieldByNumber(2); long second = (long) timestamp.getField(secondField); int nano = (int) timestamp.getField(nanoField); return Duration.ofSeconds(second, nano); }
Example 19
Source File: ProtoDynamicMessageSchema.java From beam with Apache License 2.0 | 5 votes |
@Override Object getFromProtoMessage(Message message) { FieldDescriptor fieldDescriptor = getFieldDescriptor(message); if (message.hasField(fieldDescriptor)) { Message wrapper = (Message) message.getField(fieldDescriptor); return convertFromProtoValue(wrapper); } return null; }
Example 20
Source File: FieldKeyExpression.java From fdb-record-layer with Apache License 2.0 | 4 votes |
@Nonnull @Override @SuppressWarnings("unchecked") public <M extends Message> List<Key.Evaluated> evaluateMessage(@Nullable FDBRecord<M> record, @Nullable Message message) { if (message == null) { return getNullResult(); } Descriptors.Descriptor recordDescriptor = message.getDescriptorForType(); Descriptors.FieldDescriptor fieldDescriptor = recordDescriptor.findFieldByName(fieldName); // TODO: Part of this is working around a deficiency in DynamicMessage.getField() prior // to 2.5, where a repeated message field returns an empty message instead of an // empty collection. if (fieldDescriptor != null && fieldDescriptor.isRepeated()) { List<Object> values; if (message.getRepeatedFieldCount(fieldDescriptor) > 0) { values = (List<Object>)message.getField(fieldDescriptor); } else { values = Collections.emptyList(); } switch (fanType) { case FanOut: return Key.Evaluated.fan(values); case Concatenate: return Collections.singletonList(Key.Evaluated.scalar(values)); case None: throw new RecordCoreException("FanType.None with repeated field"); default: throw new RecordCoreException(String.format("unknown fan type: %s", fanType)); } } else if (fieldDescriptor != null && (nullStandin == Key.Evaluated.NullStandin.NOT_NULL || message.hasField(fieldDescriptor))) { Object value = message.getField(fieldDescriptor); if (fieldDescriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE && TupleFieldsHelper.isTupleField(fieldDescriptor.getMessageType())) { value = TupleFieldsHelper.fromProto((Message)value, fieldDescriptor.getMessageType()); } // ignore FanType return Collections.singletonList(Key.Evaluated.scalar(value)); } else { return getNullResult(); } }