Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 6.0
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -15,31 +15,29 @@ let package = Package(
.visionOS(.v1)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "ReerCodable",
targets: ["ReerCodable"]
)
],
traits: [
.trait(
name: "AutoFlexibleType",
description: "Enable automatic type conversion for all @Codable/@Decodable types"
),
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", from: "600.0.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
// Macro implementation that performs the source transformation of a macro.
.macro(
name: "ReerCodableMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),

// Library that exposes a macro as part of its API, which is used in client programs.
.target(name: "ReerCodable", dependencies: ["ReerCodableMacros"]),

// A test target used to develop the macro implementation.
.testTarget(
name: "ReerCodableTests",
dependencies: [
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,25 @@ struct Settings {
}
```

#### AutoFlexibleType Trait (Swift 6.1+)

If you prefer all types automatically support flexible type conversion without explicit `@FlexibleType` annotation, you can enable the `AutoFlexibleType` trait when adding the package dependency:

```swift
// In your Package.swift
.package(
url: "https://github.com/reers/ReerCodable.git",
from: "1.6.0",
traits: ["AutoFlexibleType"]
)
```

When this trait is enabled, all `@Codable` and `@Decodable` types will automatically support flexible type conversion, just like having `@FlexibleType` applied to every type. This is useful for projects that rely on backend APIs with inconsistent data types.

> **Note:** This feature requires Swift 6.1+ and swift-tools-version: 6.1 in your Package.swift.

> **Important:** After changing traits, you must **delete DerivedData/YourProject** and **restart Xcode** for the changes to take effect.

### 18. AnyCodable Support

Implement encoding/decoding of `Any` type through `AnyCodable`:
Expand Down
19 changes: 19 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,25 @@ struct Settings {
}
```

#### AutoFlexibleType Trait (Swift 6.1+)

如果你希望所有类型自动支持灵活类型转换而无需显式标注 `@FlexibleType`,可以在添加包依赖时启用 `AutoFlexibleType` trait:

```swift
// 在你的 Package.swift 中
.package(
url: "https://github.com/reers/ReerCodable.git",
from: "1.6.0",
traits: ["AutoFlexibleType"]
)
```

启用此 trait 后,所有 `@Codable` 和 `@Decodable` 类型将自动支持灵活类型转换,就像每个类型都应用了 `@FlexibleType` 一样。这对于依赖后端 API 数据类型不一致的项目非常有用。

> **注意:** 此功能需要 Swift 6.1+ 并且你的 Package.swift 中需要设置 swift-tools-version: 6.1。

> **重要:** 修改 traits 后,必须**删除 DerivedData/YourProject** 并**重启 Xcode** 才能使更改生效。

### 18. AnyCodable 支持

通过 `AnyCodable` 实现对 `Any` 类型的编解码:
Expand Down
15 changes: 15 additions & 0 deletions Sources/ReerCodable/MacroDeclarations/FlexibleType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,20 @@
/// ```
///
/// The supported type conversions are defined in the `TypeConvertible` protocol implementations.
///
/// ## AutoFlexibleType Trait (Swift 6.1+)
///
/// If you prefer automatic type conversion for all types without explicit annotation,
/// you can enable the `AutoFlexibleType` trait when adding the package dependency:
///
/// ```swift
/// .package(
/// url: "https://github.com/reers/ReerCodable.git",
/// from: "1.6.0",
/// traits: ["AutoFlexibleType"]
/// )
/// ```
///
/// - Important: After changing traits, you must delete DerivedData/YourProject and restart Xcode for the changes to take effect.
@attached(peer)
public macro FlexibleType() = #externalMacro(module: "ReerCodableMacros", type: "FlexibleType")
4 changes: 4 additions & 0 deletions Sources/ReerCodableMacros/TypeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ struct TypeInfo {
if decl.attributes.containsAttribute(named: "FlexibleType") {
isFlexibleType = true
}
#if AutoFlexibleType
// When AutoFlexibleType trait is enabled, all types default to flexible type conversion
isFlexibleType = true
#endif
// Check if the class inherits from NSObject
if let classDecl = decl.as(ClassDeclSyntax.self),
let inheritedTypes = classDecl.inheritanceClause?.inheritedTypes {
Expand Down
Loading