From cf334a0ab9c4e25a0ba704521a5bec914ca4aee9 Mon Sep 17 00:00:00 2001 From: Dmitry Shlagoff Date: Tue, 8 Aug 2023 13:20:09 +0200 Subject: [PATCH 1/3] Use flat structure for agent events --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/v1/agent.rs | 39 ++++++++++++++++++++++++--------------- src/v1/mod.rs | 5 +++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 53111f2..76024c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "svc-events" -version = "0.11.0" +version = "0.12.0" edition = "2021" license = "MIT" repository = "https://github.com/foxford/svc-events" diff --git a/src/lib.rs b/src/lib.rs index 5075c0f..52452ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; pub use crate::event_id::EventId; -pub use crate::v1::{agent::AgentEventV1, ban, video_group::VideoGroupEventV1, EventV1}; +pub use crate::v1::{agent, ban, video_group::VideoGroupEventV1, EventV1}; mod event_id; mod v1; diff --git a/src/v1/agent.rs b/src/v1/agent.rs index 280a9a9..52bedf5 100644 --- a/src/v1/agent.rs +++ b/src/v1/agent.rs @@ -3,16 +3,26 @@ use serde::{Deserialize, Serialize}; use svc_agent::AgentId; #[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)] -#[serde(tag = "label", rename_all = "snake_case")] -#[serde(rename(deserialize = "AgentEvent"))] -pub enum AgentEventV1 { - Entered { agent_id: AgentId }, - Left { agent_id: AgentId }, +#[serde(rename(deserialize = "AgentEntered"))] +pub struct AgentEnteredV1 { + agent_id: AgentId, } -impl From for EventV1 { - fn from(event: AgentEventV1) -> Self { - EventV1::Agent(event) +impl From for EventV1 { + fn from(ev: AgentEnteredV1) -> Self { + EventV1::AgentEntered(ev) + } +} + +#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)] +#[serde(rename(deserialize = "AgentLeft"))] +pub struct AgentLeftV1 { + agent_id: AgentId, +} + +impl From for EventV1 { + fn from(ev: AgentLeftV1) -> Self { + EventV1::AgentLeft(ev) } } @@ -29,14 +39,14 @@ mod tests { fn serialize_test() { let agent_id = AgentId::from_str("instance01.service_name.svc.example.org") .expect("parse agent_id"); - let agent = AgentEventV1::Entered { agent_id }; - let event = EventV1::Agent(agent); + let agent = AgentEnteredV1 { agent_id }; + let event = EventV1::AgentEntered(agent); let json = serde_json::to_string(&event).expect("serialization to string"); assert_eq!( json, - "{\"entity_type\":\"agent\",\"label\":\"entered\",\"agent_id\":\"instance01.service_name.svc.example.org\"}" + "{\"type\":\"agent_entered\",\"agent_id\":\"instance01.service_name.svc.example.org\"}" ) } @@ -45,8 +55,7 @@ mod tests { let agent_id = "instance01.service_name.svc.example.org"; let json = json!( { - "entity_type": "agent", - "label": "left", + "type": "agent_left", "agent_id": agent_id, } ); @@ -54,8 +63,8 @@ mod tests { let event1 = serde_json::from_str::(&json).unwrap(); let agent_id = AgentId::from_str(agent_id).expect("parse agent_id"); - let agent = AgentEventV1::Left { agent_id }; - let event2 = EventV1::Agent(agent); + let agent = AgentLeftV1 { agent_id }; + let event2 = EventV1::AgentLeft(agent); assert_eq!(event1, event2); } diff --git a/src/v1/mod.rs b/src/v1/mod.rs index 4fdc7e7..2937a63 100644 --- a/src/v1/mod.rs +++ b/src/v1/mod.rs @@ -6,7 +6,7 @@ pub mod ban; pub mod video_group; #[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] -#[serde(tag = "entity_type", rename_all = "snake_case")] +#[serde(tag = "type", rename_all = "snake_case")] #[serde(rename(deserialize = "Event"))] pub enum EventV1 { VideoGroup(video_group::VideoGroupEventV1), @@ -16,7 +16,8 @@ pub enum EventV1 { BanVideoStreamingCompleted(ban::BanVideoStreamingCompletedV1), BanCollaborationCompleted(ban::BanCollaborationCompletedV1), BanCompleted(ban::BanCompletedV1), - Agent(agent::AgentEventV1), + AgentEntered(agent::AgentEnteredV1), + AgentLeft(agent::AgentLeftV1), } impl From for Event { From 372ede1e7dc712348f174aee7a2256e5117b4037 Mon Sep 17 00:00:00 2001 From: Dmitry Shlagoff Date: Tue, 8 Aug 2023 13:22:42 +0200 Subject: [PATCH 2/3] Fix tests --- src/lib.rs | 4 ++-- src/v1/video_group.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 52452ba..5c8645a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ mod tests { assert_eq!( json, - "{\"version\":\"v1\",\"entity_type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}" + "{\"version\":\"v1\",\"type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}" ) } @@ -41,7 +41,7 @@ mod tests { let json = json!( { "version": "v1", - "entity_type": "video_group", + "type": "video_group", "label": "updated", "created_at": 1673955105514 as i64, } diff --git a/src/v1/video_group.rs b/src/v1/video_group.rs index d786b5c..440f923 100644 --- a/src/v1/video_group.rs +++ b/src/v1/video_group.rs @@ -59,7 +59,7 @@ mod tests { assert_eq!( json, - "{\"entity_type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}" + "{\"type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}" ) } @@ -67,7 +67,7 @@ mod tests { fn deserialize_test() { let json = json!( { - "entity_type": "video_group", + "type": "video_group", "label": "updated", "created_at": 1673955105514 as i64, } From 799f0dc307d6f43189ae0b39d22f75a69d520e38 Mon Sep 17 00:00:00 2001 From: Dmitry Shlagoff Date: Tue, 8 Aug 2023 13:23:03 +0200 Subject: [PATCH 3/3] Run tests in CI --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86f57e9..ced437b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,3 +22,4 @@ jobs: - run: cargo fmt --all --check - run: cargo clippy --all-features -- -D warnings - run: cargo build --release --all-features + - run: cargo test