|
| 1 | +# SmartCodable Package 迁移指南 |
| 2 | + |
| 3 | +## 📋 变更概述 |
| 4 | + |
| 5 | +SmartCodable 现在提供了两种产品选择,让用户可以根据需求选择合适的版本: |
| 6 | + |
| 7 | +### 🎯 新的产品结构 |
| 8 | + |
| 9 | +| 产品名称 | 依赖 | 适用场景 | 编译速度 | |
| 10 | +|---------|------|---------|---------| |
| 11 | +| **SmartCodable** | 无 swift-syntax | 不需要继承功能的项目 | ⚡️ 快 | |
| 12 | +| **SmartCodableWithMacros** | 包含 swift-syntax | 需要 @SmartSubclass 继承功能 | 🐢 较慢 | |
| 13 | +| **SmartCodableInherit** (已废弃) | 包含 swift-syntax | 向后兼容,建议迁移到 SmartCodableWithMacros | 🐢 较慢 | |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🚀 使用方式 |
| 18 | + |
| 19 | +### 方式 1: 核心功能(推荐大多数用户) |
| 20 | + |
| 21 | +如果你不需要类继承功能,使用这个版本可以获得更快的编译速度: |
| 22 | + |
| 23 | +```swift |
| 24 | +// Package.swift |
| 25 | +dependencies: [ |
| 26 | + .package(url: "https://github.com/iAmMccc/SmartCodable.git", from: "6.0.1") |
| 27 | +], |
| 28 | +targets: [ |
| 29 | + .target( |
| 30 | + name: "MyApp", |
| 31 | + dependencies: [ |
| 32 | + .product(name: "SmartCodable", package: "SmartCodable") |
| 33 | + ] |
| 34 | + ) |
| 35 | +] |
| 36 | +``` |
| 37 | + |
| 38 | +**特性支持:** |
| 39 | +- ✅ 基础解码/编码 |
| 40 | +- ✅ 类型兼容和默认值 |
| 41 | +- ✅ 所有 PropertyWrapper (@SmartAny, @SmartIgnored, @SmartFlat 等) |
| 42 | +- ✅ 解码策略和回调 |
| 43 | +- ✅ Struct 和 Class 的基础使用 |
| 44 | +- ❌ 不支持 @SmartSubclass 宏(类继承) |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 方式 2: 完整功能(需要继承支持) |
| 49 | + |
| 50 | +如果你需要使用 `@SmartSubclass` 进行类继承: |
| 51 | + |
| 52 | +```swift |
| 53 | +// Package.swift |
| 54 | +dependencies: [ |
| 55 | + .package(url: "https://github.com/iAmMccc/SmartCodable.git", from: "6.0.1") |
| 56 | +], |
| 57 | +targets: [ |
| 58 | + .target( |
| 59 | + name: "MyApp", |
| 60 | + dependencies: [ |
| 61 | + .product(name: "SmartCodableWithMacros", package: "SmartCodable") |
| 62 | + ] |
| 63 | + ) |
| 64 | +] |
| 65 | +``` |
| 66 | + |
| 67 | +**特性支持:** |
| 68 | +- ✅ 所有核心功能 |
| 69 | +- ✅ @SmartSubclass 宏支持 |
| 70 | +- ✅ 类继承场景 |
| 71 | + |
| 72 | +**注意事项:** |
| 73 | +- 需要 Xcode 15+ 和 Swift 5.9+ |
| 74 | +- 首次构建会下载 swift-syntax 依赖(可能较慢) |
| 75 | +- 编译时间会比核心版本长 |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## 🔄 迁移指南 |
| 80 | + |
| 81 | +### 从旧版本迁移 |
| 82 | + |
| 83 | +#### 场景 1: 你没有使用 @SmartSubclass |
| 84 | + |
| 85 | +**之前:** |
| 86 | +```swift |
| 87 | +dependencies: [ |
| 88 | + .product(name: "SmartCodable", package: "SmartCodable") |
| 89 | +] |
| 90 | +``` |
| 91 | + |
| 92 | +**现在:** |
| 93 | +```swift |
| 94 | +// 无需修改,继续使用 SmartCodable |
| 95 | +dependencies: [ |
| 96 | + .product(name: "SmartCodable", package: "SmartCodable") |
| 97 | +] |
| 98 | +``` |
| 99 | + |
| 100 | +✅ 无需任何代码修改,编译速度会更快! |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +#### 场景 2: 你使用了 @SmartSubclass |
| 105 | + |
| 106 | +**之前:** |
| 107 | +```swift |
| 108 | +dependencies: [ |
| 109 | + .product(name: "SmartCodable", package: "SmartCodable"), |
| 110 | + .product(name: "SmartCodableInherit", package: "SmartCodable") |
| 111 | +] |
| 112 | +``` |
| 113 | + |
| 114 | +**现在(推荐):** |
| 115 | +```swift |
| 116 | +// 使用新的统一产品 |
| 117 | +dependencies: [ |
| 118 | + .product(name: "SmartCodableWithMacros", package: "SmartCodable") |
| 119 | +] |
| 120 | +``` |
| 121 | + |
| 122 | +**或者(向后兼容):** |
| 123 | +```swift |
| 124 | +// 保持原样也可以工作,但建议迁移 |
| 125 | +dependencies: [ |
| 126 | + .product(name: "SmartCodable", package: "SmartCodable"), |
| 127 | + .product(name: "SmartCodableInherit", package: "SmartCodable") |
| 128 | +] |
| 129 | +``` |
| 130 | + |
| 131 | +✅ 无需修改代码,只需更新 Package.swift |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## 💡 如何选择? |
| 136 | + |
| 137 | +### 使用 SmartCodable(核心版本)如果: |
| 138 | +- ✅ 你只使用 Struct |
| 139 | +- ✅ 你的 Class 不需要继承 |
| 140 | +- ✅ 你想要更快的编译速度 |
| 141 | +- ✅ 你想减少依赖体积 |
| 142 | + |
| 143 | +### 使用 SmartCodableWithMacros(完整版本)如果: |
| 144 | +- ✅ 你需要使用 @SmartSubclass |
| 145 | +- ✅ 你有类继承的场景 |
| 146 | +- ✅ 你需要在子类中重写 SmartCodable 协议方法 |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## 🔧 技术细节 |
| 151 | + |
| 152 | +### 依赖关系 |
| 153 | + |
| 154 | +``` |
| 155 | +SmartCodable (核心) |
| 156 | +├── 无外部依赖 |
| 157 | +└── 包含所有核心功能 |
| 158 | +
|
| 159 | +SmartCodableWithMacros (完整) |
| 160 | +├── SmartCodable (核心) |
| 161 | +├── SmartCodableInherit (宏暴露层) |
| 162 | +└── swift-syntax (宏实现) |
| 163 | + └── SwiftSyntax |
| 164 | + └── SwiftSyntaxMacros |
| 165 | + └── SwiftCompilerPlugin |
| 166 | + └── ... |
| 167 | +``` |
| 168 | + |
| 169 | +### swift-syntax 版本 |
| 170 | + |
| 171 | +```swift |
| 172 | +.package(url: "https://github.com/swiftlang/swift-syntax", from: "509.0.0") |
| 173 | +``` |
| 174 | + |
| 175 | +- 支持 Swift 5.9+ |
| 176 | +- 使用 `from:` 语义化版本,兼容性更好 |
| 177 | +- 自动支持未来的 swift-syntax 版本 |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## 📊 性能对比 |
| 182 | + |
| 183 | +| 指标 | SmartCodable | SmartCodableWithMacros | |
| 184 | +|-----|-------------|----------------------| |
| 185 | +| 首次编译时间 | ⚡️ 快 | 🐢 慢(需下载 swift-syntax) | |
| 186 | +| 增量编译时间 | ⚡️ 快 | 🔶 中等 | |
| 187 | +| 包大小 | 📦 小 | 📦 大 | |
| 188 | +| 功能完整性 | 🔶 核心功能 | ✅ 完整功能 | |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## ❓ 常见问题 |
| 193 | + |
| 194 | +### Q1: 我应该使用哪个版本? |
| 195 | + |
| 196 | +**A:** 如果不确定,先使用 `SmartCodable`(核心版本)。只有在遇到类继承需求时,再切换到 `SmartCodableWithMacros`。 |
| 197 | + |
| 198 | +### Q2: 切换版本需要修改代码吗? |
| 199 | + |
| 200 | +**A:** 不需要!只需修改 Package.swift 中的产品名称即可。 |
| 201 | + |
| 202 | +### Q3: SmartCodableInherit 还能用吗? |
| 203 | + |
| 204 | +**A:** 可以,但已标记为废弃。建议迁移到 `SmartCodableWithMacros`,它们功能完全相同。 |
| 205 | + |
| 206 | +### Q4: 为什么要分成两个产品? |
| 207 | + |
| 208 | +**A:** |
| 209 | +- swift-syntax 是一个很大的依赖(编译慢、体积大) |
| 210 | +- 大多数用户不需要继承功能 |
| 211 | +- 分离后,不需要宏的用户可以获得更快的编译速度 |
| 212 | + |
| 213 | +### Q5: 这个改动会破坏现有代码吗? |
| 214 | + |
| 215 | +**A:** 不会!这是完全向后兼容的改动: |
| 216 | +- 原有的 `SmartCodable` 产品继续工作 |
| 217 | +- 原有的 `SmartCodableInherit` 产品继续工作 |
| 218 | +- 只是新增了 `SmartCodableWithMacros` 作为推荐选项 |
| 219 | + |
| 220 | +--- |
| 221 | + |
| 222 | +## 🎯 推荐实践 |
| 223 | + |
| 224 | +### 新项目 |
| 225 | + |
| 226 | +```swift |
| 227 | +// 1. 先使用核心版本 |
| 228 | +dependencies: [ |
| 229 | + .product(name: "SmartCodable", package: "SmartCodable") |
| 230 | +] |
| 231 | + |
| 232 | +// 2. 如果需要继承,再升级到完整版本 |
| 233 | +dependencies: [ |
| 234 | + .product(name: "SmartCodableWithMacros", package: "SmartCodable") |
| 235 | +] |
| 236 | +``` |
| 237 | + |
| 238 | +### 现有项目 |
| 239 | + |
| 240 | +```swift |
| 241 | +// 1. 评估是否使用了 @SmartSubclass |
| 242 | +// 2. 如果没有,保持使用 SmartCodable(自动获得性能提升) |
| 243 | +// 3. 如果有,迁移到 SmartCodableWithMacros |
| 244 | +``` |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | +## 📝 更新日志 |
| 249 | + |
| 250 | +### 版本 6.0.1 |
| 251 | + |
| 252 | +- 🟢 新增 `SmartCompact` 命名空间(Array / Dictionary 宽容解析) |
| 253 | +- ✨ 新增 `SmartCodableWithMacros` 产品 |
| 254 | +- 🔧 修改 swift-syntax 版本约束为 `from: "509.0.0"` |
| 255 | +- 📚 改进产品文档和注释 |
| 256 | +- ⚠️ 标记 `SmartCodableInherit` 为废弃(但仍可用) |
| 257 | + |
| 258 | +--- |
| 259 | + |
| 260 | +## 🤝 贡献 |
| 261 | + |
| 262 | +如果你有任何问题或建议,欢迎: |
| 263 | +- 提交 Issue |
| 264 | +- 发起 Discussion |
| 265 | +- 提交 Pull Request |
| 266 | + |
| 267 | +--- |
| 268 | + |
| 269 | +## 📖 相关资源 |
| 270 | + |
| 271 | +- [SmartCodable 主仓库](https://github.com/iAmMccc/SmartCodable) |
| 272 | +- [Swift Package Manager 文档](https://swift.org/package-manager/) |
| 273 | +- [swift-syntax 仓库](https://github.com/swiftlang/swift-syntax) |
| 274 | +- [Swift Macros 文档](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/) |
0 commit comments