Skip to content

Commit 915578a

Browse files
authored
Merge pull request #56 from fraktalio/feature/depricate_combine
`combine` depricated on saga and view, in faviour of `merge`
2 parents 04efe12 + b97e50f commit 915578a

File tree

7 files changed

+98
-144
lines changed

7 files changed

+98
-144
lines changed

src/saga.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl<'a, AR, A> Saga<'a, AR, A> {
117117

118118
/// Combines two sagas into one.
119119
/// Creates a new instance of a Saga by combining two sagas of type `AR`, `A` and `AR2`, `A2` into a new saga of type `Sum<AR, AR2>`, `Sum<A2, A>`
120+
#[deprecated(
121+
since = "0.8.0",
122+
note = "Use the `merge` function instead. This ensures all your sagas can subscribe to all `Event`/`E` in the system."
123+
)]
120124
pub fn combine<AR2, A2>(self, saga2: Saga<'a, AR2, A2>) -> Saga<'a, Sum<AR, AR2>, Sum<A2, A>> {
121125
let new_react = Box::new(move |ar: &Sum<AR, AR2>| match ar {
122126
Sum::First(ar) => {
@@ -134,6 +138,8 @@ impl<'a, AR, A> Saga<'a, AR, A> {
134138

135139
/// Merges two sagas into one.
136140
/// Creates a new instance of a Saga by merging two sagas of type `AR`, `A` and `AR`, `A2` into a new saga of type `AR`, `Sum<A, A2>`
141+
/// Similar to `combine`, but the event type is the same for both sagas.
142+
/// This ensures all your sagas can subscribe to all `Event`/`E` in the system.
137143
pub fn merge<A2>(self, saga2: Saga<'a, AR, A2>) -> Saga<'a, AR, Sum<A2, A>> {
138144
let new_react = Box::new(move |ar: &AR| {
139145
let a: Vec<Sum<A2, A>> = (self.react)(ar)

src/view.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl<'a, S, E> View<'a, S, E> {
128128
/// Combines two views into one.
129129
/// Creates a new instance of a View by combining two views of type `S`, `E` and `S2`, `E2` into a new view of type `(S, S2)`, `Sum<E, E2>`
130130
/// Combines two views that operate on different event types (`E`` and `E2``) into a new view operating on `Sum<E, E2>`
131+
#[deprecated(
132+
since = "0.8.0",
133+
note = "Use the `merge` function instead. This ensures all your views can subscribe to all `Event`/`E` in the system."
134+
)]
131135
pub fn combine<S2, E2>(self, view2: View<'a, S2, E2>) -> View<'a, (S, S2), Sum<E, E2>>
132136
where
133137
S: Clone,
@@ -161,7 +165,7 @@ impl<'a, S, E> View<'a, S, E> {
161165
/// Merges two views into one.
162166
/// Creates a new instance of a View by merging two views of type `S`, `E` and `S2`, `E` into a new view of type `(S, S2)`, `E`
163167
/// Similar to `combine`, but the event type is the same for both views.
164-
/// Composes two views that operate on the same/shared event type (`E`) into a new view operating on `E`
168+
/// This ensures all your views can subscribe to all `Event`/`E` in the system.
165169
pub fn merge<S2>(self, view2: View<'a, S2, E>) -> View<'a, (S, S2), E>
166170
where
167171
S: Clone,

tests/aggregate_combined_test.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,36 +211,49 @@ fn shipment_decider<'a>() -> Decider<'a, ShipmentCommand, ShipmentState, Shipmen
211211
}
212212
}
213213

214-
fn order_saga<'a>() -> Saga<'a, OrderEvent, ShipmentCommand> {
214+
fn order_saga<'a>() -> Saga<'a, Event, ShipmentCommand> {
215215
Saga {
216216
react: Box::new(|event| match event {
217-
OrderEvent::Created(evt) => {
217+
Event::OrderCreated(evt) => {
218218
vec![ShipmentCommand::Create(CreateShipmentCommand {
219219
shipment_id: evt.order_id,
220220
order_id: evt.order_id,
221221
customer_name: evt.customer_name.to_owned(),
222222
items: evt.items.to_owned(),
223223
})]
224224
}
225-
OrderEvent::Updated(_) => {
225+
Event::OrderUpdated(_) => {
226226
vec![]
227227
}
228-
OrderEvent::Cancelled(_) => {
228+
Event::OrderCancelled(_) => {
229+
vec![]
230+
}
231+
Event::ShipmentCreated(_) => {
229232
vec![]
230233
}
231234
}),
232235
}
233236
}
234237

235-
fn shipment_saga<'a>() -> Saga<'a, ShipmentEvent, OrderCommand> {
238+
fn shipment_saga<'a>() -> Saga<'a, Event, OrderCommand> {
236239
Saga {
237240
react: Box::new(|event| match event {
238-
ShipmentEvent::Created(evt) => {
239-
vec![OrderCommand::Update(api::UpdateOrderCommand {
241+
Event::ShipmentCreated(evt) => {
242+
vec![OrderCommand::Update(UpdateOrderCommand {
240243
order_id: evt.order_id,
241244
new_items: evt.items.to_owned(),
242245
})]
243246
}
247+
248+
Event::OrderCreated(_) => {
249+
vec![]
250+
}
251+
Event::OrderUpdated(_) => {
252+
vec![]
253+
}
254+
Event::OrderCancelled(_) => {
255+
vec![]
256+
}
244257
}),
245258
}
246259
}
@@ -367,9 +380,8 @@ async fn orchestrated_event_sourced_aggregate_test() {
367380
.map_command(&command_from_sum) // Decider<Command, (OrderState, ShipmentState), Sum<OrderEvent, ShipmentEvent>>
368381
.map_event(&event_from_sum, &sum_to_event); // Decider<Command, (OrderState, ShipmentState), Event>
369382
let combined_saga = order_saga()
370-
.combine(shipment_saga())
371-
.map_action(&sum_to_command)
372-
.map_action_result(&event_from_sum);
383+
.merge(shipment_saga())
384+
.map_action(&sum_to_command);
373385
let repository = InMemoryEventRepository::new();
374386
let aggregate = Arc::new(EventSourcedOrchestratingAggregate::new(
375387
repository,
@@ -700,9 +712,8 @@ async fn state_stored_combined_test() {
700712
.map_event(&event_from_sum, &sum_to_event); // Decider<Command, (OrderState, ShipmentState), Event>
701713

702714
let combined_saga = order_saga()
703-
.combine(shipment_saga())
704-
.map_action(&sum_to_command)
705-
.map_action_result(&event_from_sum);
715+
.merge(shipment_saga())
716+
.map_action(&sum_to_command);
706717

707718
let repository = InMemoryStateRepository::new();
708719
let aggregate = Arc::new(StateStoredOrchestratingAggregate::new(

tests/materialized_view_combined_test.rs renamed to tests/materialized_view_merged_test.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ use fmodel_rust::view::View;
77
use fmodel_rust::Identifier;
88

99
use crate::api::{
10-
OrderCancelledEvent, OrderCreatedEvent, OrderEvent, OrderUpdatedEvent, OrderViewState,
11-
ShipmentEvent, ShipmentViewState,
10+
OrderCancelledEvent, OrderCreatedEvent, OrderUpdatedEvent, OrderViewState, ShipmentViewState,
1211
};
13-
use crate::application::{event_from_sum, Event, MaterializedViewError};
12+
use crate::application::{Event, MaterializedViewError};
1413

1514
mod api;
1615
mod application;
1716

18-
fn order_view<'a>() -> View<'a, OrderViewState, OrderEvent> {
17+
fn order_view<'a>() -> View<'a, OrderViewState, Event> {
1918
View {
2019
evolve: Box::new(|state, event| {
2120
let mut new_state = state.clone();
2221
match event {
23-
OrderEvent::Created(evt) => {
22+
Event::OrderCreated(evt) => {
2423
new_state.order_id = evt.order_id;
2524
new_state.customer_name = evt.customer_name.to_owned();
2625
new_state.items = evt.items.to_owned();
2726
}
28-
OrderEvent::Updated(evt) => {
27+
Event::OrderUpdated(evt) => {
2928
new_state.items = evt.updated_items.to_owned();
3029
}
31-
OrderEvent::Cancelled(_) => {
30+
Event::OrderCancelled(_) => {
3231
new_state.is_cancelled = true;
3332
}
33+
Event::ShipmentCreated(_) => {}
3434
}
3535
new_state
3636
}),
@@ -43,17 +43,20 @@ fn order_view<'a>() -> View<'a, OrderViewState, OrderEvent> {
4343
}
4444
}
4545

46-
fn shipment_view<'a>() -> View<'a, ShipmentViewState, ShipmentEvent> {
46+
fn shipment_view<'a>() -> View<'a, ShipmentViewState, Event> {
4747
View {
4848
evolve: Box::new(|state, event| {
4949
let mut new_state = state.clone();
5050
match event {
51-
ShipmentEvent::Created(evt) => {
51+
Event::ShipmentCreated(evt) => {
5252
new_state.shipment_id = evt.shipment_id;
5353
new_state.order_id = evt.order_id;
5454
new_state.customer_name = evt.customer_name.to_owned();
5555
new_state.items = evt.items.to_owned();
5656
}
57+
Event::OrderCreated(_) => {}
58+
Event::OrderUpdated(_) => {}
59+
Event::OrderCancelled(_) => {}
5760
}
5861
new_state
5962
}),
@@ -108,9 +111,7 @@ impl ViewStateRepository<Event, (OrderViewState, ShipmentViewState), Materialize
108111

109112
#[tokio::test]
110113
async fn test() {
111-
let combined_view = order_view()
112-
.combine(shipment_view())
113-
.map_event(&event_from_sum);
114+
let combined_view = order_view().merge(shipment_view());
114115
let repository = InMemoryViewStateRepository::new();
115116
let materialized_view = Arc::new(MaterializedView::new(repository, combined_view));
116117
let materialized_view1 = Arc::clone(&materialized_view);

tests/saga_manager_combined_test.rs renamed to tests/saga_manager_merged_test.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,59 @@ use fmodel_rust::saga::Saga;
22
use fmodel_rust::saga_manager::{ActionPublisher, SagaManager};
33

44
use crate::api::{
5-
CreateShipmentCommand, OrderCommand, OrderCreatedEvent, OrderEvent, ShipmentCommand,
6-
ShipmentEvent, UpdateOrderCommand,
5+
CreateShipmentCommand, OrderCommand, OrderCreatedEvent, ShipmentCommand, UpdateOrderCommand,
76
};
8-
use crate::application::{event_from_sum2, sum_to_command2, Command, Event, SagaManagerError};
7+
use crate::application::{sum_to_command2, Command, Event, SagaManagerError};
98

109
mod api;
1110
mod application;
1211

13-
fn order_saga<'a>() -> Saga<'a, OrderEvent, ShipmentCommand> {
12+
fn order_saga<'a>() -> Saga<'a, Event, ShipmentCommand> {
1413
Saga {
1514
react: Box::new(|event| match event {
16-
OrderEvent::Created(evt) => {
15+
Event::OrderCreated(evt) => {
1716
vec![ShipmentCommand::Create(CreateShipmentCommand {
1817
shipment_id: evt.order_id,
1918
order_id: evt.order_id,
2019
customer_name: evt.customer_name.to_owned(),
2120
items: evt.items.to_owned(),
2221
})]
2322
}
24-
OrderEvent::Updated(_) => {
23+
Event::OrderUpdated(_) => {
2524
vec![]
2625
}
27-
OrderEvent::Cancelled(_) => {
26+
Event::OrderCancelled(_) => {
27+
vec![]
28+
}
29+
Event::ShipmentCreated(_) => {
2830
vec![]
2931
}
3032
}),
3133
}
3234
}
3335

34-
fn shipment_saga<'a>() -> Saga<'a, ShipmentEvent, OrderCommand> {
36+
fn shipment_saga<'a>() -> Saga<'a, Event, OrderCommand> {
3537
Saga {
3638
react: Box::new(|event| match event {
37-
ShipmentEvent::Created(evt) => {
39+
Event::ShipmentCreated(evt) => {
3840
vec![OrderCommand::Update(UpdateOrderCommand {
3941
order_id: evt.order_id,
4042
new_items: evt.items.to_owned(),
4143
})]
4244
}
45+
46+
Event::OrderCreated(_) => {
47+
vec![]
48+
}
49+
Event::OrderUpdated(_) => {
50+
vec![]
51+
}
52+
Event::OrderCancelled(_) => {
53+
vec![]
54+
}
4355
}),
4456
}
4557
}
46-
4758
/// Simple action publisher that just returns the action/command.
4859
/// It is used for testing. In real life, it would publish the action/command to some external system. or to an aggregate that is able to handel the action/command.
4960
struct SimpleActionPublisher;
@@ -71,9 +82,8 @@ async fn test() {
7182
let saga_manager = SagaManager::new(
7283
SimpleActionPublisher::new(),
7384
shipment_saga()
74-
.combine(order_saga())
75-
.map_action(&sum_to_command2)
76-
.map_action_result(&event_from_sum2),
85+
.merge(order_saga())
86+
.map_action(&sum_to_command2),
7787
);
7888
let result = saga_manager.handle(&order_created_event).await;
7989
assert!(result.is_ok());

tests/saga_test.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use fmodel_rust::saga::{ActionComputation, Saga};
22

33
use crate::api::{
44
CreateShipmentCommand, OrderCommand, OrderCreatedEvent, OrderEvent, ShipmentCommand,
5-
ShipmentEvent, UpdateOrderCommand,
5+
UpdateOrderCommand,
66
};
7-
use crate::application::{event_from_sum, sum_to_command, Command, Event};
7+
use crate::application::{sum_to_command, Command, Event};
88

99
mod api;
1010
mod application;
@@ -54,19 +54,6 @@ fn order_saga_2<'a>() -> Saga<'a, Event, ShipmentCommand> {
5454
}
5555
}
5656

57-
fn shipment_saga<'a>() -> Saga<'a, ShipmentEvent, OrderCommand> {
58-
Saga {
59-
react: Box::new(|event| match event {
60-
ShipmentEvent::Created(evt) => {
61-
vec![OrderCommand::Update(UpdateOrderCommand {
62-
order_id: evt.order_id,
63-
new_items: evt.items.to_owned(),
64-
})]
65-
}
66-
}),
67-
}
68-
}
69-
7057
fn shipment_saga_2<'a>() -> Saga<'a, Event, OrderCommand> {
7158
Saga {
7259
react: Box::new(|event| match event {
@@ -93,14 +80,8 @@ fn shipment_saga_2<'a>() -> Saga<'a, Event, OrderCommand> {
9380
#[test]
9481
fn test() {
9582
let order_saga: Saga<OrderEvent, ShipmentCommand> = order_saga();
96-
let order_saga2: Saga<OrderEvent, ShipmentCommand> = crate::order_saga();
9783
let order_saga_2: Saga<Event, ShipmentCommand> = crate::order_saga_2();
98-
let shipment_saga: Saga<ShipmentEvent, OrderCommand> = shipment_saga();
9984
let shipment_saga_2: Saga<Event, OrderCommand> = crate::shipment_saga_2();
100-
let combined_saga = order_saga2
101-
.combine(shipment_saga)
102-
.map_action(&sum_to_command)
103-
.map_action_result(&event_from_sum);
10485
let merged_saga = order_saga_2
10586
.merge(shipment_saga_2)
10687
.map_action(&sum_to_command);
@@ -126,17 +107,6 @@ fn test() {
126107
items: vec!["Item 1".to_string(), "Item 2".to_string()],
127108
});
128109

129-
let combined_commands = combined_saga.compute_new_actions(&order_created_event2);
130-
assert_eq!(
131-
combined_commands,
132-
[Command::ShipmentCreate(CreateShipmentCommand {
133-
shipment_id: 1,
134-
order_id: 1,
135-
customer_name: "John Doe".to_string(),
136-
items: vec!["Item 1".to_string(), "Item 2".to_string()],
137-
})]
138-
);
139-
140110
let merged_commands = merged_saga.compute_new_actions(&order_created_event2);
141111
assert_eq!(
142112
merged_commands,

0 commit comments

Comments
 (0)