mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2025-12-23 02:29:57 +00:00
add AsRef, expose object and array iter on Value (#2207)
add AsRef expose object and array iter add to_json on Document
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
use tantivy::collector::TopDocs;
|
||||
use tantivy::query::QueryParser;
|
||||
use tantivy::schema::{DateOptions, OwnedValue, Schema, INDEXED, STORED, STRING};
|
||||
use tantivy::schema::{DateOptions, Document, OwnedValue, Schema, INDEXED, STORED, STRING};
|
||||
use tantivy::{Index, IndexWriter, TantivyDocument};
|
||||
|
||||
fn main() -> tantivy::Result<()> {
|
||||
|
||||
@@ -494,7 +494,8 @@ mod tests {
|
||||
use crate::query::PhraseQuery;
|
||||
use crate::schema::document::Value;
|
||||
use crate::schema::{
|
||||
IndexRecordOption, Schema, TextFieldIndexing, TextOptions, Type, STORED, STRING, TEXT,
|
||||
Document, IndexRecordOption, Schema, TextFieldIndexing, TextOptions, Type, STORED, STRING,
|
||||
TEXT,
|
||||
};
|
||||
use crate::store::{Compressor, StoreReader, StoreWriter};
|
||||
use crate::time::format_description::well_known::Rfc3339;
|
||||
|
||||
@@ -195,14 +195,6 @@ impl TantivyDocument {
|
||||
Ok(document)
|
||||
}
|
||||
|
||||
/// Encode the schema in JSON.
|
||||
///
|
||||
/// Encoding a document cannot fail.
|
||||
pub fn to_json(&self, schema: &Schema) -> String {
|
||||
serde_json::to_string(&self.to_named_doc(schema))
|
||||
.expect("doc encoding failed. This is a bug")
|
||||
}
|
||||
|
||||
/// Build a document object from a json-object.
|
||||
pub fn parse_json(schema: &Schema, doc_json: &str) -> Result<TantivyDocument, DocParsingError> {
|
||||
let json_obj: Map<String, serde_json::Value> =
|
||||
|
||||
@@ -235,6 +235,14 @@ pub trait Document: DocumentDeserialize + Send + Sync + 'static {
|
||||
}
|
||||
NamedFieldDocument(field_map)
|
||||
}
|
||||
|
||||
/// Encode the doc in JSON.
|
||||
///
|
||||
/// Encoding a document cannot fail.
|
||||
fn to_json(&self, schema: &Schema) -> String {
|
||||
serde_json::to_string(&self.to_named_doc(schema))
|
||||
.expect("doc encoding failed. This is a bug")
|
||||
}
|
||||
}
|
||||
|
||||
/// A single field value.
|
||||
@@ -357,6 +365,26 @@ pub trait Value<'a>: Send + Sync + Debug {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns the iterator over the array if the Value is an array.
|
||||
fn as_array(&self) -> Option<Self::ArrayIter> {
|
||||
if let ReferenceValue::Array(val) = self.as_value() {
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns the iterator over the object if the Value is an object.
|
||||
fn as_object(&self) -> Option<Self::ObjectIter> {
|
||||
if let ReferenceValue::Object(val) = self.as_value() {
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns true if the Value is an array.
|
||||
fn is_array(&self) -> bool {
|
||||
|
||||
@@ -535,7 +535,9 @@ mod tests {
|
||||
|
||||
use super::FieldType;
|
||||
use crate::schema::field_type::ValueParsingError;
|
||||
use crate::schema::{NumericOptions, OwnedValue, Schema, TextOptions, Type, COERCE, INDEXED};
|
||||
use crate::schema::{
|
||||
Document, NumericOptions, OwnedValue, Schema, TextOptions, Type, COERCE, INDEXED,
|
||||
};
|
||||
use crate::time::{Date, Month, PrimitiveDateTime, Time};
|
||||
use crate::tokenizer::{PreTokenizedString, Token};
|
||||
use crate::{DateTime, TantivyDocument};
|
||||
|
||||
@@ -49,6 +49,13 @@ pub enum OwnedValue {
|
||||
IpAddr(Ipv6Addr),
|
||||
}
|
||||
|
||||
impl AsRef<OwnedValue> for OwnedValue {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &OwnedValue {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Value<'a> for &'a OwnedValue {
|
||||
type ChildValue = Self;
|
||||
type ArrayIter = ArrayIter<'a>;
|
||||
@@ -441,11 +448,11 @@ impl<'a> Iterator for ObjectMapIter<'a> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::OwnedValue;
|
||||
use super::*;
|
||||
use crate::schema::{BytesOptions, Schema};
|
||||
use crate::time::format_description::well_known::Rfc3339;
|
||||
use crate::time::OffsetDateTime;
|
||||
use crate::{DateTime, TantivyDocument};
|
||||
use crate::{DateTime, Document, TantivyDocument};
|
||||
|
||||
#[test]
|
||||
fn test_parse_bytes_doc() {
|
||||
|
||||
Reference in New Issue
Block a user