-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
WatchStreamExt::modify()
to modify events
Add `WatchStreamExt::modify()` which returns an `EventModify` stream. This allows for users to directly modify the inner value of an `Event` returned by the watcher stream, thus avoiding the usage of nested maps. Example usage: ```rust let stream = watcher(api, watcher::Config::default()).modify(|pod| { pod.managed_fields_mut().clear(); pod.annotations_mut().clear(); pod.status = None; }); ``` Signed-off-by: Sanskar Jaiswal <[email protected]>
- Loading branch information
Showing
4 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use core::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use std::task::ready; | ||
|
||
use futures::{Stream, TryStream}; | ||
use pin_project::pin_project; | ||
|
||
use crate::watcher::{Error, Event}; | ||
|
||
#[pin_project] | ||
/// Stream returned by the [`modify`](super::WatchStreamExt::modify) method. | ||
/// Modifies the [`Event`] item returned by the inner stream by calling | ||
/// [`modify`](Event::modify()) on it. | ||
pub struct EventModify<St, F> { | ||
#[pin] | ||
stream: St, | ||
f: F, | ||
} | ||
|
||
impl<St, F, K> EventModify<St, F> | ||
where | ||
St: TryStream<Ok = Event<K>>, | ||
F: FnMut(&mut K), | ||
{ | ||
pub(super) fn new(stream: St, f: F) -> EventModify<St, F> { | ||
Self { stream, f } | ||
} | ||
} | ||
|
||
impl<St, F, K> Stream for EventModify<St, F> | ||
where | ||
St: Stream<Item = Result<Event<K>, Error>>, | ||
F: FnMut(&mut K), | ||
{ | ||
type Item = Result<Event<K>, Error>; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let mut me = self.project(); | ||
Poll::Ready(match ready!(me.stream.as_mut().poll_next(cx)) { | ||
Some(Ok(event)) => Some(Ok(event.modify(me.f))), | ||
Some(Err(err)) => Some(Err(err)), | ||
None => None, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) mod test { | ||
use std::task::Poll; | ||
|
||
use super::{Error, Event, EventModify}; | ||
use futures::{pin_mut, poll, stream, StreamExt}; | ||
|
||
#[tokio::test] | ||
async fn eventmodify_modifies_innner_value_of_event() { | ||
let restart_val = vec![10]; | ||
let st = stream::iter([Ok(Event::Applied(0)), | ||
Err(Error::TooManyObjects), | ||
Ok(Event::Restarted(restart_val)), | ||
]); | ||
let ev_modify = EventModify::new(st, |x| { | ||
*x += 1; | ||
}); | ||
pin_mut!(ev_modify); | ||
assert!(matches!( | ||
poll!(ev_modify.next()), | ||
Poll::Ready(Some(Ok(Event::Applied(1)))) | ||
)); | ||
assert!(matches!( | ||
poll!(ev_modify.next()), | ||
Poll::Ready(Some(Err(Error::TooManyObjects))) | ||
)); | ||
assert!(matches!( | ||
poll!(ev_modify.next()), | ||
Poll::Ready(Some(Ok(Event::Restarted(restart_val)))) | ||
)); | ||
assert!(matches!(poll!(ev_modify.next()), Poll::Ready(None))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters