Skip to main content

swf_core/models/
output.rs

1use crate::models::schema::*;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5/// Represents the definition of an output data model
6#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
7pub struct OutputDataModelDefinition {
8    /// Gets/sets the schema, if any, that defines and describes the output data of a workflow or task
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub schema: Option<SchemaDefinition>,
11
12    /// Gets/sets a runtime expression, if any, used to output specific data to the scope data
13    #[serde(rename = "as", skip_serializing_if = "Option::is_none")]
14    pub as_: Option<Value>,
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn test_output_with_as() {
23        let json = r#"{"as": {"result": "output"}}"#;
24        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
25        assert!(output.as_.is_some());
26        assert!(output.schema.is_none());
27    }
28
29    #[test]
30    fn test_output_with_schema() {
31        let json = r#"{"schema": {"format": "json", "document": {"type": "string"}}}"#;
32        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
33        assert!(output.schema.is_some());
34        assert!(output.as_.is_none());
35    }
36
37    #[test]
38    fn test_output_roundtrip() {
39        let json = r#"{"as": {"result": "output"}}"#;
40        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
41        let serialized = serde_json::to_string(&output).unwrap();
42        let deserialized: OutputDataModelDefinition = serde_json::from_str(&serialized).unwrap();
43        assert_eq!(output, deserialized);
44    }
45
46    // Additional tests matching Go SDK patterns
47
48    #[test]
49    fn test_output_with_as_expression() {
50        // Output with a runtime expression as the "as" value
51        let json = r#"{"as": "${ .result }"}"#;
52        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
53        assert!(output.as_.is_some());
54        assert_eq!(output.as_.unwrap(), serde_json::json!("${ .result }"));
55    }
56
57    #[test]
58    fn test_output_with_schema_and_as() {
59        // Both schema and as set
60        let json = r#"{
61            "schema": {"format": "json", "document": {"type": "object"}},
62            "as": {"result": "output"}
63        }"#;
64        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
65        assert!(output.schema.is_some());
66        assert!(output.as_.is_some());
67    }
68
69    #[test]
70    fn test_output_default() {
71        let output = OutputDataModelDefinition::default();
72        assert!(output.schema.is_none());
73        assert!(output.as_.is_none());
74    }
75
76    #[test]
77    fn test_output_schema_roundtrip() {
78        let json = r#"{"schema": {"format": "json", "document": {"type": "string"}}}"#;
79        let output: OutputDataModelDefinition = serde_json::from_str(json).unwrap();
80        let serialized = serde_json::to_string(&output).unwrap();
81        let deserialized: OutputDataModelDefinition = serde_json::from_str(&serialized).unwrap();
82        assert_eq!(output, deserialized);
83    }
84}