Skip to content

Commit 46cb009

Browse files
authored
feat: added support for RawRepresentable enum conformance generation (#147)
* feat: added support for `RawRepresentable` enum conformance generation * fix: fixed podspecs * ci: added swift-syntax 602 version
1 parent 75a24e1 commit 46cb009

File tree

22 files changed

+1081
-136
lines changed

22 files changed

+1081
-136
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,22 @@ jobs:
176176
},
177177
{
178178
"os": "ubuntu-latest",
179-
"swift": "latest"
179+
"swift": "6.2",
180+
"dependencies": {
181+
"swift-syntax": "602.0.0"
182+
}
180183
},
181184
{
182185
"os": "macos-15",
183186
"swift": "6.1.2"
184187
}
185188
]
186189
}
190+
# disable until prebuilt macro issues are fixed
191+
# {
192+
# "os": "ubuntu-latest",
193+
# "swift": "latest"
194+
# },
187195

188196
cocoapods-test:
189197
name: CocoaPods

MetaCodableHelperCoders.podspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ Pod::Spec.new do |s|
44
s.module_name = "HelperCoders"
55
s.define
66
s.dependency 'MetaCodableMacro', "= #{s.version}"
7+
8+
s.pod_target_xcconfig = {
9+
'OTHER_SWIFT_FLAGS' => "-Xfrontend -package-name -Xfrontend MetaCodable",
10+
}
711
end

MetaCodableMacro.podspec

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ Pod::Spec.new do |s|
2828
:execution_position => :before_compile
2929
}
3030

31-
xcconfig = {
31+
s.user_target_xcconfig = {
3232
'OTHER_SWIFT_FLAGS' => "-Xfrontend -load-plugin-executable -Xfrontend #{plugin_path}",
3333
'METACODABLE_PLUGIN_BUILD_ENVIRONMENT' => 'METACODABLE_BEING_USED_FROM_COCOAPODS=true'
3434
}
35-
s.user_target_xcconfig = xcconfig
36-
s.pod_target_xcconfig = xcconfig
35+
s.pod_target_xcconfig = {
36+
'OTHER_SWIFT_FLAGS' => "-Xfrontend -load-plugin-executable -Xfrontend #{plugin_path} -Xfrontend -package-name -Xfrontend MetaCodable",
37+
'METACODABLE_PLUGIN_BUILD_ENVIRONMENT' => 'METACODABLE_BEING_USED_FROM_COCOAPODS=true'
38+
}
3739
end

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let package = Package(
2020
.plugin(name: "MetaProtocolCodable", targets: ["MetaProtocolCodable"]),
2121
],
2222
dependencies: [
23-
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"602.0.0"),
23+
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"603.0.0"),
2424
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
2525
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
2626
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
@@ -47,6 +47,7 @@ let package = Package(
4747
.product(name: "SwiftSyntax", package: "swift-syntax"),
4848
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
4949
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
50+
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
5051
]
5152
),
5253
.target(name: "MetaCodable", dependencies: ["MacroPlugin"]),

[email protected]

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let package = Package(
2020
.plugin(name: "MetaProtocolCodable", targets: ["MetaProtocolCodable"]),
2121
],
2222
dependencies: [
23-
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"602.0.0"),
23+
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"603.0.0"),
2424
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
2525
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
2626
],
@@ -46,6 +46,7 @@ let package = Package(
4646
.product(name: "SwiftSyntax", package: "swift-syntax"),
4747
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
4848
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
49+
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
4950
]
5051
),
5152
.target(name: "MetaCodable", dependencies: ["MacroPlugin"]),

Sources/HelperCoders/ValueCoders/Number.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
protocol NumberCodingStrategy: ValueCodingStrategy where Value == Self {}
44

55
public extension ValueCodingStrategy
6-
where Value: Decodable & ExpressibleByIntegerLiteral & LosslessStringConvertible
6+
where Value: Decodable & ExpressibleByIntegerLiteral & LosslessStringConvertible & Sendable
77
{
88
/// Decodes numeric data from the given `decoder`.
99
///

Sources/MacroPlugin/Definitions.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ struct UnTagged: PeerMacro {
707707
/// implementation depending on the type of attached declaration:
708708
/// * `struct`/`class`/`enum`/`actor` types: Expansion of `Decodable`
709709
/// protocol conformance members.
710-
public struct ConformDecodable: MemberMacro, ExtensionMacro {
710+
struct ConformDecodable: MemberMacro, ExtensionMacro {
711711
/// Expand to produce members for `Decodable`.
712712
///
713713
/// Membership macro expansion for `ConformDecodable` macro
@@ -719,7 +719,7 @@ public struct ConformDecodable: MemberMacro, ExtensionMacro {
719719
/// - context: The context in which to perform the macro expansion.
720720
///
721721
/// - Returns: Delegated member expansion from `PluginCore.ConformDecodable`.
722-
public static func expansion(
722+
static func expansion(
723723
of node: AttributeSyntax,
724724
providingMembersOf declaration: some DeclGroupSyntax,
725725
in context: some MacroExpansionContext
@@ -743,7 +743,7 @@ public struct ConformDecodable: MemberMacro, ExtensionMacro {
743743
/// - context: The context in which to perform the macro expansion.
744744
///
745745
/// - Returns: Delegated member expansion from `PluginCore.ConformDecodable`.
746-
public static func expansion(
746+
static func expansion(
747747
of node: AttributeSyntax,
748748
providingMembersOf declaration: some DeclGroupSyntax,
749749
conformingTo protocols: [TypeSyntax],
@@ -769,7 +769,7 @@ public struct ConformDecodable: MemberMacro, ExtensionMacro {
769769
/// - context: The context in which to perform the macro expansion.
770770
///
771771
/// - Returns: Delegated extension expansion from `PluginCore.ConformDecodable`.
772-
public static func expansion(
772+
static func expansion(
773773
of node: AttributeSyntax,
774774
attachedTo declaration: some DeclGroupSyntax,
775775
providingExtensionsOf type: some TypeSyntaxProtocol,
@@ -791,7 +791,7 @@ public struct ConformDecodable: MemberMacro, ExtensionMacro {
791791
/// implementation depending on the type of attached declaration:
792792
/// * `struct`/`class`/`enum`/`actor` types: Expansion of `Encodable`
793793
/// protocol conformance members.
794-
public struct ConformEncodable: MemberMacro, ExtensionMacro {
794+
struct ConformEncodable: MemberMacro, ExtensionMacro {
795795
/// Expand to produce members for `Encodable`.
796796
///
797797
/// Membership macro expansion for `ConformEncodable` macro
@@ -803,7 +803,7 @@ public struct ConformEncodable: MemberMacro, ExtensionMacro {
803803
/// - context: The context in which to perform the macro expansion.
804804
///
805805
/// - Returns: Delegated member expansion from `PluginCore.ConformEncodable`.
806-
public static func expansion(
806+
static func expansion(
807807
of node: AttributeSyntax,
808808
providingMembersOf declaration: some DeclGroupSyntax,
809809
in context: some MacroExpansionContext
@@ -827,7 +827,7 @@ public struct ConformEncodable: MemberMacro, ExtensionMacro {
827827
/// - context: The context in which to perform the macro expansion.
828828
///
829829
/// - Returns: Delegated member expansion from `PluginCore.ConformEncodable`.
830-
public static func expansion(
830+
static func expansion(
831831
of node: AttributeSyntax,
832832
providingMembersOf declaration: some DeclGroupSyntax,
833833
conformingTo protocols: [TypeSyntax],
@@ -853,7 +853,7 @@ public struct ConformEncodable: MemberMacro, ExtensionMacro {
853853
/// - context: The context in which to perform the macro expansion.
854854
///
855855
/// - Returns: Delegated extension expansion from `PluginCore.ConformEncodable`.
856-
public static func expansion(
856+
static func expansion(
857857
of node: AttributeSyntax,
858858
attachedTo declaration: some DeclGroupSyntax,
859859
providingExtensionsOf type: some TypeSyntaxProtocol,

Sources/MetaCodable/MetaCodable.docc/Limitations.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ enum SomeEnum {
6161
}
6262
```
6363

64-
### Why enums with raw value aren't supported?
65-
66-
`Swift` compiler by default generates `Codable` conformance for `enum`s with raw value and `MetaCodable` has nothing extra to add for these type of `enum`s. Hence, in this case the default compiler generated implementation can be used.
67-
6864
### Why actor conformance to Encodable not generated?
6965

7066
For `actor`s ``Codable(commonStrategies:)`` generates `Decodable` conformance, while `Encodable` conformance isn't generated, only `encode(to:)` method implementation is generated which is isolated to `actor`.
7167

7268
To generate `Encodable` conformance, the `encode(to:)` method must be `nonisolated` to `actor`, and since `encode(to:)` method must be synchronous making it `nonisolated` will prevent accessing mutable properties.
7369

74-
Due to these limitations, `Encodable` conformance isn't generated, users has to implement the conformance manually.
70+
Due to these limitations, `Encodable` conformance isn't generated, users have to implement the conformance manually.
7571

7672
### Why MetaProtocolCodable plugin can't scan Xcode target dependencies?
7773

Sources/MetaCodable/MetaCodable.docc/MetaCodable.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Supercharge `Swift`'s `Codable` implementations with macros.
1010

1111
`MetaCodable` framework exposes custom macros which can be used to generate dynamic `Codable` implementations. The core of the framework is ``Codable(commonStrategies:)`` macro which generates the implementation aided by data provided with using other macros.
1212

13-
1413
`MetaCodable` aims to supercharge your `Codable` implementations by providing these inbox features:
1514

1615
- Allows custom `CodingKey` value declaration per variable with ``CodedAt(_:)`` passing single argument, instead of requiring you to write all the `CodingKey` values.

Sources/PluginCore/Attributes/CodedBy.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ package struct CodedBy: PropertyAttribute {
5454
isEnum || isProtocol,
5555
AggregatedDiagnosticProducer {
5656
mustBeCombined(with: Codable.self)
57-
mustBeCombined(
58-
with: CodedAt.self, or: DecodedAt.self, EncodedAt.self
57+
`if`(
58+
isRawRepresentableEnum,
59+
mustBeCombined(with: Codable.self),
60+
else: mustBeCombined(
61+
with: CodedAt.self,
62+
or: DecodedAt.self, EncodedAt.self
63+
)
5964
)
6065
},
6166
else: AggregatedDiagnosticProducer {

0 commit comments

Comments
 (0)