pub struct SemanticVersionEventUpcaster { /* private fields */ }
Expand description

This upcasts any event that has the same event_type and an event_version that is less than the version configured on the upcaster.

use persist_es::{EventUpcaster,SemanticVersionEventUpcaster};
use serde_json::Value;
use persist_es::SerializedEvent;

let upcast_function = Box::new(|payload: Value| match payload {
            Value::Object(mut object_map) => {
                object_map.insert("country".to_string(), "USA".into());
                Value::Object(object_map)
            }
            _ => {
                panic!("the event payload is not an object")
            }
        });
let upcaster = SemanticVersionEventUpcaster::new("EventX", "2.3.4", upcast_function);

let payload: Value = serde_json::from_str(
            r#"{
                    "zip code": 98103,
                    "state": "Washington"
                   }"#,
        ).unwrap();
 let event = SerializedEvent::new(
            "".to_string(),
            0,
            "".to_string(),
            "".to_string(),
            "".to_string(),
            payload,
            Default::default(),
        );
let upcasted_event = upcaster.upcast(event);

let expected_payload: Value = serde_json::from_str(
            r#"{
                    "zip code": 98103,
                    "state": "Washington",
                    "country": "USA"
                   }"#,
        ).unwrap();
let expected_event = SerializedEvent::new(
            "".to_string(),
            0,
            "".to_string(),
            "".to_string(),
            "2.3.4".to_string(),
            expected_payload,
            Default::default(),
        );

assert_eq!(upcasted_event, expected_event);

Implementations

Creates a SemanticVersionEventUpcaster

Trait Implementations

Examines and event type and version to understand if the event should be upcasted.

Modifies the serialized event to conform the the new structure.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.