Represents a CDDL type definition.
| 424 | |
| 425 | @dataclass |
| 426 | class CddlTypeDefinition: |
| 427 | """Represents a CDDL type definition.""" |
| 428 | |
| 429 | module: str |
| 430 | name: str |
| 431 | fields: dict[str, str] = field(default_factory=dict) |
| 432 | description: str = "" |
| 433 | |
| 434 | def to_python_dataclass(self, enhancements: dict[str, Any] | None = None) -> str: |
| 435 | """Generate Python dataclass code for this type. |
| 436 | |
| 437 | Args: |
| 438 | enhancements: Dictionary containing dataclass_methods and method_docstrings |
| 439 | """ |
| 440 | enhancements = enhancements or {} |
| 441 | dataclass_methods = enhancements.get("dataclass_methods", {}) |
| 442 | method_docstrings = enhancements.get("method_docstrings", {}) |
| 443 | |
| 444 | # Generate class name from type name (keep it as-is, don't split on underscores) |
| 445 | class_name = self.name |
| 446 | code = "@dataclass\n" |
| 447 | code += f"class {class_name}:\n" |
| 448 | class_docstrings = enhancements.get("class_docstrings", {}) |
| 449 | class_doc = _docstring_text(class_docstrings.get(class_name), class_name, self.description) |
| 450 | code += _emit_docstring(class_doc, 4) |
| 451 | code += "\n" |
| 452 | |
| 453 | if not self.fields: |
| 454 | code += " pass\n" |
| 455 | else: |
| 456 | for field_name, field_type in self.fields.items(): |
| 457 | # Convert CDDL type to Python type |
| 458 | python_type = self._get_python_type(field_type) |
| 459 | snake_name = CddlCommand._camel_to_snake(field_name) |
| 460 | |
| 461 | # Check if the CDDL field type is a quoted string literal (e.g., type: "key") |
| 462 | # These are discriminant fields: auto-populate and exclude from __init__ |
| 463 | # so callers don't need to pass them as positional or keyword arguments. |
| 464 | literal_match = re.match(r'^"([^"]+)"$', field_type.strip()) |
| 465 | if literal_match: |
| 466 | literal_value = literal_match.group(1) |
| 467 | code += f' {snake_name}: str = field(default="{literal_value}", init=False)\n' |
| 468 | # Check if this field is a list type (using lowercase 'list[' from Python 3.10+ syntax) |
| 469 | elif python_type.startswith("list["): |
| 470 | # Remove the trailing ' | None' from list types since default_factory=list ensures non-None |
| 471 | type_annotation = python_type.replace(" | None", "") |
| 472 | code += f" {snake_name}: {type_annotation} = field(default_factory=list)\n" |
| 473 | # Check if this field is a dict type (using lowercase 'dict[' from Python 3.10+ syntax) |
| 474 | elif python_type.startswith("dict["): |
| 475 | # Remove the trailing ' | None' from dict types since default_factory=dict ensures non-None |
| 476 | type_annotation = python_type.replace(" | None", "") |
| 477 | code += f" {snake_name}: {type_annotation} = field(default_factory=dict)\n" |
| 478 | else: |
| 479 | code += f" {snake_name}: {python_type} = None\n" |
| 480 | |
| 481 | # Add custom methods if defined for this class |
| 482 | if class_name in dataclass_methods: |
| 483 | code += "\n" |