Revert "Simplify the code when array_each failed"

This reverts commit 271685cceb06f9203f1f7298e5c21ce0c6e4b11f.
This commit is contained in:
Kerollmops 2022-12-13 16:29:49 +01:00
parent 7b2f2a4f9c
commit a08cc82983
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -4,8 +4,10 @@ use std::fs::File;
use std::io::{self, Seek, Write}; use std::io::{self, Seek, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
use either::Either;
use memmap2::MmapOptions; use memmap2::MmapOptions;
use milli::documents::{DocumentsBatchBuilder, Error}; use milli::documents::{DocumentsBatchBuilder, Error};
use milli::Object;
use serde::de::{SeqAccess, Visitor}; use serde::de::{SeqAccess, Visitor};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use serde_json::error::Category; use serde_json::error::Category;
@ -138,19 +140,28 @@ fn read_json_inner(
// The data has been transferred to the writer during the deserialization process. // The data has been transferred to the writer during the deserialization process.
Ok(Ok(_)) => (), Ok(Ok(_)) => (),
Ok(Err(e)) => return Err(DocumentFormatError::Internal(Box::new(e))), Ok(Err(e)) => return Err(DocumentFormatError::Internal(Box::new(e))),
Err(_) => { Err(_e) => {
// If we cannot deserialize the content as an array of object then // If we cannot deserialize the content as an array of object then we try
// we try to deserialize it as a single JSON object. // to deserialize it with the original method to keep correct error messages.
let object = serde_json::from_reader(file) #[derive(Deserialize, Debug)]
#[serde(transparent)]
struct ArrayOrSingleObject {
#[serde(with = "either::serde_untagged")]
inner: Either<Vec<Object>, Object>,
}
let content: ArrayOrSingleObject = serde_json::from_reader(file)
.map_err(Error::Json) .map_err(Error::Json)
.map_err(|e| (payload_type, e))?; .map_err(|e| (payload_type, e))?;
for object in content.inner.map_right(|o| vec![o]).into_inner() {
builder builder
.append_json_object(&object) .append_json_object(&object)
.map_err(Into::into) .map_err(Into::into)
.map_err(DocumentFormatError::Internal)?; .map_err(DocumentFormatError::Internal)?;
} }
} }
}
let count = builder.documents_count(); let count = builder.documents_count();
let _ = builder.into_inner().map_err(Into::into).map_err(DocumentFormatError::Internal)?; let _ = builder.into_inner().map_err(Into::into).map_err(DocumentFormatError::Internal)?;