|
| 1 | +from typing import List, Optional, Dict, Any |
| 2 | +from dataclasses import dataclass |
| 3 | + |
| 4 | + |
| 5 | +@dataclass |
| 6 | +class TreeNode: |
| 7 | + """Represents any node in the MindsDB tree structure.""" |
| 8 | + name: str |
| 9 | + class_: str # 'class' is a reserved keyword, so using 'class_' |
| 10 | + type: Optional[str] = None |
| 11 | + engine: Optional[str] = None |
| 12 | + deletable: bool = False |
| 13 | + visible: bool = True |
| 14 | + schema: Optional[str] = None # For table nodes that have schema information |
| 15 | + children: Optional[List['TreeNode']] = None |
| 16 | + |
| 17 | + def __post_init__(self): |
| 18 | + if self.children is None: |
| 19 | + self.children = [] |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def from_dict(cls, data: Dict[str, Any]) -> 'TreeNode': |
| 23 | + """Create TreeNode from dictionary data.""" |
| 24 | + children = [] |
| 25 | + if 'children' in data and data['children']: |
| 26 | + children = [cls.from_dict(child) for child in data['children']] |
| 27 | + |
| 28 | + return cls( |
| 29 | + name=data['name'], |
| 30 | + class_=data.get('class', ''), |
| 31 | + type=data.get('type'), |
| 32 | + engine=data.get('engine'), |
| 33 | + deletable=data.get('deletable', False), |
| 34 | + visible=data.get('visible', True), |
| 35 | + schema=data.get('schema'), # Include schema if present |
| 36 | + children=children |
| 37 | + ) |
| 38 | + |
| 39 | + def to_dict(self) -> Dict[str, Any]: |
| 40 | + """Convert TreeNode to dictionary.""" |
| 41 | + result = { |
| 42 | + 'name': self.name, |
| 43 | + 'class': self.class_, |
| 44 | + 'deletable': self.deletable, |
| 45 | + 'visible': self.visible |
| 46 | + } |
| 47 | + |
| 48 | + if self.type is not None: |
| 49 | + result['type'] = self.type |
| 50 | + if self.engine is not None: |
| 51 | + result['engine'] = self.engine |
| 52 | + if self.schema is not None: |
| 53 | + result['schema'] = self.schema |
| 54 | + if self.children: |
| 55 | + result['children'] = [child.to_dict() for child in self.children] |
| 56 | + |
| 57 | + return result |
0 commit comments