data-modelling-core 2.4.0

Core SDK library for model operations across platforms
Documentation
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! ODCSContract type for ODCS native data structures
//!
//! Represents the root data contract document following the ODCS v3.1.0 specification.

use super::schema::SchemaObject;
use super::supporting::{
    AuthoritativeDefinition, CustomProperty, Description, Link, Price, QualityRule, Role, Server,
    ServiceLevel, Support, Team, Terms,
};
use serde::{Deserialize, Serialize};

/// ODCSContract - the root data contract document (ODCS v3.1.0)
///
/// This is the top-level structure that represents an entire ODCS data contract.
/// It contains all contract-level metadata plus one or more schema objects (tables).
///
/// # Example
///
/// ```rust
/// use data_modelling_core::models::odcs::{ODCSContract, SchemaObject, Property};
///
/// let contract = ODCSContract::new("customer-contract", "v1.0.0")
///     .with_domain("retail")
///     .with_status("active")
///     .with_schema(
///         SchemaObject::new("customers")
///             .with_physical_type("table")
///             .with_properties(vec![
///                 Property::new("id", "integer").with_primary_key(true),
///                 Property::new("name", "string").with_required(true),
///             ])
///     );
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ODCSContract {
    // === Required Identity Fields ===
    /// API version (e.g., "v3.1.0")
    pub api_version: String,
    /// Kind identifier (always "DataContract")
    pub kind: String,
    /// Unique contract ID (UUID or other identifier)
    pub id: String,
    /// Contract version (semantic versioning recommended)
    pub version: String,
    /// Contract name
    pub name: String,

    // === Status ===
    /// Contract status: "draft", "active", "deprecated", "retired"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,

    // === Organization ===
    /// Domain this contract belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
    /// Data product this contract belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_product: Option<String>,
    /// Tenant identifier for multi-tenant systems
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<String>,

    // === Description ===
    /// Contract description (can be simple string or structured object)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<Description>,

    // === Schema (Tables) ===
    /// Schema objects (tables, views, topics) in this contract
    #[serde(default)]
    pub schema: Vec<SchemaObject>,

    // === Configuration ===
    /// Server configurations
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub servers: Vec<Server>,
    /// Team information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub team: Option<Team>,
    /// Support information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub support: Option<Support>,
    /// Role definitions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub roles: Vec<Role>,

    // === SLA & Quality ===
    /// Service level agreements
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub service_levels: Vec<ServiceLevel>,
    /// Contract-level quality rules
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub quality: Vec<QualityRule>,

    // === Pricing & Terms ===
    /// Price information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<Price>,
    /// Terms and conditions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terms: Option<Terms>,

    // === Links & References ===
    /// External links
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub links: Vec<Link>,
    /// Authoritative definitions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub authoritative_definitions: Vec<AuthoritativeDefinition>,

    // === Tags & Custom Properties ===
    /// Contract-level tags
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    /// Custom properties for format-specific metadata
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub custom_properties: Vec<CustomProperty>,

    // === Timestamps ===
    /// Contract creation timestamp (ISO 8601)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contract_created_ts: Option<String>,
}

impl Default for ODCSContract {
    fn default() -> Self {
        Self {
            api_version: "v3.1.0".to_string(),
            kind: "DataContract".to_string(),
            id: String::new(),
            version: "1.0.0".to_string(),
            name: String::new(),
            status: None,
            domain: None,
            data_product: None,
            tenant: None,
            description: None,
            schema: Vec::new(),
            servers: Vec::new(),
            team: None,
            support: None,
            roles: Vec::new(),
            service_levels: Vec::new(),
            quality: Vec::new(),
            price: None,
            terms: None,
            links: Vec::new(),
            authoritative_definitions: Vec::new(),
            tags: Vec::new(),
            custom_properties: Vec::new(),
            contract_created_ts: None,
        }
    }
}

impl ODCSContract {
    /// Create a new contract with the given name and version
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            id: uuid::Uuid::new_v4().to_string(),
            ..Default::default()
        }
    }

    /// Create a new contract with a specific ID
    pub fn new_with_id(
        id: impl Into<String>,
        name: impl Into<String>,
        version: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            version: version.into(),
            ..Default::default()
        }
    }

    /// Set the API version
    pub fn with_api_version(mut self, api_version: impl Into<String>) -> Self {
        self.api_version = api_version.into();
        self
    }

    /// Set the status
    pub fn with_status(mut self, status: impl Into<String>) -> Self {
        self.status = Some(status.into());
        self
    }

    /// Set the domain
    pub fn with_domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// Set the data product
    pub fn with_data_product(mut self, data_product: impl Into<String>) -> Self {
        self.data_product = Some(data_product.into());
        self
    }

    /// Set the tenant
    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant = Some(tenant.into());
        self
    }

    /// Set the description (simple string)
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(Description::Simple(description.into()));
        self
    }

    /// Set a structured description
    pub fn with_structured_description(mut self, description: Description) -> Self {
        self.description = Some(description);
        self
    }

    /// Add a schema object
    pub fn with_schema(mut self, schema: SchemaObject) -> Self {
        self.schema.push(schema);
        self
    }

    /// Set all schema objects
    pub fn with_schemas(mut self, schemas: Vec<SchemaObject>) -> Self {
        self.schema = schemas;
        self
    }

    /// Add a server configuration
    pub fn with_server(mut self, server: Server) -> Self {
        self.servers.push(server);
        self
    }

    /// Set the team information
    pub fn with_team(mut self, team: Team) -> Self {
        self.team = Some(team);
        self
    }

    /// Set the support information
    pub fn with_support(mut self, support: Support) -> Self {
        self.support = Some(support);
        self
    }

    /// Add a role
    pub fn with_role(mut self, role: Role) -> Self {
        self.roles.push(role);
        self
    }

    /// Add a service level
    pub fn with_service_level(mut self, service_level: ServiceLevel) -> Self {
        self.service_levels.push(service_level);
        self
    }

    /// Add a quality rule
    pub fn with_quality_rule(mut self, rule: QualityRule) -> Self {
        self.quality.push(rule);
        self
    }

    /// Set the price
    pub fn with_price(mut self, price: Price) -> Self {
        self.price = Some(price);
        self
    }

    /// Set the terms
    pub fn with_terms(mut self, terms: Terms) -> Self {
        self.terms = Some(terms);
        self
    }

    /// Add a link
    pub fn with_link(mut self, link: Link) -> Self {
        self.links.push(link);
        self
    }

    /// Add an authoritative definition
    pub fn with_authoritative_definition(mut self, definition: AuthoritativeDefinition) -> Self {
        self.authoritative_definitions.push(definition);
        self
    }

    /// Add a tag
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Set all tags
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Add a custom property
    pub fn with_custom_property(mut self, custom_property: CustomProperty) -> Self {
        self.custom_properties.push(custom_property);
        self
    }

    /// Set the contract creation timestamp
    pub fn with_contract_created_ts(mut self, timestamp: impl Into<String>) -> Self {
        self.contract_created_ts = Some(timestamp.into());
        self
    }

    /// Get the number of schema objects
    pub fn schema_count(&self) -> usize {
        self.schema.len()
    }

    /// Get a schema object by name
    pub fn get_schema(&self, name: &str) -> Option<&SchemaObject> {
        self.schema.iter().find(|s| s.name == name)
    }

    /// Get a mutable schema object by name
    pub fn get_schema_mut(&mut self, name: &str) -> Option<&mut SchemaObject> {
        self.schema.iter_mut().find(|s| s.name == name)
    }

    /// Get all schema names
    pub fn schema_names(&self) -> Vec<&str> {
        self.schema.iter().map(|s| s.name.as_str()).collect()
    }

    /// Check if this is a multi-table contract
    pub fn is_multi_table(&self) -> bool {
        self.schema.len() > 1
    }

    /// Get the first schema (for single-table contracts)
    pub fn first_schema(&self) -> Option<&SchemaObject> {
        self.schema.first()
    }

    /// Get the description as a simple string
    pub fn description_string(&self) -> Option<String> {
        self.description.as_ref().map(|d| d.as_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::odcs::Property;

    #[test]
    fn test_contract_creation() {
        let contract = ODCSContract::new("my-contract", "1.0.0")
            .with_domain("retail")
            .with_status("active");

        assert_eq!(contract.name, "my-contract");
        assert_eq!(contract.version, "1.0.0");
        assert_eq!(contract.domain, Some("retail".to_string()));
        assert_eq!(contract.status, Some("active".to_string()));
        assert_eq!(contract.api_version, "v3.1.0");
        assert_eq!(contract.kind, "DataContract");
        assert!(!contract.id.is_empty()); // UUID was generated
    }

    #[test]
    fn test_contract_with_schema() {
        let contract = ODCSContract::new("order-contract", "2.0.0")
            .with_schema(
                SchemaObject::new("orders")
                    .with_physical_type("table")
                    .with_properties(vec![
                        Property::new("id", "integer").with_primary_key(true),
                        Property::new("customer_id", "integer"),
                        Property::new("total", "number"),
                    ]),
            )
            .with_schema(
                SchemaObject::new("order_items")
                    .with_physical_type("table")
                    .with_properties(vec![
                        Property::new("id", "integer").with_primary_key(true),
                        Property::new("order_id", "integer"),
                        Property::new("product_id", "integer"),
                    ]),
            );

        assert_eq!(contract.schema_count(), 2);
        assert!(contract.is_multi_table());
        assert_eq!(contract.schema_names(), vec!["orders", "order_items"]);

        let orders = contract.get_schema("orders");
        assert!(orders.is_some());
        assert_eq!(orders.unwrap().property_count(), 3);
    }

    #[test]
    fn test_contract_serialization() {
        let contract = ODCSContract::new_with_id(
            "550e8400-e29b-41d4-a716-446655440000",
            "test-contract",
            "1.0.0",
        )
        .with_domain("test")
        .with_status("draft")
        .with_description("A test contract")
        .with_tag("test")
        .with_schema(SchemaObject::new("test_table").with_property(Property::new("id", "string")));

        let json = serde_json::to_string_pretty(&contract).unwrap();

        assert!(json.contains("\"apiVersion\": \"v3.1.0\""));
        assert!(json.contains("\"kind\": \"DataContract\""));
        assert!(json.contains("\"id\": \"550e8400-e29b-41d4-a716-446655440000\""));
        assert!(json.contains("\"name\": \"test-contract\""));
        assert!(json.contains("\"domain\": \"test\""));
        assert!(json.contains("\"status\": \"draft\""));

        // Verify camelCase
        assert!(json.contains("apiVersion"));
        assert!(!json.contains("api_version"));
    }

    #[test]
    fn test_contract_deserialization() {
        let json = r#"{
            "apiVersion": "v3.1.0",
            "kind": "DataContract",
            "id": "test-id-123",
            "version": "2.0.0",
            "name": "customer-contract",
            "status": "active",
            "domain": "customers",
            "description": "Customer data contract",
            "schema": [
                {
                    "name": "customers",
                    "physicalType": "table",
                    "properties": [
                        {
                            "name": "id",
                            "logicalType": "integer",
                            "primaryKey": true
                        },
                        {
                            "name": "name",
                            "logicalType": "string",
                            "required": true
                        }
                    ]
                }
            ],
            "tags": ["customer", "pii"]
        }"#;

        let contract: ODCSContract = serde_json::from_str(json).unwrap();
        assert_eq!(contract.api_version, "v3.1.0");
        assert_eq!(contract.kind, "DataContract");
        assert_eq!(contract.id, "test-id-123");
        assert_eq!(contract.version, "2.0.0");
        assert_eq!(contract.name, "customer-contract");
        assert_eq!(contract.status, Some("active".to_string()));
        assert_eq!(contract.domain, Some("customers".to_string()));
        assert_eq!(contract.schema_count(), 1);
        assert_eq!(contract.tags, vec!["customer", "pii"]);

        let customers = contract.get_schema("customers").unwrap();
        assert_eq!(customers.property_count(), 2);
    }

    #[test]
    fn test_structured_description() {
        let json = r#"{
            "apiVersion": "v3.1.0",
            "kind": "DataContract",
            "id": "test",
            "version": "1.0.0",
            "name": "test",
            "description": {
                "purpose": "Store customer information",
                "usage": "Read-only access for analytics"
            }
        }"#;

        let contract: ODCSContract = serde_json::from_str(json).unwrap();
        assert_eq!(
            contract.description_string(),
            Some("Store customer information".to_string())
        );
    }
}