relay_event_schema/protocol/contexts/
spring.rs

1use crate::processor::ProcessValue;
2use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};
3
4/// Spring context.
5///
6/// The Spring context contains attributes that are specific to Spring / Spring Boot applications.
7#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
8pub struct SpringContext {
9    /// A list of the active Spring profiles.
10    pub active_profiles: Annotated<Vec<Annotated<String>>>,
11    /// Additional arbitrary fields for forwards compatibility.
12    #[metastructure(additional_properties, retain = true, pii = "maybe")]
13    pub other: Object<Value>,
14}
15
16impl super::DefaultContext for SpringContext {
17    fn default_key() -> &'static str {
18        "spring"
19    }
20
21    fn from_context(context: super::Context) -> Option<Self> {
22        match context {
23            super::Context::Spring(c) => Some(*c),
24            _ => None,
25        }
26    }
27
28    fn cast(context: &super::Context) -> Option<&Self> {
29        match context {
30            super::Context::Spring(c) => Some(c),
31            _ => None,
32        }
33    }
34
35    fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
36        match context {
37            super::Context::Spring(c) => Some(c),
38            _ => None,
39        }
40    }
41
42    fn into_context(self) -> super::Context {
43        super::Context::Spring(Box::new(self))
44    }
45}
46
47#[cfg(test)]
48mod test {
49    use super::*;
50    use crate::protocol::Context;
51
52    #[test]
53    fn test_spring_context_roundtrip() {
54        let json = r#"{
55  "active_profiles": [
56    "some",
57    "profiles"
58  ],
59  "unknown_key": [
60    123
61  ],
62  "type": "spring"
63}"#;
64
65        let active_profiles = Annotated::new(vec![
66            Annotated::new("some".to_owned()),
67            Annotated::new("profiles".to_owned()),
68        ]);
69        let other = {
70            let mut map = Object::new();
71            map.insert(
72                "unknown_key".to_owned(),
73                Annotated::new(Value::Array(vec![Annotated::new(Value::I64(123))])),
74            );
75            map
76        };
77        let context = Annotated::new(Context::Spring(Box::new(SpringContext {
78            active_profiles,
79            other,
80        })));
81
82        assert_eq!(context, Annotated::from_json(json).unwrap());
83        assert_eq!(json, context.to_json_pretty().unwrap());
84    }
85}