From cca246746538a5e689168741b2e9e2542613cf8a Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 22 Dec 2022 14:49:43 -0500 Subject: Fix an issue with json serialization of journal entries --- server/src/play.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'server/src') diff --git a/server/src/play.rs b/server/src/play.rs index 19fa0a0..62b6219 100644 --- a/server/src/play.rs +++ b/server/src/play.rs @@ -9,9 +9,10 @@ use protocol::{ simple_bots::AlwaysPassBiddingBot, }; use rand::random; -use serde::{Deserialize, Serialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::json; -use sqlx::{query, PgPool}; +use sqlx::{query, query_as, PgPool}; +use tracing::info; use uuid::Uuid; use crate::error::BridgeError; @@ -45,7 +46,10 @@ impl DbJournal { } #[async_trait] -impl Journal for DbJournal { +impl Journal for DbJournal +where + Item: Clone + Serialize + DeserializeOwned + Send + Sync + Unpin + 'static, +{ async fn append( &mut self, seq: i64, @@ -58,7 +62,7 @@ impl Journal for DbJo "#, self.id, seq, - json!(payload), + sqlx::types::Json(payload) as _, ) .execute(&self.db) .await; @@ -75,9 +79,10 @@ impl Journal for DbJo } async fn replay(&mut self, seq: i64) -> Result, BridgeError> { - let rows = query!( + let rows = query_as!( + ReplayRow::, r#" - select seq, payload as "payload!: String" from object_journal + select seq, payload as "payload: _" from object_journal where id = $1 and seq >= $2 order by seq "#, @@ -86,9 +91,9 @@ impl Journal for DbJo ) .fetch_all(&self.db) .await?; - let mut payloads = vec![]; + let mut payloads: Vec = vec![]; for v in rows { - payloads.push(serde_json::from_str(&v.payload)?); + payloads.push(v.payload.0); self.seq = v.seq; } Ok(payloads) @@ -185,7 +190,7 @@ impl> Table { Ok(()) } - pub async fn play(&mut self, card: Card) -> Result<(), BridgeError> { + pub async fn play(&mut self, _card: Card) -> Result<(), BridgeError> { Ok(()) } @@ -198,6 +203,7 @@ impl> Table { } let mut state = TableState::Unknown; for update in log { + info!("Replaying update {update:?}"); match update { TableUpdate::NewDeal(_, _) => todo!(), TableUpdate::ChangeSettings(_) => todo!(), @@ -225,6 +231,12 @@ impl> Table { } } +#[derive(Debug)] +struct ReplayRow { + seq: i64, + payload: sqlx::types::Json, +} + pub async fn advance_play>( table: &mut Table, ) -> Result<(), BridgeError> { @@ -320,8 +332,7 @@ mod test { #[tokio::test] async fn test_advance_play() { - let mut t1 = - Table::new(TestJournal::new()).await.unwrap(); + let mut t1 = Table::new(TestJournal::new()).await.unwrap(); let player = t1.game().unwrap().current_player(); advance_play(&mut t1).await.unwrap(); assert_ne!(player, t1.game().unwrap().current_player()); -- cgit v1.2.3