|
| 1 | +# MetaCodable Architecture |
| 2 | + |
| 3 | +This document provides a high-level overview of MetaCodable's architecture and implementation details. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +MetaCodable is a Swift macro-based framework that supercharges Swift's `Codable` implementations. It uses the power of Swift macros to generate sophisticated coding logic while keeping the API simple and declarative. |
| 8 | + |
| 9 | +## Core Components |
| 10 | + |
| 11 | +```mermaid |
| 12 | +graph TD |
| 13 | + A[MetaCodable Core] --> B[Macro System] |
| 14 | + A --> C[Helper Coders] |
| 15 | + A --> D[Protocol Generation] |
| 16 | + B --> E[Attribute Processing] |
| 17 | + B --> F[Code Generation] |
| 18 | + C --> G[Built-in Coders] |
| 19 | + C --> H[Custom Coders] |
| 20 | + D --> I[Build Tool Plugin] |
| 21 | + D --> J[Dynamic Codable] |
| 22 | +
|
| 23 | + style A fill:#f9f,stroke:#333,stroke-width:4px |
| 24 | +``` |
| 25 | + |
| 26 | +### 1. Macro System |
| 27 | +The core of MetaCodable is built around Swift's macro system, primarily using: |
| 28 | +- `@Codable` - The main attribute macro that drives the code generation |
| 29 | +- `@CodedAt`, `@CodedIn`, `@CodedBy` etc. - Attribute macros for customizing coding behavior |
| 30 | +- `PluginCore` - Core functionality for macro expansions and code generation |
| 31 | + |
| 32 | +### 2. Helper Coders System |
| 33 | +Implements extensible coding strategies through: |
| 34 | +- `HelperCoder` protocol for custom coding implementations |
| 35 | +- Built-in coders like `ValueCoder`, `SequenceCoder`, etc. |
| 36 | +- Support for custom coders through `@CodedBy` attribute |
| 37 | + |
| 38 | +### 3. Protocol Generation System |
| 39 | +Provides dynamic protocol conformance through: |
| 40 | +- `MetaProtocolCodable` build tool plugin |
| 41 | +- `DynamicCodable` type for generating protocol implementations |
| 42 | +- Source code parsing and synthesis capabilities |
| 43 | + |
| 44 | +## Data Flow |
| 45 | + |
| 46 | +```mermaid |
| 47 | +sequenceDiagram |
| 48 | + participant User |
| 49 | + participant Macros |
| 50 | + participant CodeGen |
| 51 | + participant Runtime |
| 52 | +
|
| 53 | + User->>Macros: Adds @Codable attribute |
| 54 | + Macros->>CodeGen: Process attributes |
| 55 | + CodeGen->>CodeGen: Generate implementation |
| 56 | + CodeGen->>Runtime: Emit Codable conformance |
| 57 | + Runtime->>User: Use generated code |
| 58 | +``` |
| 59 | + |
| 60 | +## Key Features |
| 61 | + |
| 62 | +1. **Flexible Key Mapping** |
| 63 | + - Custom CodingKey values per property |
| 64 | + - Nested coding key support |
| 65 | + - Multiple coding key paths |
| 66 | + |
| 67 | +2. **Smart Defaults and Error Handling** |
| 68 | + - Default value specification |
| 69 | + - Custom error handling |
| 70 | + - Missing value handling |
| 71 | + |
| 72 | +3. **Protocol and Type Support** |
| 73 | + - Enum support with various tagging styles |
| 74 | + - Protocol type generation |
| 75 | + - Generic type support |
| 76 | + |
| 77 | +## Implementation Details |
| 78 | + |
| 79 | +### Attribute Processing |
| 80 | +The framework processes attributes in multiple phases: |
| 81 | +1. Attribute validation and preprocessing |
| 82 | +2. Context gathering and analysis |
| 83 | +3. Code generation and emission |
| 84 | + |
| 85 | +### Code Generation |
| 86 | +The code generation system follows these principles: |
| 87 | +1. Maintains source compatibility |
| 88 | +2. Generates optimal, efficient code |
| 89 | +3. Supports full customization through attributes |
0 commit comments