Skip to content
Open
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: 5 additions & 11 deletions Sources/DependenciesMacrosMacros/EnumMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct MissingEnumCaseLabelDiagnosticMessage: DiagnosticMessage, Error {
internal struct EnumCase {
struct AssociatedValue {
let firstName: TokenSyntax
let type: TokenSyntax
let type: String
}

let identifier: TokenSyntax
Expand Down Expand Up @@ -60,9 +60,10 @@ internal struct EnumCase {

return TupleTypeElementListSyntax {
for associatedValue in associatedValues {
let syntax = "\(raw: associatedValue.firstName.text): \(raw: associatedValue.type)" as TokenSyntax
TupleTypeElementSyntax(
name: associatedValue.firstName,
type: SimpleTypeIdentifierSyntax(name: associatedValue.type)
type: "\(raw: associatedValue.type)" as TypeSyntax
)
}
}
Expand All @@ -77,14 +78,7 @@ internal struct EnumCase {

self.identifier = caseElement.identifier
self.associatedValues = try? caseElement.associatedValue?.parameterList.map { parameter in
guard let typeName = parameter.type.as(SimpleTypeIdentifierSyntax.self)?.name else {
let error = UnsuportedTypeDiagnosticMessage()
context.diagnose(Diagnostic(
node: parameter._syntaxNode,
message: error
))
throw UnsuportedTypeDiagnosticMessage()
}
let typeName = parameter.type.description

guard let firstName = parameter.firstName else {
let error = MissingEnumCaseLabelDiagnosticMessage()
Expand Down Expand Up @@ -187,7 +181,7 @@ public struct EnumCodableMacro: MemberMacro {
var cases = [String]()

for associatedValue in associatedValues {
decodeStatements.append("let \(associatedValue.firstName) = try container.decode(\(associatedValue.type).self, forKey: .\(associatedValue.firstName))")
decodeStatements.append("let \(associatedValue.firstName) = try container.decode(\(raw: associatedValue.type).self, forKey: .\(associatedValue.firstName))")
cases.append("\(associatedValue.firstName): \(associatedValue.firstName)")
}

Expand Down
48 changes: 48 additions & 0 deletions Tests/DependenciesMacrosTests/EnumCodableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,54 @@ import XCTest
@testable import DependenciesMacrosMacros

final class EnumCodableMacroTests: XCTestCase {
func testEnumWithNestedType() {
assertMacroExpansion(
"""
@EnumCodable
enum Profile {
case user(profile: User.Profile)
case admin(profile: Admin.Profile)
}
""",
expandedSource: """
enum Profile {
case user(profile: User.Profile)
case admin(profile: Admin.Profile)
enum SubType: String, Codable {
case user, admin
}
private enum CodingKeys: String, CodingKey {
case type, profile
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .user(let profile):
try container.encode(SubType.user, forKey: .type)
try container.encode(profile, forKey: .profile)
case .admin(let profile):
try container.encode(SubType.admin, forKey: .type)
try container.encode(profile, forKey: .profile)
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let subtype = try container.decode(SubType.self, forKey: .type)
switch subtype {
case .user:
let profile = try container.decode(User.Profile.self, forKey: .profile)
self = .user(profile: profile)
case .admin:
let profile = try container.decode(Admin.Profile.self, forKey: .profile)
self = .admin(profile: profile)
}
}
}
""",
macros: DependenciesMacrosPlugin.macros
)
}

func testMacro() {
assertMacroExpansion(
"""
Expand Down