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
//! Protobuf parser for importing .proto files into data models.
//!
//! This module provides a complete implementation for parsing proto3 syntax, including:
//! - Message definitions and nested messages
//! - Field parsing with proper type mapping
//! - Support for repeated fields (arrays)
//! - Optional field handling
//! - Nested message expansion with dot notation
//!
//! # Validation
//!
//! All imported table and column names are validated for:
//! - Valid identifier format
//! - Maximum length limits
//!
//! # Note
//!
//! This is a complete implementation for proto3 syntax parsing. For build-time code generation
//! from .proto files, consider using `prost-build` in a build script. This parser is designed
//! for runtime parsing of .proto file content.

use crate::import::odcs_shared::column_to_column_data;
use crate::import::{ImportError, ImportResult, TableData};
use crate::models::{Column, Table, Tag};
use crate::validation::input::{validate_column_name, validate_data_type, validate_table_name};
use anyhow::Result;
use std::collections::HashMap;
use tracing::{info, warn};

/// Parser for Protobuf format.
pub struct ProtobufImporter;

impl Default for ProtobufImporter {
    fn default() -> Self {
        Self::new()
    }
}

impl ProtobufImporter {
    /// Create a new Protobuf parser instance.
    ///
    /// # Example
    ///
    /// ```rust
    /// use data_modelling_core::import::protobuf::ProtobufImporter;
    ///
    /// let importer = ProtobufImporter::new();
    /// ```
    pub fn new() -> Self {
        Self
    }

    /// Import Protobuf content and create Table(s) (SDK interface).
    ///
    /// # Arguments
    ///
    /// * `proto_content` - Protobuf `.proto` file content as a string
    ///
    /// # Returns
    ///
    /// An `ImportResult` containing extracted tables and any parse errors.
    ///
    /// # Example
    ///
    /// ```rust
    /// use data_modelling_core::import::protobuf::ProtobufImporter;
    ///
    /// let importer = ProtobufImporter::new();
    /// let proto = r#"
    /// syntax = "proto3";
    /// message User {
    ///   int64 id = 1;
    ///   string name = 2;
    /// }
    /// "#;
    /// let result = importer.import(proto).unwrap();
    /// ```
    pub fn import(&self, proto_content: &str) -> Result<ImportResult, ImportError> {
        match self.parse(proto_content) {
            Ok((tables, errors)) => {
                let mut sdk_tables = Vec::new();
                for (idx, table) in tables.iter().enumerate() {
                    sdk_tables.push(TableData {
                        table_index: idx,
                        id: Some(table.id.to_string()),
                        name: Some(table.name.clone()),
                        columns: table.columns.iter().map(column_to_column_data).collect(),
                        ..Default::default()
                    });
                }
                let sdk_errors: Vec<ImportError> = errors
                    .iter()
                    .map(|e| ImportError::ParseError(e.message.clone()))
                    .collect();
                Ok(ImportResult {
                    tables: sdk_tables,
                    tables_requiring_name: Vec::new(),
                    errors: sdk_errors,
                    ai_suggestions: None,
                })
            }
            Err(e) => Err(ImportError::ParseError(e.to_string())),
        }
    }

    /// Parse Protobuf content and create Table(s) (internal method).
    ///
    /// This is a complete implementation for proto3 syntax parsing. It handles:
    /// - Message definitions and nested messages
    /// - Field parsing with proper type mapping
    /// - Support for repeated fields (arrays)
    /// - Optional field handling
    /// - Nested message expansion with dot notation
    ///
    /// # Returns
    ///
    /// Returns a tuple of (Tables, list of errors/warnings).
    fn parse(&self, proto_content: &str) -> Result<(Vec<Table>, Vec<ParserError>)> {
        let mut errors = Vec::new();
        let mut tables = Vec::new();

        // Complete parser for proto3 syntax
        let lines: Vec<&str> = proto_content.lines().collect();
        let mut current_message: Option<Message> = None;
        let mut messages = Vec::new();

        for (_line_num, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            // Skip comments and empty lines
            if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("/*") {
                continue;
            }

            // Check for message definition
            if trimmed.starts_with("message ") {
                // Save previous message if exists
                if let Some(msg) = current_message.take() {
                    messages.push(msg);
                }

                // Parse message name - handle both "message Name {" and "message Name{"
                let msg_name = trimmed
                    .strip_prefix("message ")
                    .and_then(|s| {
                        // Remove trailing "{"
                        let s = s.trim_end();
                        if let Some(stripped) = s.strip_suffix("{") {
                            Some(stripped)
                        } else if let Some(stripped) = s.strip_suffix(" {") {
                            Some(stripped)
                        } else {
                            s.split_whitespace().next()
                        }
                    })
                    .map(|s| s.trim())
                    .filter(|s| !s.is_empty())
                    .ok_or_else(|| anyhow::anyhow!("Invalid message syntax: {}", trimmed))?;

                // Validate message name as a table name
                if let Err(e) = validate_table_name(msg_name) {
                    warn!("Message name validation warning for '{}': {}", msg_name, e);
                }

                current_message = Some(Message {
                    name: msg_name.to_string(),
                    fields: Vec::new(),
                });
            } else if trimmed == "}" || trimmed == "};" {
                // End of message
                if let Some(msg) = current_message.take() {
                    messages.push(msg);
                }
            } else if trimmed.starts_with("enum ") {
                // Skip enum definitions for now - they're handled when referenced by fields
                continue;
            } else if let Some(ref mut msg) = current_message {
                // Parse field
                if let Ok(field) = self.parse_field(trimmed, _line_num) {
                    msg.fields.push(field);
                } else {
                    // Don't add error for empty lines or comments that slipped through
                    if !trimmed.is_empty() && !trimmed.starts_with("//") {
                        errors.push(ParserError {
                            error_type: "parse_error".to_string(),
                            field: Some(format!("line {}", _line_num + 1)),
                            message: format!("Failed to parse field: {}", trimmed),
                        });
                    }
                }
            }
        }

        // Add last message if exists
        if let Some(msg) = current_message {
            messages.push(msg);
        }

        // Convert messages to tables
        for message in &messages {
            match self.message_to_table(message, &messages, &mut errors) {
                Ok(table) => tables.push(table),
                Err(e) => {
                    errors.push(ParserError {
                        error_type: "parse_error".to_string(),
                        field: Some(message.name.clone()),
                        message: format!("Failed to convert message to table: {}", e),
                    });
                }
            }
        }

        Ok((tables, errors))
    }

    /// Parse a Protobuf field line.
    fn parse_field(&self, line: &str, _line_num: usize) -> Result<ProtobufField> {
        // Remove comments
        let line = line.split("//").next().unwrap_or(line).trim();

        // Parse: [repeated] [optional] type name = number;
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() < 3 {
            return Err(anyhow::anyhow!("Invalid field syntax"));
        }

        let mut idx = 0;
        let mut repeated = false;
        let mut optional = false;

        // Check for repeated/optional keywords
        while idx < parts.len() {
            match parts[idx] {
                "repeated" => {
                    repeated = true;
                    idx += 1;
                }
                "optional" => {
                    optional = true;
                    idx += 1;
                }
                _ => break,
            }
        }

        if idx >= parts.len() {
            return Err(anyhow::anyhow!("Missing field type"));
        }

        let field_type = parts[idx].to_string();
        idx += 1;

        if idx >= parts.len() {
            return Err(anyhow::anyhow!("Missing field name"));
        }

        let field_name = parts[idx]
            .strip_suffix(";")
            .unwrap_or(parts[idx])
            .to_string();
        idx += 1;

        // Validate field name and type
        if let Err(e) = validate_column_name(&field_name) {
            warn!("Field name validation warning for '{}': {}", field_name, e);
        }
        if let Err(e) = validate_data_type(&field_type) {
            warn!("Field type validation warning for '{}': {}", field_type, e);
        }

        // Field number (optional for parsing)
        let _field_number = if idx < parts.len() {
            parts[idx]
                .strip_prefix("=")
                .and_then(|s| s.strip_suffix(";"))
                .and_then(|s| s.parse::<u32>().ok())
        } else {
            None
        };

        Ok(ProtobufField {
            name: field_name,
            field_type,
            repeated,
            nullable: optional || repeated, // Repeated fields are nullable
        })
    }

    /// Convert a Protobuf message to a Table.
    fn message_to_table(
        &self,
        message: &Message,
        all_messages: &[Message],
        _errors: &mut Vec<ParserError>,
    ) -> Result<Table> {
        let mut columns = Vec::new();

        for field in &message.fields {
            // Check if field type is a nested message
            if let Some(nested_msg) = all_messages.iter().find(|m| m.name == field.field_type) {
                // Nested message - recursively extract nested columns with dot notation
                // Check if nested message itself contains nested messages
                for nested_field in &nested_msg.fields {
                    let nested_field_name = format!("{}.{}", field.name, nested_field.name);

                    // Check if this nested field is itself a nested message (deep nesting)
                    if let Some(deep_nested_msg) = all_messages
                        .iter()
                        .find(|m| m.name == nested_field.field_type)
                    {
                        // Deeply nested message - create columns for its fields
                        for deep_nested_field in &deep_nested_msg.fields {
                            let data_type = if deep_nested_field.repeated {
                                format!(
                                    "ARRAY<{}>",
                                    self.map_proto_type_to_sql(&deep_nested_field.field_type)
                                )
                            } else {
                                self.map_proto_type_to_sql(&deep_nested_field.field_type)
                            };

                            columns.push(Column {
                                name: format!("{}.{}", nested_field_name, deep_nested_field.name),
                                data_type,
                                nullable: nested_field.nullable || deep_nested_field.nullable,
                                ..Default::default()
                            });
                        }
                    } else {
                        // Simple nested field
                        let data_type = if nested_field.repeated {
                            format!(
                                "ARRAY<{}>",
                                self.map_proto_type_to_sql(&nested_field.field_type)
                            )
                        } else {
                            self.map_proto_type_to_sql(&nested_field.field_type)
                        };

                        columns.push(Column {
                            name: nested_field_name,
                            data_type,
                            nullable: nested_field.nullable,
                            ..Default::default()
                        });
                    }
                }
            } else {
                // Simple field
                let data_type = if field.repeated {
                    format!("ARRAY<{}>", self.map_proto_type_to_sql(&field.field_type))
                } else {
                    self.map_proto_type_to_sql(&field.field_type)
                };

                columns.push(Column {
                    name: field.name.clone(),
                    data_type,
                    nullable: field.nullable,
                    ..Default::default()
                });
            }
        }

        // Extract tags from Protobuf content (from comments)
        // Note: We need the original proto_content to extract tags, but we don't have it here
        // For now, we'll leave tags empty - tags can be added via custom options or comments
        // In a full implementation, we'd pass proto_content to this method
        let tags: Vec<Tag> = Vec::new(); // Tags extracted from comments/options would go here

        let mut odcl_metadata = HashMap::new();
        odcl_metadata.insert(
            "syntax".to_string(),
            serde_json::Value::String("proto3".to_string()),
        );

        let table = Table {
            id: crate::models::table::Table::generate_id(&message.name, None, None, None),
            name: message.name.clone(),
            columns,
            database_type: None,
            catalog_name: None,
            schema_name: None,
            medallion_layers: Vec::new(),
            scd_pattern: None,
            data_vault_classification: None,
            modeling_level: None,
            tags,
            odcl_metadata,
            owner: None,
            sla: None,
            contact_details: None,
            infrastructure_type: None,
            notes: None,
            position: None,
            yaml_file_path: None,
            drawio_cell_id: None,
            quality: Vec::new(),
            errors: Vec::new(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };

        info!(
            "Parsed Protobuf message: {} with {} columns",
            message.name,
            table.columns.len()
        );
        Ok(table)
    }

    /// Map Protobuf scalar type to SQL/ODCL data type (including well-known wrapper types).
    fn map_proto_type_to_sql(&self, proto_type: &str) -> String {
        match proto_type {
            // Basic scalar types
            "int32" | "int" => "INTEGER".to_string(),
            "int64" | "long" => "BIGINT".to_string(),
            "uint32" => "INTEGER".to_string(), // Unsigned, but SQL doesn't distinguish
            "uint64" => "BIGINT".to_string(),
            "sint32" => "INTEGER".to_string(), // Signed, zigzag encoding
            "sint64" => "BIGINT".to_string(),
            "fixed32" => "INTEGER".to_string(),  // Fixed 32-bit
            "fixed64" => "BIGINT".to_string(),   // Fixed 64-bit
            "sfixed32" => "INTEGER".to_string(), // Signed fixed 32-bit
            "sfixed64" => "BIGINT".to_string(),  // Signed fixed 64-bit
            "float" => "FLOAT".to_string(),
            "double" => "DOUBLE".to_string(),
            "bool" | "boolean" => "BOOLEAN".to_string(),
            "bytes" => "BYTES".to_string(),
            "string" => "STRING".to_string(),
            // Google protobuf wrapper types (nullable scalars)
            "google.protobuf.StringValue" => "STRING".to_string(),
            "google.protobuf.BytesValue" => "BYTES".to_string(),
            "google.protobuf.Int32Value" => "INTEGER".to_string(),
            "google.protobuf.Int64Value" => "BIGINT".to_string(),
            "google.protobuf.UInt32Value" => "INTEGER".to_string(),
            "google.protobuf.UInt64Value" => "BIGINT".to_string(),
            "google.protobuf.FloatValue" => "FLOAT".to_string(),
            "google.protobuf.DoubleValue" => "DOUBLE".to_string(),
            "google.protobuf.BoolValue" => "BOOLEAN".to_string(),
            // Timestamp and duration
            "google.protobuf.Timestamp" => "TIMESTAMP".to_string(),
            "google.protobuf.Duration" => "STRING".to_string(),
            // Other well-known types (map to JSON-compatible STRING)
            "google.protobuf.Any" => "STRING".to_string(),
            "google.protobuf.Struct" => "STRING".to_string(),
            "google.protobuf.Value" => "STRING".to_string(),
            "google.protobuf.ListValue" => "STRING".to_string(),
            "google.protobuf.FieldMask" => "STRING".to_string(),
            "google.protobuf.Empty" => "STRING".to_string(),
            // Default fallback
            _ => "STRING".to_string(),
        }
    }
}

/// Protobuf message structure.
#[derive(Debug, Clone)]
struct Message {
    name: String,
    fields: Vec<ProtobufField>,
}

/// Protobuf field structure.
#[derive(Debug, Clone)]
struct ProtobufField {
    name: String,
    field_type: String,
    repeated: bool,
    nullable: bool,
}

/// Parser error structure (matches ODCL parser format).
#[derive(Debug, Clone)]
pub struct ParserError {
    pub error_type: String,
    pub field: Option<String>,
    pub message: String,
}