@Override protected Object getField(Object record, String name, int pos, Object state) { Message m = (Message)record; FieldDescriptor f = ((FieldDescriptor[])state)[pos]; switch (f.getType()) { case MESSAGE: if (!f.isRepeated() && !m.hasField(f)) return null; default: return m.getField(f); } }
private static void writeMessage(Message message, JsonWriter writer) { MessageType type = MessageType.of(message); for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) { if (fieldDescriptor.isRepeated()) { writer.name(fieldDescriptor.getName()); if (fieldDescriptor.isMapField()) { writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer); } else { writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor)); } } else if (message.hasField(fieldDescriptor)) { writer.name(fieldDescriptor.getName()); Object fieldValue = message.getField(fieldDescriptor); writeFieldValue(fieldDescriptor, fieldValue, writer); } } }
/** * Find out which field in the incoming message contains the payload that is. * delivered to the service method. */ protected FieldDescriptor resolvePayloadField(Message message) { for (FieldDescriptor field : message.getDescriptorForType().getFields()) { if (message.hasField(field)) { return field; } } throw new RuntimeException("No payload found in message " + message); }
@Override boolean hasField(Message proto) { // TODO(lukes): Currently we assume that a field is present if it is repeated, has an // explicit default value or is set. However, the type of fields is not generally nullable, // so we can return null for a non-nullable field type. Given the current ToFu implementation // this is not a problem, but modifying the field types to be nullable for non-repeated fields // with non explicit defaults should probably happen. return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor()); } }
@Override boolean hasField(Message proto) { // TODO(lukes): Currently we assume that a field is present if it is repeated, has an // explicit default value or is set. However, the type of fields is not generally nullable, // so we can return null for a non-nullable field type. Given the current ToFu implementation // this is not a problem, but modifying the field types to be nullable for non-repeated fields // with non explicit defaults should probably happen. return !shouldCheckFieldPresenceToEmulateJspbNullability() || proto.hasField(getDescriptor()); } }
public static boolean isFieldSetByName(Message message, String name) { return message.hasField(message.getDescriptorForType().findFieldByName(name)); }
@Override public SoyValue getProtoField(String name) { FieldWithInterpreter field = clazz().fields.get(name); if (field == null) { throw new IllegalArgumentException( "Proto " + proto.getClass().getName() + " does not have a field of name " + name); } if (field.shouldCheckFieldPresenceToEmulateJspbNullability() && !proto.hasField(field.getDescriptor())) { return NullData.INSTANCE; } return field.interpretField(proto).resolve(); }
@Override public boolean isFieldSet(T msg, String fieldName) { Descriptors.FieldDescriptor fd = checkFieldDescriptor(fieldName); if (fd.isRepeated()) { return !((Collection<?>) msg.getField(fd)).isEmpty(); } return msg.hasField(fd); }
/** * Gets a value for the field for the underlying proto object. Not intended for general use. * * @param name The proto field name. * @return The value of the given field for the underlying proto object, or NullData if either the * field does not exist or the value is not set in the underlying proto (according to the jspb * semantics) */ public SoyValue getProtoField(String name) { FieldWithInterpreter field = clazz().fields.get(name); if (field == null) { throw new IllegalArgumentException( "Proto " + proto.getClass().getName() + " does not have a field of name " + name); } if (field.shouldCheckFieldPresenceToEmulateJspbNullability() && !proto.hasField(field.getDescriptor())) { return NullData.INSTANCE; } return field.interpretField(proto); }
@Override protected Object getField(Object record, String name, int pos, Object state) { Message m = (Message)record; FieldDescriptor f = ((FieldDescriptor[])state)[pos]; switch (f.getType()) { case MESSAGE: if (!f.isRepeated() && !m.hasField(f)) return null; default: return m.getField(f); } }
public static void writeJson(@Nonnull Message message, @Nonnull JsonGenerator jg) throws IOException { final Descriptors.Descriptor descriptor = message.getDescriptorForType(); jg.writeStartObject(); for (final Descriptors.FieldDescriptor fieldDescriptor : descriptor.getFields()) { if (!(fieldDescriptor.isRepeated() || fieldDescriptor.isRequired()) && !message.hasField(fieldDescriptor)) { continue; } jg.writeFieldName(fieldDescriptor.getName()); final Object value = message.getField(fieldDescriptor); if (!writeValue(value, jg)) { throw new IOException("Unable to serialize field '" + fieldDescriptor.getName() + "' in " + message + ": unhandled field value"); } } jg.writeEndObject(); }
@Nonnull @Override protected Message getUnionField(@Nonnull Descriptors.Descriptor unionDescriptor, @Nonnull Message storedRecord) { final List<Descriptors.OneofDescriptor> oneofs = unionDescriptor.getOneofs(); if (!oneofs.isEmpty()) { Descriptors.FieldDescriptor unionField = storedRecord.getOneofFieldDescriptor(oneofs.get(0)); if (unionField != null) { return (Message)storedRecord.getField(unionField); } } else { for (Descriptors.FieldDescriptor unionField : unionDescriptor.getFields()) { if (storedRecord.hasField(unionField)) { return (Message)storedRecord.getField(unionField); } } } throw new RecordSerializationException("Union record does not have any fields") .addLogInfo("unionDescriptorFullName", unionDescriptor.getFullName()) .addLogInfo("recordType", storedRecord.getDescriptorForType().getName()); }
if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) { configBuilder.clearField(fieldInLocationsToMerge);
if (!proto3 && !messageToMergeForm.hasField(fieldInLocationsToMerge)) { configBuilder.clearField(fieldInLocationsToMerge);
/** * Retrieves and converts Protobuf fields from a Message. * <p> * If the field in the {@link com.google.protobuf.Descriptors.Descriptor} exists in the {@link Message}, the value is * retrieved and converted using {@link #getFieldValue(Descriptors.FieldDescriptor, Object, DataType)}. * Otherwise, the field value is {@code null}. * The extraction honors the order of the {@code Descriptor}. * * @param dsc the Protobuf Descriptor with all fields * @param msg the Message with the current field values * @param schema the Dataset schema derived from the Descriptor * @return a list of converted values */ public static List<Object> buildRowValues(Descriptors.Descriptor dsc, Message msg, StructType schema) { List<Object> values = new ArrayList<>(); Object val; for (Descriptors.FieldDescriptor fd : dsc.getFields()) { if ( (!fd.isRepeated() && msg.hasField(fd)) || (fd.isRepeated() && msg.getRepeatedFieldCount(fd) > 0) ) { val = getFieldValue(fd, msg.getField(fd), schema.apply(fd.getName()).dataType()); } else { LOG.trace("FieldDescriptor[{}] => not found", fd.getFullName()); val = null; } values.add(val); } return values; }
@Nonnull @Override public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record, @Nullable Message message, @Nonnull Key.Evaluated arguments) { if (message == null) { return Collections.emptyList(); } List<Key.Evaluated> keys = new ArrayList<>(); Descriptors.Descriptor descriptor = message.getDescriptorForType(); Descriptors.FieldDescriptor strField = descriptor.findFieldByNumber(TypesRecord.STR_VALUE_FIELD_NUMBER); Descriptors.FieldDescriptor strListField = descriptor.findFieldByNumber(TypesRecord.STR_LIST_VALUE_FIELD_NUMBER); if (message.hasField(strField)) { keys.add(toKey((String) message.getField(strField))); } final int len = message.getRepeatedFieldCount(strListField); for (int i = 0; i < len; i++) { keys.add(toKey((String) message.getRepeatedField(strListField, i))); } return keys; }
continue; if (!source.hasField(field) && !destination.hasField(field)) { if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (options.replaceMessageFields()) { if (!source.hasField(field)) { destination.clearField(field); } else { if (source.hasField(field)) { destination.getFieldBuilder(field).mergeFrom((Message) source.getField(field)); if (source.hasField(field) || !options.replacePrimitiveFields()) { destination.setField(field, source.getField(field)); } else {
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 &&
private static void writeMessage(Message message, JsonWriter writer) { MessageType type = MessageType.of(message); for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) { if (fieldDescriptor.isRepeated()) { writer.name(fieldDescriptor.getName()); if (fieldDescriptor.isMapField()) { writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer); } else { writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor)); } } else if (message.hasField(fieldDescriptor)) { writer.name(fieldDescriptor.getName()); Object fieldValue = message.getField(fieldDescriptor); writeFieldValue(fieldDescriptor, fieldValue, writer); } } }