1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! ODPS (Open Data Product Standard) exporter
//!
//! Exports ODPSDataProduct models to ODPS YAML format.
use crate::export::ExportError;
use crate::models::odps::*;
/// ODPS exporter for generating ODPS YAML from ODPSDataProduct models
pub struct ODPSExporter;
impl ODPSExporter {
/// Export a Data Product to ODPS YAML format (instance method for WASM compatibility)
///
/// # Arguments
///
/// * `product` - The Data Product to export
///
/// # Returns
///
/// A Result containing the YAML string in ODPS format, or an ExportError
pub fn export(&self, product: &ODPSDataProduct) -> Result<String, ExportError> {
let yaml = Self::export_product(product);
// Validate exported YAML against ODPS schema (if feature enabled)
#[cfg(feature = "odps-validation")]
{
use crate::validation::schema::validate_odps_internal;
validate_odps_internal(&yaml).map_err(ExportError::ValidationError)?;
}
Ok(yaml)
}
/// Export a Data Product to ODPS YAML format
///
/// # Arguments
///
/// * `product` - The Data Product to export
///
/// # Returns
///
/// A YAML string in ODPS format
///
/// # Example
///
/// ```rust
/// use data_modelling_core::export::odps::ODPSExporter;
/// use data_modelling_core::models::odps::*;
///
/// let product = ODPSDataProduct {
/// api_version: "v1.0.0".to_string(),
/// kind: "DataProduct".to_string(),
/// id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
/// name: Some("customer-data-product".to_string()),
/// version: Some("1.0.0".to_string()),
/// status: ODPSStatus::Active,
/// domain: None,
/// tenant: None,
/// authoritative_definitions: None,
/// description: None,
/// custom_properties: None,
/// tags: vec![],
/// input_ports: None,
/// output_ports: None,
/// management_ports: None,
/// support: None,
/// team: None,
/// product_created_ts: None,
/// created_at: None,
/// updated_at: None,
/// };
///
/// let yaml = ODPSExporter::export_product(&product);
/// assert!(yaml.contains("apiVersion: v1.0.0"));
/// assert!(yaml.contains("kind: DataProduct"));
/// ```
pub fn export_product(product: &ODPSDataProduct) -> String {
// Use direct struct serialization - serde handles all field naming and optional fields
match serde_yaml::to_string(product) {
Ok(yaml) => yaml,
Err(e) => format!("# Error serializing product: {}\n", e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_export_product_basic() {
let product = ODPSDataProduct {
api_version: "v1.0.0".to_string(),
kind: "DataProduct".to_string(),
id: "test-id".to_string(),
name: Some("Test Product".to_string()),
version: Some("1.0.0".to_string()),
status: ODPSStatus::Active,
domain: Some("test-domain".to_string()),
tenant: None,
authoritative_definitions: None,
description: None,
custom_properties: None,
tags: vec![],
input_ports: None,
output_ports: None,
management_ports: None,
support: None,
team: None,
product_created_ts: None,
created_at: None,
updated_at: None,
};
let yaml = ODPSExporter::export_product(&product);
assert!(yaml.contains("apiVersion: v1.0.0"));
assert!(yaml.contains("kind: DataProduct"));
assert!(yaml.contains("id: test-id"));
assert!(yaml.contains("status: active"));
assert!(yaml.contains("name: Test Product"));
assert!(yaml.contains("domain: test-domain"));
}
#[test]
fn test_export_product_with_ports() {
let product = ODPSDataProduct {
api_version: "v1.0.0".to_string(),
kind: "DataProduct".to_string(),
id: "test-id".to_string(),
name: None,
version: None,
status: ODPSStatus::Draft,
domain: None,
tenant: None,
authoritative_definitions: None,
description: None,
custom_properties: None,
tags: vec![],
input_ports: Some(vec![ODPSInputPort {
name: "input-1".to_string(),
version: "1.0.0".to_string(),
contract_id: "contract-123".to_string(),
tags: vec![],
custom_properties: None,
authoritative_definitions: None,
}]),
output_ports: Some(vec![ODPSOutputPort {
name: "output-1".to_string(),
description: Some("Output port description".to_string()),
r#type: Some("dataset".to_string()),
version: "1.0.0".to_string(),
contract_id: Some("contract-456".to_string()),
sbom: None,
input_contracts: None,
tags: vec![],
custom_properties: None,
authoritative_definitions: None,
}]),
management_ports: None,
support: None,
team: None,
product_created_ts: None,
created_at: None,
updated_at: None,
};
let yaml = ODPSExporter::export_product(&product);
assert!(yaml.contains("inputPorts:"));
assert!(yaml.contains("name: input-1"));
assert!(yaml.contains("contractId: contract-123"));
assert!(yaml.contains("outputPorts:"));
assert!(yaml.contains("name: output-1"));
}
#[test]
fn test_export_product_with_team() {
let product = ODPSDataProduct {
api_version: "v1.0.0".to_string(),
kind: "DataProduct".to_string(),
id: "test-id".to_string(),
name: None,
version: None,
status: ODPSStatus::Active,
domain: None,
tenant: None,
authoritative_definitions: None,
description: None,
custom_properties: None,
tags: vec![],
input_ports: None,
output_ports: None,
management_ports: None,
support: None,
team: Some(ODPSTeam {
name: Some("Data Team".to_string()),
description: Some("The data team".to_string()),
members: Some(vec![ODPSTeamMember {
username: "user@example.com".to_string(),
name: Some("John Doe".to_string()),
description: None,
role: Some("Lead".to_string()),
date_in: Some("2024-01-01".to_string()),
date_out: None,
replaced_by_username: None,
tags: vec![],
custom_properties: None,
authoritative_definitions: None,
}]),
tags: vec![],
custom_properties: None,
authoritative_definitions: None,
}),
product_created_ts: None,
created_at: None,
updated_at: None,
};
let yaml = ODPSExporter::export_product(&product);
assert!(yaml.contains("team:"));
assert!(yaml.contains("name: Data Team"));
assert!(yaml.contains("members:"));
assert!(yaml.contains("username: user@example.com"));
assert!(yaml.contains("role: Lead"));
}
}