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
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ extension InternallyTaggedEnumSwitcher: AdjacentlyTaggableSwitcher {
let switchExpr = self.encodeSwitchExpression(
over: location.selfValue, at: location, from: encoder,
in: context, withDefaultCase: location.hasDefaultCase
) { name in
) { name, _ in
let (variable, _) = identifierVariableAndKey(
name, withType: "_", context: context
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ package struct ExternallyTaggedEnumSwitcher: TaggedEnumSwitcherVariable {
let switchExpr = self.encodeSwitchExpression(
over: location.selfValue, at: location, from: contentEncoder,
in: context, withDefaultCase: location.hasDefaultCase
) { name in
) { name, hasContent in
"""
let \(contentEncoder) = \(container).superEncoder(forKey: \(name))
let \(hasContent ? contentEncoder : "_") = \(container).superEncoder(forKey: \(name))
"""
}
if let switchExpr = switchExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension EnumSwitcherVariable {
from coder: TokenSyntax,
in context: some MacroExpansionContext,
withDefaultCase default: Bool,
preSyntax: (TokenSyntax) -> CodeBlockItemListSyntax
preSyntax: ((TokenSyntax, hasContent: Bool)) -> CodeBlockItemListSyntax
) -> SwitchExprSyntax? {
let cases = location.cases
let allEncodable = cases.allSatisfy { $0.variable.encode ?? true }
Expand All @@ -144,12 +144,12 @@ extension EnumSwitcherVariable {

let generatedCode = generated.code.combined()
SwitchCaseSyntax(label: .case(label)) {
let preSyntax = preSyntax(("\(values.first!)", !generatedCode.isEmpty))
preSyntax
if !generatedCode.isEmpty {
CodeBlockItemListSyntax {
preSyntax("\(values.first!)")
generatedCode
}
} else {
generatedCode
}
if preSyntax.isEmpty && generatedCode.isEmpty {
"break"
}
}
Expand Down
82 changes: 82 additions & 0 deletions Tests/MetaCodableTests/CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,88 @@ struct CodableTests {
)
}
}

struct EnumWithoutAssociatedVariables {
@Codable
enum Foo {
case foo
}

@Test
func expansion() throws {
assertMacroExpansion(
"""
@Codable
enum Foo {
case foo
}
""",
expandedSource:
"""
enum Foo {
case foo
}

extension Foo: Decodable {
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: DecodingKeys.self)
guard container.allKeys.count == 1 else {
let context = DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "Invalid number of keys found, expected one."
)
throw DecodingError.typeMismatch(Self.self, context)
}
let contentDecoder = try container.superDecoder(forKey: container.allKeys.first.unsafelyUnwrapped)
switch container.allKeys.first.unsafelyUnwrapped {
case DecodingKeys.foo:
self = .foo
}
}
}

extension Foo: Encodable {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .foo:
let _ = container.superEncoder(forKey: CodingKeys.foo)
}
}
}

extension Foo {
enum CodingKeys: String, CodingKey {
case foo = "foo"
}
enum DecodingKeys: String, CodingKey {
case foo = "foo"
}
}
"""
)
}

@Test
func decodingFromJSON() throws {
let jsonStr = """
{
"foo": {}
}
"""
let jsonData = try #require(jsonStr.data(using: .utf8))
let decoded = try JSONDecoder().decode(Foo.self, from: jsonData)
#expect(decoded == Foo.foo)
}

@Test
func encodingToJSON() throws {
let original = Foo.foo
let encoded = try JSONEncoder().encode(original)
let json = try JSONSerialization.jsonObject(with: encoded) as? [String: Any]
#expect((json?["foo"] as? [String: Any])?.isEmpty == true)
}
}
}

#if canImport(MacroPlugin)
Expand Down
98 changes: 98 additions & 0 deletions Tests/MetaCodableTests/CodedAt/CodedAtEnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,102 @@ struct CodedAtEnumTests {
#expect(json["int"] as? Int == 42)
}
}

struct WithoutAssociatedVariables {
@Codable
@CodedAt("type")
enum Foo {
case foo
}

@Test
func expansion() throws {
assertMacroExpansion(
"""
@Codable
@CodedAt("type")
enum Foo {
case foo
}
""",
expandedSource:
"""
enum Foo {
case foo
}

extension Foo: Decodable {
init(from decoder: any Decoder) throws {
var typeContainer: KeyedDecodingContainer<CodingKeys>?
let container = try? decoder.container(keyedBy: CodingKeys.self)
if let container = container {
typeContainer = container
} else {
typeContainer = nil
}
if let typeContainer = typeContainer {
let typeString: String?
do {
typeString = try typeContainer.decodeIfPresent(String.self, forKey: CodingKeys.type) ?? nil
} catch {
typeString = nil
}
if let typeString = typeString {
switch typeString {
case "foo":
self = .foo
return
default:
break
}
}
}
let context = DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Couldn't match any cases."
)
throw DecodingError.typeMismatch(Self.self, context)
}
}

extension Foo: Encodable {
func encode(to encoder: any Encoder) throws {
let container = encoder.container(keyedBy: CodingKeys.self)
var typeContainer = container
switch self {
case .foo:
try typeContainer.encode("foo", forKey: CodingKeys.type)
}
}
}

extension Foo {
enum CodingKeys: String, CodingKey {
case type = "type"
}
}
"""
)
}

@Test
func decodingFromJSON() throws {
let jsonStr = """
{
"type": "foo"
}
"""
let jsonData = try #require(jsonStr.data(using: .utf8))
let decoded = try JSONDecoder().decode(Foo.self, from: jsonData)
#expect(decoded == Foo.foo)
}

@Test
func encodingToJSON() throws {
let original = Foo.foo
let encoded = try JSONEncoder().encode(original)
let json = try JSONSerialization.jsonObject(with: encoded) as? [String: Any]
#expect(json?["type"] as? String == "foo")
}
}
}