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
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ struct BasicPropertyVariable: DefaultPropertyVariable, DeclaredVariable {
) -> CodeBlockItemListSyntax {
switch location {
case .coder(let decoder, let passedMethod):
let optionalToken: TokenSyntax =
if passedMethod?.trimmedDescription == "decodeIfPresent" {
"?"
} else {
""
}
let optionalToken: TokenSyntax = passedMethod?.trimmedDescription == "decodeIfPresent" ? "?" : ""

var type = type
if let implicitlyUnwrappedType = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
type = TypeSyntax(OptionalTypeSyntax(wrappedType: implicitlyUnwrappedType.wrappedType))
}

return CodeBlockItemListSyntax {
"""
\(decodePrefix)\(name) = try \(type)\(optionalToken)(from: \(decoder))
Expand Down
8 changes: 7 additions & 1 deletion Sources/PluginCore/Variables/Property/PropertyVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ extension PropertyVariable {
///
/// Checks whether the type syntax uses
/// `?` optional type syntax (i.e. `Type?`) or
/// `!` implicitly unwrapped optional type syntax (i.e. `Type!`) or
/// generic optional syntax (i.e. `Optional<Type>`).
var hasOptionalType: Bool {
if type.is(OptionalTypeSyntax.self) {
return true
} else if let type = type.as(IdentifierTypeSyntax.self),
} else if type.is(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
return true
} else if let type = type.as(IdentifierTypeSyntax.self),
type.name.text == "Optional",
let gArgs = type.genericArgumentClause?.arguments,
gArgs.count == 1
Expand Down Expand Up @@ -152,6 +155,9 @@ extension PropertyVariable {
if let type = type.as(OptionalTypeSyntax.self) {
dType = type.wrappedType
dMethod = "\(method)IfPresent"
} else if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
dType = type.wrappedType
dMethod = "\(method)IfPresent"
} else if let type = type.as(IdentifierTypeSyntax.self),
type.name.text == "Optional",
let gArgs = type.genericArgumentClause?.arguments,
Expand Down
47 changes: 47 additions & 0 deletions Tests/MetaCodableTests/CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,53 @@ final class CodableTests: XCTestCase {
)
}

func testOptionalWithoutAnyCustomization() throws {
assertMacroExpansion(
"""
@Codable
struct SomeCodable {
let value1: String?
let value2: String!
let value3: Optional<String>
}
""",
expandedSource:
"""
struct SomeCodable {
let value1: String?
let value2: String!
let value3: Optional<String>
}

extension SomeCodable: Decodable {
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.value1 = try container.decodeIfPresent(String.self, forKey: CodingKeys.value1)
self.value2 = try container.decodeIfPresent(String.self, forKey: CodingKeys.value2)
self.value3 = try container.decodeIfPresent(String.self, forKey: CodingKeys.value3)
}
}

extension SomeCodable: Encodable {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(self.value1, forKey: CodingKeys.value1)
try container.encodeIfPresent(self.value2, forKey: CodingKeys.value2)
try container.encodeIfPresent(self.value3, forKey: CodingKeys.value3)
}
}

extension SomeCodable {
enum CodingKeys: String, CodingKey {
case value1 = "value1"
case value2 = "value2"
case value3 = "value3"
}
}
"""
)
}

func testWithoutAnyCustomizationWithStaticVar() throws {
assertMacroExpansion(
"""
Expand Down
Loading