Java Code Examples for com.google.protobuf.Message#hasField()

The following examples show how to use com.google.protobuf.Message#hasField() . 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: FunctionKeyIndexTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ProtobufUtils.java    From envelope with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: JsonEncoder.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
/** Encode the given message as a JSON array. */
public JsonArray encode(Message message) {
  JsonArray array = new JsonArray();
  for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
    if (field.isRepeated() || message.hasField(field)) {
      JsonElement element = encodeField(field, message.getField(field));
      if (!element.isJsonNull()) {
        while (array.size() < field.getNumber()) {
          array.add(JsonNull.INSTANCE);
        }
        array.set(field.getNumber() - 1, element);
      }
    }
  }
  return array;
}
 
Example 4
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
Object getFromProtoMessage(Message message) {
  if (message.hasField(getFieldDescriptor(message))) {
    Message wrapper = (Message) message.getField(getFieldDescriptor(message));
    return valueConvert.getFromProtoMessage(wrapper);
  }
  return null;
}
 
Example 5
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
Object getFromProtoMessage(Message message) {
  FieldDescriptor fieldDescriptor = getFieldDescriptor(message);
  if (message.hasField(fieldDescriptor)) {
    return convertFromProtoValue(message.getField(fieldDescriptor));
  }
  return null;
}
 
Example 8
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
Object getFromProtoMessage(Message message) {
  if (message.hasField(getFieldDescriptor(message))) {
    return fieldOverlay.getFromProtoMessage(message);
  }
  return null;
}
 
Example 9
Source File: ChangeSetHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void requiredFields(Message msg, int... fields) {
    for(int fieldNumber : fields) {
        FieldDescriptor field = msg.getDescriptorForType().findFieldByNumber(fieldNumber);
        if(!msg.hasField(field)) {
            throw new IllegalArgumentException("Missing field: " + field.getName());
        }
    }
}
 
Example 10
Source File: SimpleRpcDispatcher.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: FieldKeyExpression.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@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();
    }
}
 
Example 12
Source File: FieldMasks.java    From google-ads-java with Apache License 2.0 4 votes vote down vote up
private static void compare(
    FieldMask.Builder mask, String currentField, Message original, Message modified) {
  Descriptor descriptor = original.getDescriptorForType();
  for (FieldDescriptor field : descriptor.getFields()) {
    String fieldName = getFieldName(currentField, field);
    Object originalValue = original.getField(field);
    Object modifiedValue = modified.getField(field);
    if (field.isRepeated()) {
      if (!Objects.equals(originalValue, modifiedValue)) {
        mask.addPaths(fieldName);
      }
    } else {
      switch (field.getJavaType()) {
        case MESSAGE:
          // Because getField never returns null, we use hasField to distinguish null
          // from empty message when getType() == MESSAGE
          if (original.hasField(field) != modified.hasField(field)
              || !Objects.equals(originalValue, modifiedValue)) {
            if (isWrapperType(field.getMessageType())) {
              // For wrapper types, just emit the field name.
              mask.addPaths(fieldName);
            } else if (!modified.hasField(field)) {
              // Just emit the deleted field name
              mask.addPaths(fieldName);
            } else {
              // Recursively compare to find different values
              compare(mask, fieldName, (Message) originalValue, (Message) modifiedValue);
            }
          }
          break;
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
        case BOOLEAN:
        case STRING:
        case BYTE_STRING:
        case ENUM:
          // Handle all java types except MESSAGE
          if (!Objects.equals(originalValue, modifiedValue)) {
            mask.addPaths(fieldName);
          }
          break;
        default:
          throw new IllegalArgumentException(
              "Unexpected java type "
                  + field.getJavaType()
                  + " encountered for field "
                  + fieldName);
      }
    }
  }
}