Skip to content

Commit

Permalink
Return Upcaster to original location
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnabell committed Mar 28, 2024
1 parent 15efc76 commit 0b165f8
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/common/a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ pub struct EventA {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for EventA {}
impl esrs::event::Upcaster for EventA {}
2 changes: 1 addition & 1 deletion examples/common/b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ pub struct EventB {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for EventB {}
impl esrs::event::Upcaster for EventB {}
2 changes: 1 addition & 1 deletion examples/common/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct BasicEvent {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for BasicEvent {}
impl esrs::event::Upcaster for BasicEvent {}

#[allow(dead_code)]
#[derive(Debug, thiserror::Error)]
Expand Down
2 changes: 1 addition & 1 deletion examples/readme/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub enum BookEvent {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for BookEvent {}
impl esrs::event::Upcaster for BookEvent {}

#[derive(Debug, Error)]
pub enum BookError {
Expand Down
2 changes: 1 addition & 1 deletion examples/saga/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ pub enum SagaEvent {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for SagaEvent {}
impl esrs::event::Upcaster for SagaEvent {}
4 changes: 2 additions & 2 deletions examples/schema/adding_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod before_schema {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Event {}
impl esrs::event::Upcaster for Event {}
}

mod after_schema {
Expand Down Expand Up @@ -129,7 +129,7 @@ mod after_schema {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Schema {}
impl esrs::event::Upcaster for Schema {}
}

pub(crate) async fn example(pool: PgPool) {
Expand Down
4 changes: 2 additions & 2 deletions examples/schema/deprecating_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod before_deprecation {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Schema {}
impl esrs::event::Upcaster for Schema {}

impl esrs::store::postgres::Schema<Event> for Schema {
fn from_event(value: Event) -> Self {
Expand Down Expand Up @@ -135,7 +135,7 @@ mod after_deprecation {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Schema {}
impl esrs::event::Upcaster for Schema {}

impl esrs::store::postgres::Schema<Event> for Schema {
fn from_event(value: Event) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions examples/schema/upcasting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod before_upcasting {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Schema {}
impl esrs::event::Upcaster for Schema {}

impl esrs::store::postgres::Schema<Event> for Schema {
fn from_event(value: Event) -> Self {
Expand Down Expand Up @@ -132,7 +132,7 @@ mod after_upcasting {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for Schema {}
impl esrs::event::Upcaster for Schema {}

impl esrs::store::postgres::Schema<Event> for Schema {
fn from_event(value: Event) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/upcasting/a.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

use esrs::sql::event::Upcaster;
use esrs::event::Upcaster;
use esrs::Aggregate;

use crate::{Command, Error};
Expand Down
2 changes: 1 addition & 1 deletion examples/upcasting/b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use esrs::sql::event::Upcaster;
use esrs::event::Upcaster;
use esrs::Aggregate;

use crate::{Command, Error};
Expand Down
17 changes: 17 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use serde::de::DeserializeOwned;

pub trait Upcaster
where
Self: Sized,
{
fn upcast(value: serde_json::Value, _version: Option<i32>) -> Result<Self, serde_json::Error>
where
Self: DeserializeOwned,
{
serde_json::from_value(value)
}

fn current_version() -> Option<i32> {
None
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod aggregate;
mod state;

pub mod bus;
#[cfg(feature = "upcasting")]
pub mod event;
pub mod handler;
pub mod manager;
pub mod store;
Expand Down
21 changes: 2 additions & 19 deletions src/sql/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,17 @@ use crate::types::SequenceNumber;
use serde::de::DeserializeOwned;
use serde::Serialize;

#[cfg(feature = "upcasting")]
pub trait Upcaster
where
Self: Sized,
{
fn upcast(value: serde_json::Value, _version: Option<i32>) -> Result<Self, serde_json::Error>
where
Self: DeserializeOwned,
{
serde_json::from_value(value)
}

fn current_version() -> Option<i32> {
None
}
}

#[cfg(not(feature = "upcasting"))]
pub trait Persistable: Serialize + DeserializeOwned {}

#[cfg(not(feature = "upcasting"))]
impl<T> Persistable for T where T: Serialize + DeserializeOwned {}

#[cfg(feature = "upcasting")]
pub trait Persistable: Serialize + DeserializeOwned + Upcaster {}
pub trait Persistable: Serialize + DeserializeOwned + crate::event::Upcaster {}

#[cfg(feature = "upcasting")]
impl<T> Persistable for T where T: Serialize + DeserializeOwned + Upcaster {}
impl<T> Persistable for T where T: Serialize + DeserializeOwned + crate::event::Upcaster {}

/// Event representation on the event store
#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/sql/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {
pub struct TestEvent;

#[cfg(feature = "upcasting")]
impl crate::sql::event::Upcaster for TestEvent {}
impl crate::event::Upcaster for TestEvent {}

impl Aggregate for TestAggregate {
const NAME: &'static str = "test";
Expand Down
4 changes: 2 additions & 2 deletions src/store/postgres/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::sql::event::Persistable;
/// # }
/// #
/// # #[cfg(feature = "upcasting")]
/// # impl esrs::sql::event::Upcaster for Schema {}
/// # impl esrs::event::Upcaster for Schema {}
/// #
/// # impl SchemaTrait<Event> for Schema {
/// # fn from_event(Event { a }: Event) -> Self {
Expand Down Expand Up @@ -65,7 +65,7 @@ pub trait Schema<E>: Persistable {
/// # }
/// #
/// # #[cfg(feature = "upcasting")]
/// # impl esrs::sql::event::Upcaster for Schema {}
/// # impl esrs::event::Upcaster for Schema {}
/// #
/// # impl SchemaTrait<Event> for Schema {
/// # fn from_event(Event { a }: Event) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion tests/aggregate/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct TestEvent {
}

#[cfg(feature = "upcasting")]
impl esrs::sql::event::Upcaster for TestEvent {}
impl esrs::event::Upcaster for TestEvent {}

#[derive(Debug, thiserror::Error)]
pub enum TestError {}

0 comments on commit 0b165f8

Please sign in to comment.