Skip to content

Instantly share code, notes, and snippets.

@obmarg
Created June 25, 2020 13:51
Show Gist options
  • Save obmarg/ff8292ba1966c7bfd3c93f8d7fe5dede to your computer and use it in GitHub Desktop.
Save obmarg/ff8292ba1966c7bfd3c93f8d7fe5dede to your computer and use it in GitHub Desktop.
enum OrderEvent {
OrderPlaced(OrderPlacedPayload)
}
struct OrderPlacedPayload {
name: String,
whatever: String
}
mod event_formats {
struct OrderPlacedPayloadV1 {
name: String
}
impl Into<OrderPlacedPayloadV2> for OrderPlacedPayloadV1 {
// Convert v1 into v2
}
struct OrderPlacedPayloadV2 {
name: String,
whatever: String
}
impl Into<OrderPlacedPayload> for OrderPlacedPayloadV2 {
// Convert v2 into main payload
}
}
fn load_order(event_name: String, event_version: String, payload: json::Value) -> OrderPlacedPayload {
match (event_name, event_version) {
("OrderPlaced", "v1") => serde_json::from_value::<OrderPlacedPayloadV1>(payload)
.into::<OrderPlacedPayloadV2>()
.into::<OrderPlacedPayload>();
("OrderPlaced", "v2") => serde_json::from_value::<OrderPlacedPayloadV2>(payload)
.into::<OrderPlacedPayload>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment