Skip to main content

swf_core/models/
input.rs

1use crate::models::schema::*;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5/// Represents the definition of an input data model
6#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
7pub struct InputDataModelDefinition {
8    /// Gets/sets the schema, if any, that defines and describes the input 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 build the workflow or task input data based on both input and scope data
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub from: Option<Value>,
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn test_input_with_from() {
23        let json = r#"{"from": {"key": "value"}}"#;
24        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
25        assert!(input.from.is_some());
26        assert!(input.schema.is_none());
27    }
28
29    #[test]
30    fn test_input_with_schema() {
31        let json = r#"{"schema": {"format": "json", "document": {"type": "object"}}}"#;
32        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
33        assert!(input.schema.is_some());
34        assert!(input.from.is_none());
35    }
36
37    #[test]
38    fn test_input_roundtrip() {
39        let json = r#"{"from": {"key": "value"}}"#;
40        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
41        let serialized = serde_json::to_string(&input).unwrap();
42        let deserialized: InputDataModelDefinition = serde_json::from_str(&serialized).unwrap();
43        assert_eq!(input, deserialized);
44    }
45
46    // Additional tests matching Go SDK patterns
47
48    #[test]
49    fn test_input_with_from_expression() {
50        // Input with a runtime expression as the "from" value
51        let json = r#"{"from": "${ .inputData }"}"#;
52        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
53        assert!(input.from.is_some());
54        assert_eq!(input.from.unwrap(), serde_json::json!("${ .inputData }"));
55    }
56
57    #[test]
58    fn test_input_with_schema_and_from() {
59        // Both schema and from set
60        let json = r#"{
61            "schema": {"format": "json", "document": {"type": "object"}},
62            "from": {"key": "value"}
63        }"#;
64        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
65        assert!(input.schema.is_some());
66        assert!(input.from.is_some());
67    }
68
69    #[test]
70    fn test_input_default() {
71        let input = InputDataModelDefinition::default();
72        assert!(input.schema.is_none());
73        assert!(input.from.is_none());
74    }
75
76    #[test]
77    fn test_input_empty_object() {
78        let json = r#"{}"#;
79        let input: InputDataModelDefinition = serde_json::from_str(json).unwrap();
80        assert!(input.schema.is_none());
81        assert!(input.from.is_none());
82    }
83}