ggen-core 26.6.25

Core graph-aware code generation engine
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Code Generators for A2A Schemas
//!
//! This module provides code generators for multiple target languages from parsed schemas:
//! - Java (POJOs with Jackson annotations)
//! - Elixir (structs with @type specs)
//! - TypeScript (interfaces with optional fields)

use crate::schema::{Schema, SchemaType};
use std::fmt::Write;

/// Java code generator
pub struct JavaGenerator;

impl JavaGenerator {
    /// Generate Java POJO class from schema
    pub fn generate_class(schema: &Schema) -> String {
        let mut output = String::new();

        // Package declaration
        let _ = writeln!(output, "package com.a2a.generated;");
        let _ = writeln!(output);

        // Imports
        let _ = writeln!(output, "import com.fasterxml.jackson.annotation.*;");
        let _ = writeln!(output, "import java.util.List;");
        let _ = writeln!(output, "import java.util.ArrayList;");
        let _ = writeln!(output);

        // Class Javadoc
        if let Some(desc) = &schema.description {
            let _ = writeln!(output, "/**\n * {}\n */", desc);
        }

        // Class declaration
        let _ = writeln!(output, "@JsonInclude(JsonInclude.Include.NON_NULL)");
        let _ = writeln!(output, "public class {} {{", schema.name);
        let _ = writeln!(output);

        // Private fields
        for field in &schema.fields {
            if let Some(desc) = &field.description {
                let _ = writeln!(output, "    /**\n     * {}\n     */", desc);
            }
            let _ = writeln!(
                output,
                "    private {} {};",
                Self::field_type_to_java(&field.field_type),
                Self::to_camel_case(&field.name)
            );
            let _ = writeln!(output);
        }

        // Getters and setters
        for field in &schema.fields {
            let field_name_camel = Self::to_camel_case(&field.name);
            let java_type = Self::field_type_to_java(&field.field_type);

            // Getter
            let _ = writeln!(
                output,
                "    public {} get{}() {{",
                java_type,
                Self::capitalize(&field_name_camel)
            );
            let _ = writeln!(output, "        return this.{};", field_name_camel);
            let _ = writeln!(output, "    }}");
            let _ = writeln!(output);

            // Setter
            let _ = writeln!(
                output,
                "    public void set{}({} {}) {{",
                Self::capitalize(&field_name_camel),
                java_type,
                field_name_camel
            );
            let _ = writeln!(
                output,
                "        this.{} = {};",
                field_name_camel, field_name_camel
            );
            let _ = writeln!(output, "    }}");
            let _ = writeln!(output);
        }

        // toString() method
        let _ = writeln!(output, "    @Override");
        let _ = writeln!(output, "    public String toString() {{");
        let _ = writeln!(output, "        return \"{}{{\" +", schema.name);
        let field_names: Vec<String> = schema
            .fields
            .iter()
            .map(|f| Self::to_camel_case(&f.name))
            .collect();
        for (i, field_name) in field_names.iter().enumerate() {
            let suffix = if i < field_names.len() - 1 { "," } else { "" };
            let _ = writeln!(
                output,
                "                \"{}=\" + {}{}\"",
                field_name, field_name, suffix
            );
        }
        let _ = writeln!(output, "                \"}}\";");
        let _ = writeln!(output, "    }}");

        let _ = writeln!(output, "}}");

        output
    }

    /// Convert SchemaType to Java type
    fn field_type_to_java(field_type: &SchemaType) -> String {
        match field_type {
            SchemaType::String => "String".to_string(),
            SchemaType::Integer => "Long".to_string(),
            SchemaType::Boolean => "Boolean".to_string(),
            SchemaType::Float => "Double".to_string(),
            SchemaType::Any => "Object".to_string(),
            SchemaType::Array(inner) => format!("List<{}>", Self::field_type_to_java(inner)),
            SchemaType::Object(_) => "Map<String, Object>".to_string(),
            SchemaType::Reference(name) => name.clone(),
        }
    }

    /// Convert snake_case to camelCase
    fn to_camel_case(s: &str) -> String {
        let parts: Vec<&str> = s.split('_').collect();
        let mut result = parts[0].to_string();
        for part in &parts[1..] {
            result.push_str(&Self::capitalize(part));
        }
        result
    }

    /// Capitalize first letter
    fn capitalize(s: &str) -> String {
        let mut chars = s.chars();
        match chars.next() {
            None => String::new(),
            Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
        }
    }
}

/// Elixir code generator
pub struct ElixirGenerator;

impl ElixirGenerator {
    /// Generate Elixir struct from schema
    pub fn generate_struct(schema: &Schema) -> String {
        let mut output = String::new();

        // Module documentation
        let _ = writeln!(output, "defmodule {} do", schema.name);
        let _ = writeln!(output, "  @moduledoc \"\"\"");
        if let Some(desc) = &schema.description {
            let _ = writeln!(output, "  {}", desc);
        } else {
            let _ = writeln!(output, "  Auto-generated A2A schema: {}", schema.name);
        }
        let _ = writeln!(output, "  \"\"\"");
        let _ = writeln!(output);

        // defstruct
        let _ = writeln!(output, "  defstruct [");
        for (i, field) in schema.fields.iter().enumerate() {
            let comma = if i < schema.fields.len() - 1 { "," } else { "" };
            let _ = writeln!(output, "    :{}{}", field.name, comma);
        }
        let _ = writeln!(output, "  ]");
        let _ = writeln!(output);

        // @type spec
        let _ = writeln!(output, "  @type t :: %__MODULE__{{");
        for field in &schema.fields {
            let elixir_type = Self::field_type_to_elixir(&field.field_type, field.optional);
            let _ = writeln!(output, "    {}: {},", field.name, elixir_type);
        }
        let _ = writeln!(output, "  }}");
        let _ = writeln!(output);

        // Constructor function
        let _ = writeln!(output, "  @doc \"\"\"");
        let _ = writeln!(output, "  Create a new {} struct", schema.name);
        let _ = writeln!(output, "  \"\"\"");
        let _ = writeln!(output, "  def new(fields \\\\ %{{}}) do");
        let _ = writeln!(output, "    struct = struct!(__MODULE__, fields)");
        let _ = writeln!(output, "    {{:ok, struct}}");
        let _ = writeln!(output, "  rescue");
        let _ = writeln!(output, "    error -> {{:error, error}}");
        let _ = writeln!(output, "  end");
        let _ = writeln!(output);

        // Validation function
        let _ = writeln!(output, "  @doc \"\"\"");
        let _ = writeln!(output, "  Validate required fields");
        let _ = writeln!(output, "  \"\"\"");
        let _ = writeln!(
            output,
            "  def validate(%__MODULE__{} = struct) do",
            schema
                .fields
                .iter()
                .map(|f| format!("{}: _", f.name))
                .collect::<Vec<_>>()
                .join(", ")
        );
        let _ = writeln!(output, "    required = [");
        for field in schema.fields.iter().filter(|f| !f.optional) {
            let _ = writeln!(output, "      :{},", field.name);
        }
        let _ = writeln!(output, "    ]");
        let _ = writeln!(output);
        let _ = writeln!(output, "    missing = required -- Map.keys(struct)");
        let _ = writeln!(output);
        let _ = writeln!(output, "    if Enum.empty?(missing) do");
        let _ = writeln!(output, "      {{:ok, struct}}");
        let _ = writeln!(output, "    else");
        let _ = writeln!(output, "      {{:error, {{:missing_fields, missing}}}}");
        let _ = writeln!(output, "    end");
        let _ = writeln!(output, "  end");
        let _ = writeln!(output);

        // JSON encoding helper
        let _ = writeln!(output, "  @doc \"\"\"");
        let _ = writeln!(output, "  Encode to JSON");
        let _ = writeln!(output, "  \"\"\"");
        let _ = writeln!(
            output,
            "  def to_json(%__MODULE__{} = struct) do",
            schema
                .fields
                .iter()
                .map(|f| format!("{}: _", f.name))
                .collect::<Vec<_>>()
                .join(", ")
        );
        let _ = writeln!(output, "    Jason.encode!(struct)");
        let _ = writeln!(output, "  end");

        let _ = writeln!(output, "end");

        output
    }

    /// Convert SchemaType to Elixir type
    fn field_type_to_elixir(field_type: &SchemaType, optional: bool) -> String {
        let base_type = match field_type {
            SchemaType::String => "String.t()".to_string(),
            SchemaType::Integer => "integer()".to_string(),
            SchemaType::Boolean => "boolean()".to_string(),
            SchemaType::Float => "float()".to_string(),
            SchemaType::Any => "term()".to_string(),
            SchemaType::Array(inner) => {
                format!("list({})", Self::field_type_to_elixir(inner, false))
            }
            SchemaType::Object(_) => "map()".to_string(),
            SchemaType::Reference(name) => format!("{}()", name),
        };

        if optional {
            format!("{} | nil", base_type)
        } else {
            base_type
        }
    }
}

/// TypeScript code generator
pub struct TypeScriptGenerator;

impl TypeScriptGenerator {
    /// Generate TypeScript interface from schema
    pub fn generate_interface(schema: &Schema) -> String {
        let mut output = String::new();

        // File header
        let _ = writeln!(output, "/**");
        let _ = writeln!(
            output,
            " * Auto-generated TypeScript interface from A2A schema: {}",
            schema.name
        );
        if let Some(desc) = &schema.description {
            let _ = writeln!(output, " * {}", desc);
        }
        let _ = writeln!(output, " */");
        let _ = writeln!(output);

        // Interface declaration
        let _ = writeln!(output, "/**");
        if let Some(desc) = &schema.description {
            let _ = writeln!(output, " * {}", desc);
        } else {
            let _ = writeln!(output, " * {} interface", schema.name);
        }
        let _ = writeln!(output, " */");
        let _ = writeln!(output, "export interface {} {{", schema.name);

        // Properties
        for field in &schema.fields {
            if let Some(desc) = &field.description {
                let _ = writeln!(output, "  /** {} */", desc);
            }
            let optional_marker = if field.optional { "?" } else { "" };
            let ts_type = Self::field_type_to_typescript(&field.field_type);
            let _ = writeln!(output, "  {}{}: {};", field.name, optional_marker, ts_type);
            let _ = writeln!(output);
        }

        let _ = writeln!(output, "}}");

        output
    }

    /// Convert SchemaType to TypeScript type
    fn field_type_to_typescript(field_type: &SchemaType) -> String {
        match field_type {
            SchemaType::String => "string".to_string(),
            SchemaType::Integer => "number".to_string(),
            SchemaType::Boolean => "boolean".to_string(),
            SchemaType::Float => "number".to_string(),
            SchemaType::Any => "any".to_string(),
            SchemaType::Array(inner) => format!("{}[]", Self::field_type_to_typescript(inner)),
            SchemaType::Object(_) => "Record<string, any>".to_string(),
            SchemaType::Reference(name) => name.clone(),
        }
    }
}

// Implement all code generation methods on Schema
impl Schema {
    /// Generate Rust struct
    pub fn to_rust_struct(&self) -> String {
        RustGenerator::generate(self)
    }

    /// Generate Go struct
    pub fn to_go_struct(&self) -> String {
        GoGenerator::generate(self)
    }

    /// Generate Elixir struct
    pub fn to_elixir_struct(&self) -> String {
        ElixirGenerator::generate_struct(self)
    }

    /// Generate Java class
    pub fn to_java_class(&self) -> String {
        JavaGenerator::generate_class(self)
    }

    /// Generate TypeScript interface
    pub fn to_typescript_interface(&self) -> String {
        TypeScriptGenerator::generate_interface(self)
    }
}

pub struct RustGenerator;

impl RustGenerator {
    /// Generate Rust struct from schema
    pub fn generate(schema: &crate::schema::Schema) -> String {
        let mut output = String::new();

        if let Some(desc) = &schema.description {
            let _ = writeln!(output, "/// {}", desc);
        }
        let _ = writeln!(output, "#[derive(Debug, Clone, Serialize, Deserialize)]");
        let _ = writeln!(output, "pub struct {} {{", schema.name);

        for field in &schema.fields {
            if let Some(desc) = &field.description {
                let _ = writeln!(output, "    /// {}", desc);
            }
            let rust_type = Self::field_type_to_rust(&field.field_type, field.optional);
            let _ = writeln!(output, "    pub {}: {},", field.name, rust_type);
        }

        let _ = writeln!(output, "}}");
        output
    }

    fn field_type_to_rust(field_type: &SchemaType, optional: bool) -> String {
        let base = match field_type {
            SchemaType::String => "String".to_string(),
            SchemaType::Integer => "i64".to_string(),
            SchemaType::Boolean => "bool".to_string(),
            SchemaType::Float => "f64".to_string(),
            SchemaType::Any => "serde_json::Value".to_string(),
            SchemaType::Array(inner) => format!("Vec<{}>", Self::field_type_to_rust(inner, false)),
            SchemaType::Object(_) => {
                "std::collections::HashMap<String, serde_json::Value>".to_string()
            }
            SchemaType::Reference(name) => name.clone(),
        };
        if optional {
            format!("Option<{}>", base)
        } else {
            base
        }
    }
}

/// Go code generator
pub struct GoGenerator;

impl GoGenerator {
    /// Generate Go struct from schema
    pub fn generate(schema: &crate::schema::Schema) -> String {
        let mut output = String::new();

        if let Some(desc) = &schema.description {
            let _ = writeln!(output, "// {} describes the request.", desc);
        }
        let _ = writeln!(output, "type {} struct {{", schema.name);

        for field in &schema.fields {
            let go_type = Self::field_type_to_go(&field.field_type, field.optional);
            let json_tag = if field.optional {
                format!("{},omitempty", field.name)
            } else {
                field.name.clone()
            };
            let _ = writeln!(
                output,
                "    {} {} `json:\"{}\"`",
                Self::capitalize(&field.name),
                go_type,
                json_tag
            );
        }

        let _ = writeln!(output, "}}");
        output
    }

    fn field_type_to_go(field_type: &SchemaType, _optional: bool) -> String {
        // Go uses omitempty in JSON tags for optional fields, not pointer types
        match field_type {
            SchemaType::String => "string".to_string(),
            SchemaType::Integer => "int64".to_string(),
            SchemaType::Boolean => "bool".to_string(),
            SchemaType::Float => "float64".to_string(),
            SchemaType::Any => "interface{}".to_string(),
            SchemaType::Array(inner) => format!("[]{}", Self::field_type_to_go(inner, false)),
            SchemaType::Object(_) => "map[string]interface{}".to_string(),
            SchemaType::Reference(name) => name.clone(),
        }
    }

    fn capitalize(s: &str) -> String {
        let mut chars = s.chars();
        match chars.next() {
            None => String::new(),
            Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
        }
    }
}

/// Python code generator (stub for compatibility)
pub struct PythonGenerator;

impl PythonGenerator {
    /// Generate Python dataclass from schema (placeholder)
    pub fn generate(_schema: &crate::schema::Schema) -> String {
        "# Python code generation not yet implemented for A2A schemas\n# Use OntologySchema instead"
            .to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{Field, SchemaParser};

    fn create_test_schema() -> Schema {
        Schema {
            name: "FileReadRequest".to_string(),
            description: Some("Request to read a file".to_string()),
            fields: vec![
                Field {
                    name: "path".to_string(),
                    field_type: SchemaType::String,
                    optional: false,
                    description: Some("File path to read".to_string()),
                },
                Field {
                    name: "offset".to_string(),
                    field_type: SchemaType::Integer,
                    optional: true,
                    description: Some("Byte offset to start reading".to_string()),
                },
                Field {
                    name: "limit".to_string(),
                    field_type: SchemaType::Integer,
                    optional: true,
                    description: None,
                },
            ],
            nested_schemas: vec![],
        }
    }

    #[test]
    fn test_java_generator() {
        let schema = create_test_schema();
        let java_code = schema.to_java_class();

        assert!(java_code.contains("public class FileReadRequest"));
        assert!(java_code.contains("private String path;"));
        assert!(java_code.contains("private Long offset;"));
        assert!(java_code.contains("public String getPath()"));
        assert!(java_code.contains("public void setPath(String path)"));
        assert!(java_code.contains("@JsonInclude(JsonInclude.Include.NON_NULL)"));
    }

    #[test]
    fn test_elixir_generator() {
        let schema = create_test_schema();
        let elixir_code = schema.to_elixir_struct();

        assert!(elixir_code.contains("defmodule FileReadRequest do"));
        assert!(elixir_code.contains("defstruct ["));
        assert!(elixir_code.contains(":path"));
        assert!(elixir_code.contains("@type t :: %__MODULE__{"));
        assert!(elixir_code.contains("path: String.t()"));
        assert!(elixir_code.contains("offset: integer() | nil"));
        assert!(elixir_code.contains("limit: integer() | nil"));
    }

    #[test]
    fn test_typescript_generator() {
        let schema = create_test_schema();
        let ts_code = schema.to_typescript_interface();

        assert!(ts_code.contains("export interface FileReadRequest {"));
        assert!(ts_code.contains("path: string;"));
        assert!(ts_code.contains("offset?: number;"));
        assert!(ts_code.contains("limit?: number;"));
    }

    #[test]
    fn test_array_types() {
        let schema = Schema {
            name: "ArrayTest".to_string(),
            description: None,
            fields: vec![Field {
                name: "tags".to_string(),
                field_type: SchemaType::Array(Box::new(SchemaType::String)),
                optional: false,
                description: None,
            }],
            nested_schemas: vec![],
        };

        // Java should use List<String>
        let java = schema.to_java_class();
        assert!(java.contains("List<String> tags"));

        // Elixir should use list(String.t())
        let elixir = schema.to_elixir_struct();
        assert!(elixir.contains("list(String.t())"));

        // TypeScript should use string[]
        let ts = schema.to_typescript_interface();
        assert!(ts.contains("tags: string[]"));
    }

    #[test]
    #[ignore = "JSON Schema parsing not yet implemented; enable when SchemaParser::from_json_schema is implemented"]
    fn test_nested_schema() -> Result<(), String> {
        let json_schema = r#"
        {
            "title": "NestedRequest",
            "type": "object",
            "properties": {
                "metadata": {
                    "type": "object",
                    "properties": {
                        "created_at": { "type": "string" }
                    }
                },
                "items": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            },
            "required": ["items"]
        }
        "#;

        let schema = SchemaParser::from_json_schema(json_schema)?;
        assert_eq!(schema.name, "NestedRequest");
        assert_eq!(schema.fields.len(), 2);

        let java = schema.to_java_class();
        assert!(java.contains("private List<String> items;"));
        Ok(())
    }
}