Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Use flat structure for agent events #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}"
)
}

Expand All @@ -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,
}
Expand Down
39 changes: 24 additions & 15 deletions src/v1/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AgentEventV1> for EventV1 {
fn from(event: AgentEventV1) -> Self {
EventV1::Agent(event)
impl From<AgentEnteredV1> 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<AgentLeftV1> for EventV1 {
fn from(ev: AgentLeftV1) -> Self {
EventV1::AgentLeft(ev)
}
}

Expand All @@ -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\"}"
)
}

Expand All @@ -45,17 +55,16 @@ 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,
}
);
let json = serde_json::to_string(&json).expect("serialization to string");
let event1 = serde_json::from_str::<EventV1>(&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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<EventV1> for Event {
Expand Down
4 changes: 2 additions & 2 deletions src/v1/video_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ mod tests {

assert_eq!(
json,
"{\"entity_type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}"
"{\"type\":\"video_group\",\"label\":\"created\",\"created_at\":1673955105514}"
)
}

#[test]
fn deserialize_test() {
let json = json!(
{
"entity_type": "video_group",
"type": "video_group",
"label": "updated",
"created_at": 1673955105514 as i64,
}
Expand Down