qdrant_client/grpc_conversions/extensions.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use crate::client::Payload;
#[allow(deprecated)]
use crate::error::NotA;
use crate::prelude::{PointStruct, Value};
use crate::qdrant::value::Kind;
use crate::qdrant::{ListValue, PointId, RetrievedPoint, ScoredPoint, Struct, Vectors};
/// Null value
static NULL_VALUE: Value = Value {
kind: Some(Kind::NullValue(0)),
};
impl PointStruct {
pub fn new(
id: impl Into<PointId>,
vectors: impl Into<Vectors>,
payload: impl Into<Payload>,
) -> Self {
Self {
id: Some(id.into()),
payload: payload.into().into(),
vectors: Some(vectors.into()),
}
}
}
impl RetrievedPoint {
/// Get a payload value for the specified key. If the key is not present,
/// this will return a null value.
///
/// # Examples:
///
/// ```
/// use qdrant_client::qdrant::RetrievedPoint;
/// let point = RetrievedPoint::default();
/// assert!(point.get("not_present").is_null());
/// ````
pub fn get(&self, key: &str) -> &Value {
self.try_get(key).unwrap_or(&NULL_VALUE)
}
/// Try to get a payload value for the specified key. If the key is not present,
/// this will return `None`.
///
/// # Examples:
///
/// ```
/// use qdrant_client::qdrant::RetrievedPoint;
/// let point = RetrievedPoint::default();
/// assert_eq!(point.try_get("not_present"), None);
/// ````
pub fn try_get(&self, key: &str) -> Option<&Value> {
self.payload.get(key)
}
}
impl ScoredPoint {
/// Get a payload value for the specified key. If the key is not present,
/// this will return a null value.
///
/// # Examples:
///
/// ```
/// use qdrant_client::qdrant::ScoredPoint;
/// let point = ScoredPoint::default();
/// assert!(point.get("not_present").is_null());
/// ````
pub fn get(&self, key: &str) -> &Value {
self.try_get(key).unwrap_or(&NULL_VALUE)
}
/// Get a payload value for the specified key. If the key is not present,
/// this will return `None`.
///
/// # Examples:
///
/// ```
/// use qdrant_client::qdrant::ScoredPoint;
/// let point = ScoredPoint::default();
/// assert_eq!(point.try_get("not_present"), None);
/// ````
pub fn try_get(&self, key: &str) -> Option<&Value> {
self.payload.get(key)
}
}
macro_rules! extract {
($kind:ident, $check:ident) => {
/// Check if this value is a
#[doc = stringify!([$kind])]
pub fn $check(&self) -> bool {
matches!(self.kind, Some($kind(_)))
}
};
($kind:ident, $check:ident, $extract:ident, $ty:ty) => {
extract!($kind, $check);
/// Get this value as
#[doc = stringify!([$ty])]
///
/// Returns `None` if this value is not a
#[doc = stringify!([$kind].)]
pub fn $extract(&self) -> Option<$ty> {
if let Some($kind(v)) = self.kind {
Some(v)
} else {
None
}
}
};
($kind:ident, $check:ident, $extract:ident, ref $ty:ty) => {
extract!($kind, $check);
/// Get this value as
#[doc = stringify!([$ty])]
///
/// Returns `None` if this value is not a
#[doc = stringify!([$kind].)]
pub fn $extract(&self) -> Option<&$ty> {
if let Some($kind(v)) = &self.kind {
Some(v)
} else {
None
}
}
};
}
// Separate module to not import all enum kinds of `Kind` directly as this conflicts with other types.
// The macro extract!() however is built to take enum kinds directly and passing Kind::<kind> is not possible.
mod value_extract_impl {
use crate::qdrant::value::Kind::*;
use crate::qdrant::{Struct, Value};
impl Value {
extract!(NullValue, is_null);
extract!(BoolValue, is_bool, as_bool, bool);
extract!(IntegerValue, is_integer, as_integer, i64);
extract!(DoubleValue, is_double, as_double, f64);
extract!(StringValue, is_str, as_str, ref String);
extract!(ListValue, is_list, as_list, ref [Value]);
extract!(StructValue, is_struct, as_struct, ref Struct);
}
}
impl Value {
#[cfg(feature = "serde")]
/// Convert this into a [`serde_json::Value`]
///
/// # Examples:
///
/// ```
/// use serde_json::json;
/// use qdrant_client::prelude::*;
/// use qdrant_client::qdrant::{value::Kind::*, Struct};
/// let value = Value { kind: Some(StructValue(Struct {
/// fields: [
/// ("text".into(), Value { kind: Some(StringValue("Hi Qdrant!".into())) }),
/// ("int".into(), Value { kind: Some(IntegerValue(42))}),
/// ].into()
/// }))};
/// assert_eq!(value.into_json(), json!({
/// "text": "Hi Qdrant!",
/// "int": 42
/// }));
/// ```
pub fn into_json(self) -> serde_json::Value {
use serde_json::Value as JsonValue;
match self.kind {
Some(Kind::BoolValue(b)) => JsonValue::Bool(b),
Some(Kind::IntegerValue(i)) => JsonValue::from(i),
Some(Kind::DoubleValue(d)) => JsonValue::from(d),
Some(Kind::StringValue(s)) => JsonValue::String(s),
Some(Kind::ListValue(vs)) => vs.into_iter().map(Value::into_json).collect(),
Some(Kind::StructValue(s)) => s
.fields
.into_iter()
.map(|(k, v)| (k, v.into_json()))
.collect(),
Some(Kind::NullValue(_)) | None => JsonValue::Null,
}
}
}
#[cfg(feature = "serde")]
impl From<Value> for serde_json::Value {
fn from(value: Value) -> Self {
value.into_json()
}
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.kind {
Some(Kind::BoolValue(b)) => write!(f, "{}", b),
Some(Kind::IntegerValue(i)) => write!(f, "{}", i),
Some(Kind::DoubleValue(v)) => write!(f, "{}", v),
Some(Kind::StringValue(s)) => write!(f, "{:?}", s),
Some(Kind::ListValue(vs)) => {
let mut i = vs.values.iter();
write!(f, "[")?;
if let Some(first) = i.next() {
write!(f, "{}", first)?;
for v in i {
write!(f, ",{}", v)?;
}
}
write!(f, "]")
}
Some(Kind::StructValue(s)) => {
let mut i = s.fields.iter();
write!(f, "{{")?;
if let Some((key, value)) = i.next() {
write!(f, "{:?}:{}", key, value)?;
for (key, value) in i {
write!(f, ",{:?}:{}", key, value)?;
}
}
write!(f, "}}")
}
_ => write!(f, "null"),
}
}
}
impl Value {
/// Try to get an iterator over the items of the contained list value
///
/// Returns `None` if this is not a list.
pub fn try_list_iter(&self) -> Option<impl Iterator<Item = &Value>> {
if let Some(Kind::ListValue(values)) = &self.kind {
Some(values.iter())
} else {
None
}
}
/// Try to get an iterator over the items of the contained list value, if any
#[deprecated(since = "1.10.0", note = "use `try_list_iter` instead")]
#[allow(deprecated)]
pub fn iter_list(&self) -> Result<impl Iterator<Item = &Value>, NotA<ListValue>> {
if let Some(Kind::ListValue(values)) = &self.kind {
Ok(values.iter())
} else {
Err(NotA::default())
}
}
/// Get a value from a struct field
///
/// Returns `None` if this is not a struct type or if the field is not present.
pub fn get_value(&self, key: &str) -> Option<&Value> {
if let Some(Kind::StructValue(Struct { fields })) = &self.kind {
Some(fields.get(key)?)
} else {
None
}
}
/// Try to get a field from the struct if this value contains one
#[deprecated(since = "1.10.0", note = "use `get_value` instead")]
#[allow(deprecated)]
pub fn get_struct(&self, key: &str) -> Result<&Value, NotA<Struct>> {
if let Some(Kind::StructValue(Struct { fields })) = &self.kind {
Ok(fields.get(key).unwrap_or(&NULL_VALUE))
} else {
Err(NotA::default())
}
}
}
impl std::ops::Deref for ListValue {
type Target = [Value];
fn deref(&self) -> &[Value] {
&self.values
}
}
impl IntoIterator for ListValue {
type Item = Value;
type IntoIter = std::vec::IntoIter<Value>;
fn into_iter(self) -> Self::IntoIter {
self.values.into_iter()
}
}
impl ListValue {
pub fn iter(&self) -> std::slice::Iter<'_, Value> {
self.values.iter()
}
}
impl Hash for PointId {
fn hash<H: Hasher>(&self, state: &mut H) {
use crate::qdrant::point_id::PointIdOptions::{Num, Uuid};
match &self.point_id_options {
Some(Num(u)) => state.write_u64(*u),
Some(Uuid(s)) => s.hash(state),
None => {}
}
}
}
impl Hash for ScoredPoint {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}
impl Hash for RetrievedPoint {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}