diff --git a/Package.swift b/Package.swift index 3c40bb8..3fb4c2b 100644 --- a/Package.swift +++ b/Package.swift @@ -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 @@ -15,19 +15,21 @@ 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: [ @@ -35,11 +37,7 @@ let package = Package( .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: [ diff --git a/README.md b/README.md index 86bf023..a9e5110 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/README_CN.md b/README_CN.md index 2f76d3f..306d175 100644 --- a/README_CN.md +++ b/README_CN.md @@ -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` 类型的编解码: diff --git a/Sources/ReerCodable/MacroDeclarations/FlexibleType.swift b/Sources/ReerCodable/MacroDeclarations/FlexibleType.swift index 89b7151..d89dc5f 100644 --- a/Sources/ReerCodable/MacroDeclarations/FlexibleType.swift +++ b/Sources/ReerCodable/MacroDeclarations/FlexibleType.swift @@ -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") diff --git a/Sources/ReerCodableMacros/TypeInfo.swift b/Sources/ReerCodableMacros/TypeInfo.swift index 463f145..a3f18a6 100644 --- a/Sources/ReerCodableMacros/TypeInfo.swift +++ b/Sources/ReerCodableMacros/TypeInfo.swift @@ -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 {