Java Code Examples for com.google.protobuf.MessageOrBuilder#getField()
The following examples show how to use
com.google.protobuf.MessageOrBuilder#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: MessageValue.java From fdb-record-layer with Apache License 2.0 | 6 votes |
/** * Get the value of the field with the given field name on the given message. * If the field is repeated, the repeated values are combined into a list. If the field has a message type, * the value is returned as a {@link Message} of that type. Otherwise, the field is returned as a primitive. * @param message a message or builder to extract the field from * @param fieldName the field to extract * @return the value of the field as described above */ @Nullable public static Object getFieldOnMessage(@Nonnull MessageOrBuilder message, @Nonnull String fieldName) { final Descriptors.FieldDescriptor field = findFieldDescriptorOnMessage(message, fieldName); if (field.isRepeated()) { int count = message.getRepeatedFieldCount(field); List<Object> list = new ArrayList<>(count); for (int i = 0; i < count; i++) { list.add(message.getRepeatedField(field, i)); } return list; } if (field.hasDefaultValue() || message.hasField(field)) { if (field.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE && TupleFieldsHelper.isTupleField(field.getMessageType())) { return TupleFieldsHelper.fromProto((Message)message.getField(field), field.getMessageType()); } else { return message.getField(field); } } else { return null; } }
Example 2
Source File: BaseRepeatedField.java From fdb-record-layer with Apache License 2.0 | 5 votes |
@Nullable @SuppressWarnings("unchecked") protected List<Object> getValues(@Nonnull MessageOrBuilder message) { final Descriptors.FieldDescriptor field = findFieldDescriptor(message); if (emptyMode == Field.OneOfThemEmptyMode.EMPTY_UNKNOWN && message.getRepeatedFieldCount(field) == 0) { return null; } else { return (List<Object>) message.getField(field); } }
Example 3
Source File: MessageValue.java From fdb-record-layer with Apache License 2.0 | 5 votes |
@Nullable private static Message getFieldMessageOnMessage(@Nonnull MessageOrBuilder message, @Nonnull String fieldName) { final Descriptors.FieldDescriptor field = findFieldDescriptorOnMessage(message, fieldName); if (!field.isRepeated() && (field.hasDefaultValue() || message.hasField(field)) && field.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE) { return (Message)message.getField(field); } return null; }
Example 4
Source File: ProtoHelpers.java From api-compiler with Apache License 2.0 | 5 votes |
@Nullable public static <Type> Type getFieldIfPresent( MessageOrBuilder mob, FieldDescriptor fieldDesc, Class<Type> clazz) { if (mob.hasField(fieldDesc)) { Object fieldValue = null; try { fieldValue = mob.getField(fieldDesc); if (null == fieldValue) { return null; } if (fieldValue instanceof EnumValueDescriptor && clazz.isEnum()) { // Do some sanity checks and convert the EnumValueDescriptor into the (Type) class (which // has to be an enum) EnumValueDescriptor fieldEnumValue = (EnumValueDescriptor) fieldValue; if (clazz.getSimpleName().equals(fieldEnumValue.getType().getName())) { Type[] enumValues = clazz.getEnumConstants(); if (fieldEnumValue.getIndex() >= 0 && fieldEnumValue.getIndex() < enumValues.length) { Type value = enumValues[fieldEnumValue.getIndex()]; return value; } } throw new RuntimeException( String.format("Couldn't convert '%s' to class '%s'", fieldValue, clazz.getName())); } return clazz.cast(fieldValue); } catch (ClassCastException ex) { throw new RuntimeException( String.format( "Expected (%s) type, not (%s), for field '%s' of (%s)%s", clazz, fieldValue.getClass(), fieldDesc.getName(), mob.getClass(), getName(mob)), ex); } } return null; }
Example 5
Source File: ProtoHelpers.java From api-compiler with Apache License 2.0 | 5 votes |
public static boolean getFieldBoolean( MessageOrBuilder mob, String fieldName, boolean defaultValue) { boolean value = defaultValue; FieldDescriptor fieldDesc = mob.getDescriptorForType().findFieldByName(fieldName); if (null != fieldDesc) { if (mob.hasField(fieldDesc)) { Object fieldValue = mob.getField(fieldDesc); if (fieldValue instanceof Boolean) { value = (Boolean) fieldValue; } } } return value; }
Example 6
Source File: TextFormatForTest.java From api-compiler with Apache License 2.0 | 5 votes |
/** * Converts a message into a string. */ public String printToString(MessageOrBuilder message) { StringBuilder result = new StringBuilder(); for (FieldDescriptor field : getFieldsInNumberOrder(message.getDescriptorForType())) { // Skip empty fields. if ((field.isRepeated() && message.getRepeatedFieldCount(field) == 0) || (!field.isRepeated() && !message.hasField(field))) { continue; } // Normalize repeated and singleton fields. Object rawValue = message.getField(field); @SuppressWarnings("unchecked") List<Object> values = field.isMapField() ? sortMapEntries(field, rawValue) : field.isRepeated() ? (List<Object>) rawValue : ImmutableList.of(rawValue); // Print field values. for (Object value : values) { result.append(printFieldToString(field, value)); } } return result.toString(); }
Example 7
Source File: DetailedExitCode.java From bazel with Apache License 2.0 | 5 votes |
/** * Returns the enum value descriptor for the enum field with field number 1 in the {@link * FailureDetail}'s category submessage. */ private static EnumValueDescriptor getSubcategoryDescriptor( FailureDetail failureDetail, MessageOrBuilder categoryMsg) { FieldDescriptor fieldNumberOne = categoryMsg.getDescriptorForType().findFieldByNumber(1); checkNotNull( fieldNumberOne, "FailureDetail category submessage has no field #1: %s", failureDetail); Object fieldNumberOneVal = categoryMsg.getField(fieldNumberOne); checkArgument( fieldNumberOneVal instanceof EnumValueDescriptor, "FailureDetail category submessage has non-enum field #1: %s", failureDetail); return (EnumValueDescriptor) fieldNumberOneVal; }
Example 8
Source File: WrappedPrimitiveSerializer.java From jackson-datatype-protobuf with Apache License 2.0 | 5 votes |
@Override public void serialize( MessageOrBuilder message, JsonGenerator generator, SerializerProvider serializerProvider ) throws IOException { FieldDescriptor field = message.getDescriptorForType().findFieldByName("value"); Object value = message.getField(field); writeValue(field, value, generator, serializerProvider); }
Example 9
Source File: MessageSerializer.java From jackson-datatype-protobuf with Apache License 2.0 | 4 votes |
@Override public void serialize( MessageOrBuilder message, JsonGenerator generator, SerializerProvider serializerProvider ) throws IOException { generator.writeStartObject(); boolean proto3 = message.getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3; Include include = serializerProvider.getConfig().getDefaultPropertyInclusion().getValueInclusion(); boolean writeDefaultValues = proto3 && include != Include.NON_DEFAULT; boolean writeEmptyCollections = include != Include.NON_DEFAULT && include != Include.NON_EMPTY; PropertyNamingStrategyBase namingStrategy = new PropertyNamingStrategyWrapper(serializerProvider.getConfig().getPropertyNamingStrategy()); Descriptor descriptor = message.getDescriptorForType(); List<FieldDescriptor> fields = new ArrayList<>(descriptor.getFields()); if (message instanceof ExtendableMessageOrBuilder<?>) { for (ExtensionInfo extensionInfo : config.extensionRegistry().getExtensionsByDescriptor(descriptor)) { fields.add(extensionInfo.descriptor); } } for (FieldDescriptor field : fields) { if (field.isRepeated()) { List<?> valueList = (List<?>) message.getField(field); if (!valueList.isEmpty() || writeEmptyCollections) { if (field.isMapField()) { generator.writeFieldName(namingStrategy.translate(field.getName())); writeMap(field, valueList, generator, serializerProvider); } else if (valueList.size() == 1 && writeSingleElementArraysUnwrapped(serializerProvider)) { generator.writeFieldName(namingStrategy.translate(field.getName())); writeValue(field, valueList.get(0), generator, serializerProvider); } else { generator.writeArrayFieldStart(namingStrategy.translate(field.getName())); for (Object subValue : valueList) { writeValue(field, subValue, generator, serializerProvider); } generator.writeEndArray(); } } } else if (message.hasField(field) || (writeDefaultValues && !supportsFieldPresence(field) && field.getContainingOneof() == null)) { generator.writeFieldName(namingStrategy.translate(field.getName())); writeValue(field, message.getField(field), generator, serializerProvider); } else if (include == Include.ALWAYS && field.getContainingOneof() == null) { generator.writeFieldName(namingStrategy.translate(field.getName())); generator.writeNull(); } } generator.writeEndObject(); }