Skip to content

Commit

Permalink
Remove type parameter from source()
Browse files Browse the repository at this point in the history
This will match upcoming changes for source filtering to the bulk API.
  • Loading branch information
mwilliammyers committed Oct 11, 2019
1 parent 06a3e97 commit 54b7f9b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/elastic/examples/update_with_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ fn run() -> Result<(), Box<dyn Error>> {
.update("1")
.script("ctx._source.likes++")
// Request that the updated document be returned with the response
.source::<UpdatedNewsArticle>()
.source()
.send()?;

assert!(update.updated());

// Deserialize the updated document to the type we requested earlier;
// this will return `None` if `source()` was not called on the request
let updated_doc = update.into_document().unwrap();
// Deserialize the updated document,
// will return `None` if `source()` was not called on the request
let updated_doc = update.into_document::<UpdatedNewsArticle>().unwrap();

assert!(updated_doc.likes > 0);

Expand Down
55 changes: 22 additions & 33 deletions src/elastic/src/client/requests/document_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub use crate::client::requests::common::{
ScriptBuilder,
};

pub(crate) type DefaultUpdatedSource = serde_json::Value;

/**
An [update document request][docs-update] builder that can be configured before sending.
Expand All @@ -62,17 +60,15 @@ The `send` method will either send the request [synchronously][send-sync] or [as
[send-async]: #send-asynchronously
[Client.document.update]: ../../struct.DocumentClient.html#update-document-request
*/
pub type UpdateRequestBuilder<TSender, TBody, TSource = DefaultUpdatedSource> =
RequestBuilder<TSender, UpdateRequestInner<TBody, TSource>>;
pub type UpdateRequestBuilder<TSender, TBody> = RequestBuilder<TSender, UpdateRequestInner<TBody>>;

#[doc(hidden)]
pub struct UpdateRequestInner<TBody, TSource> {
pub struct UpdateRequestInner<TBody> {
index: Index<'static>,
ty: Type<'static>,
id: Id<'static>,
body: TBody,
_body_marker: PhantomData<TBody>,
_update_source_marker: PhantomData<TSource>,
_marker: PhantomData<TBody>,
}

/**
Expand Down Expand Up @@ -221,8 +217,7 @@ where
ty: ty,
id: id.into(),
body: Doc::empty(),
_body_marker: PhantomData,
_update_source_marker: PhantomData,
_marker: PhantomData,
},
)
}
Expand Down Expand Up @@ -277,14 +272,13 @@ where
ty: DEFAULT_DOC_TYPE.into(),
id: id.into(),
body: Doc::empty(),
_body_marker: PhantomData,
_update_source_marker: PhantomData,
_marker: PhantomData,
},
)
}
}

impl<TBody, TSource> UpdateRequestInner<TBody, TSource>
impl<TBody> UpdateRequestInner<TBody>
where
TBody: Serialize,
{
Expand Down Expand Up @@ -388,8 +382,7 @@ where
index: self.inner.index,
ty: self.inner.ty,
id: self.inner.id,
_body_marker: PhantomData,
_update_source_marker: PhantomData,
_marker: PhantomData,
},
)
}
Expand Down Expand Up @@ -468,8 +461,7 @@ where
index: self.inner.index,
ty: self.inner.ty,
id: self.inner.id,
_body_marker: PhantomData,
_update_source_marker: PhantomData,
_marker: PhantomData,
},
)
}
Expand Down Expand Up @@ -560,7 +552,7 @@ where
}
}

impl<TSender, TBody, TSource> UpdateRequestBuilder<TSender, TBody, TSource>
impl<TSender, TBody> UpdateRequestBuilder<TSender, TBody>
where
TSender: Sender,
{
Expand All @@ -583,17 +575,17 @@ where
# fn main() { run().unwrap() }
# fn run() -> Result<(), Box<dyn ::std::error::Error>> {
# #[derive(Serialize, Deserialize, ElasticType)]
# struct NewsArticle { likes: i64 }
# struct NewsArticle { id: i64, likes: i64 }
# #[derive(Serialize, Deserialize, ElasticType)]
# struct UpdatedNewsArticle { likes: i64 }
# struct UpdatedNewsArticle { id: i64, likes: i64 }
# let client = SyncClientBuilder::new().build()?;
let response = client.document::<NewsArticle>()
.update(1)
.script("ctx._source.likes++")
.source::<UpdatedNewsArticle>()
.source()
.send()?;
assert!(response.into_document().unwrap().likes >= 1);
assert!(response.into_document::<UpdatedNewsArticle>().unwrap().likes >= 1);
# Ok(())
# }
```
Expand All @@ -602,12 +594,11 @@ where
[`UpdateResponse`]: ../../responses/struct.UpdateResponse.html
[`into_document`]: ../../responses/struct.UpdateResponse.html#method.into_document
*/
pub fn source<T>(self) -> UpdateRequestBuilder<TSender, TBody, T>
where
T: DeserializeOwned,
{
pub fn source(self) -> UpdateRequestBuilder<TSender, TBody> {
RequestBuilder::new(
self.client,
// TODO: allow passing in `source` parameter add `_source` to body
// instead because it supports more options
self.params_builder
.fluent(|params| params.url_param("_source", true))
.shared(),
Expand All @@ -616,8 +607,7 @@ where
index: self.inner.index,
ty: self.inner.ty,
id: self.inner.id,
_body_marker: PhantomData,
_update_source_marker: PhantomData,
_marker: PhantomData,
},
)
}
Expand All @@ -626,7 +616,7 @@ where
/**
# Send synchronously
*/
impl<TBody, TSource> UpdateRequestBuilder<SyncSender, TBody, TSource>
impl<TBody> UpdateRequestBuilder<SyncSender, TBody>
where
TBody: Serialize,
{
Expand Down Expand Up @@ -666,7 +656,7 @@ where
[SyncClient]: ../../type.SyncClient.html
[documents-mod]: ../../types/document/index.html
*/
pub fn send(self) -> Result<UpdateResponse<TSource>, Error> {
pub fn send(self) -> Result<UpdateResponse, Error> {
let req = self.inner.into_request()?;

RequestBuilder::new(self.client, self.params_builder, RawRequestInner::new(req))
Expand All @@ -678,10 +668,9 @@ where
/**
# Send asynchronously
*/
impl<TBody, TSource> UpdateRequestBuilder<AsyncSender, TBody, TSource>
impl<TBody> UpdateRequestBuilder<AsyncSender, TBody>
where
TBody: Serialize + Send + 'static,
TSource: Send + 'static,
{
/**
Send an `UpdateRequestBuilder` asynchronously using an [`AsyncClient`][AsyncClient].
Expand Down Expand Up @@ -726,7 +715,7 @@ where
[AsyncClient]: ../../type.AsyncClient.html
[documents-mod]: ../../types/document/index.html
*/
pub fn send(self) -> Pending<TSource> {
pub fn send(self) -> Pending {
let (client, params_builder, inner) = (self.client, self.params_builder, self.inner);

let req_future = client.sender.maybe_async(move || inner.into_request());
Expand Down Expand Up @@ -758,7 +747,7 @@ mod tests {

#[test]
fn is_send() {
assert_send::<super::Pending<serde_json::Value>>();
assert_send::<super::Pending>();
}

#[derive(Serialize, ElasticType)]
Expand Down
29 changes: 12 additions & 17 deletions src/elastic/src/client/responses/document_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
Response types for a [update document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html).
*/

use std::marker::PhantomData;

use serde::de::DeserializeOwned;
use serde_json::Value;

use super::common::DocumentResult;

use crate::{
client::requests::document_update::DefaultUpdatedSource,
http::receiver::IsOkOnSuccess,
types::document::{
Id,
Expand All @@ -21,7 +18,7 @@ use crate::{

/** Response for an [update document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html). */
#[derive(Deserialize, Debug)]
pub struct UpdateResponse<TSource = DefaultUpdatedSource> {
pub struct UpdateResponse {
#[serde(rename = "_index")]
index: String,
#[serde(rename = "_type")]
Expand All @@ -38,14 +35,9 @@ pub struct UpdateResponse<TSource = DefaultUpdatedSource> {
routing: Option<String>,
result: DocumentResult,
get: Option<Value>,
#[serde(skip)]
_marker: PhantomData<TSource>,
}

impl<TSource> UpdateResponse<TSource>
where
TSource: DeserializeOwned,
{
impl UpdateResponse {
/**
Convert the source in the response into the updated document.
Expand All @@ -61,31 +53,34 @@ where
# fn main() { run().unwrap() }
# fn run() -> Result<(), Box<dyn ::std::error::Error>> {
# #[derive(Serialize, Deserialize, ElasticType)]
# struct NewsArticle { likes: i64 }
# struct NewsArticle { id: i64, likes: i64 }
# #[derive(Serialize, Deserialize, ElasticType)]
# struct UpdatedNewsArticle { likes: i64 }
# struct UpdatedNewsArticle { id: i64, likes: i64 }
# let client = SyncClientBuilder::new().build()?;
let response = client.document::<NewsArticle>()
.update(1)
.script("ctx._source.likes++")
.source::<UpdatedNewsArticle>()
.source()
.send()?;
assert!(response.into_document().unwrap().likes >= 1);
assert!(response.into_document::<UpdatedNewsArticle>().unwrap().likes >= 1);
# Ok(())
# }
```
[`source`]: ../requests/document_update/type.UpdateRequestBuilder.html#method.source
[`UpdateRequestBuilder`]: ../requests/document_update/type.UpdateRequestBuilder.html
*/
pub fn into_document(&self) -> Option<TSource> {
pub fn into_document<T>(&self) -> Option<T>
where
T: DeserializeOwned,
{
self.get.as_ref().map_or_else(
|| None,
|obj| {
obj.get("_source")
.cloned()
.map_or_else(|| None, |doc| serde_json::from_value::<TSource>(doc).ok())
.map_or_else(|| None, |doc| serde_json::from_value::<T>(doc).ok())
},
)
}
Expand Down Expand Up @@ -137,4 +132,4 @@ where
}
}

impl<TSource> IsOkOnSuccess for UpdateResponse<TSource> {}
impl IsOkOnSuccess for UpdateResponse {}
6 changes: 3 additions & 3 deletions tests/integration/src/tests/document/update_with_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn doc() -> Doc {
test! {
const description: &'static str = "update and return source";

type Response = UpdateResponse<UpdatedDoc>;
type Response = UpdateResponse;

// Ensure the index doesn't exist
fn prepare(&self, client: AsyncClient) -> Box<dyn Future<Item = (), Error = Error>> {
Expand Down Expand Up @@ -64,7 +64,7 @@ test! {
.doc(json!({
"title": EXPECTED_TITLE.to_owned(),
}))
.source::<UpdatedDoc>()
.source()
.send();

Box::new(
Expand All @@ -78,7 +78,7 @@ test! {
fn assert_ok(&self, res: &Self::Response) -> bool {
let updated = res.updated();
let correct_version = res.version() == Some(2);
let correct_title = res.into_document().unwrap().title == EXPECTED_TITLE;
let correct_title = res.into_document::<UpdatedDoc>().unwrap().title == EXPECTED_TITLE;

updated && correct_version && correct_title
}
Expand Down

0 comments on commit 54b7f9b

Please sign in to comment.