From 4491fcda995fc0036d033cd1fd791bc1d35acf9e Mon Sep 17 00:00:00 2001 From: Mikhail Maslo <52037202+mikhailmaslo@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:38:36 +0100 Subject: [PATCH] Replace decodeIfPresent with decode for singleValue (#10) --- .../CodableBuilders/CodableBuildersMisc.swift | 10 +++++++++- .../DecodableBuilder+Instance.swift | 18 ++++++++++++------ .../AllOfCodable/AllOfCodableMacroTests.swift | 2 +- .../AllOfDecodableMacroTests.swift | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Sources/Macro/Misc/CodableBuilders/CodableBuildersMisc.swift b/Sources/Macro/Misc/CodableBuilders/CodableBuildersMisc.swift index 2879b79..576b0e4 100644 --- a/Sources/Macro/Misc/CodableBuilders/CodableBuildersMisc.swift +++ b/Sources/Macro/Misc/CodableBuilders/CodableBuildersMisc.swift @@ -15,9 +15,17 @@ struct FunctionBuilder: CodeBuildable { let name: String let parameters: Params + let isOptionalTry: Bool + + init(name: String, parameters: Params, isOptionalTry: Bool = false) { + self.name = name + self.parameters = parameters + self.isOptionalTry = isOptionalTry + } func build() -> String { - "try \(name)(\(parameters.map { ($0.0.map { "\($0): " } ?? "") + $0.1 }.joined(separator: ",")))" + let tryKeyword = isOptionalTry ? "try?" : "try" + return "\(tryKeyword) \(name)(\(parameters.map { ($0.0.map { "\($0): " } ?? "") + $0.1 }.joined(separator: ",")))" } } diff --git a/Sources/Macro/Misc/CodableBuilders/DecodableBuilder+Instance.swift b/Sources/Macro/Misc/CodableBuilders/DecodableBuilder+Instance.swift index de85926..ff8a4c2 100644 --- a/Sources/Macro/Misc/CodableBuilders/DecodableBuilder+Instance.swift +++ b/Sources/Macro/Misc/CodableBuilders/DecodableBuilder+Instance.swift @@ -118,17 +118,23 @@ extension DecodableBuilder { (nil, type.typeDescription(preservingOptional: false)), ] + let name: String switch strategy { case .singleValue: - break + function = FunctionBuilder( + name: "container.decode", + parameters: parameters, + isOptionalTry: type.isOptional + ) case .codingKeys: + name = type.isOptional ? "container.decodeIfPresent" : "container.decode" parameters.append(("forKey", ".\(identifier)")) - } - function = FunctionBuilder( - name: type.isOptional ? "container.decodeIfPresent" : "container.decode", - parameters: parameters - ) + function = FunctionBuilder( + name: name, + parameters: parameters + ) + } } return DecodableBuilder.BuildingData.Item(identifier: identifier, function: function.build()) diff --git a/Tests/MacroCodableKitTests/AllOfCodable/AllOfCodableMacroTests.swift b/Tests/MacroCodableKitTests/AllOfCodable/AllOfCodableMacroTests.swift index f476c4b..d7e9c5e 100644 --- a/Tests/MacroCodableKitTests/AllOfCodable/AllOfCodableMacroTests.swift +++ b/Tests/MacroCodableKitTests/AllOfCodable/AllOfCodableMacroTests.swift @@ -73,7 +73,7 @@ final class AllOfCodableMacroTests: XCTestCase { extension Example__testing__: Decodable, Encodable { init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - self.brand = try container.decodeIfPresent(Brand.self) + self.brand = try? container.decode(Brand.self) self.company = try container.decode(Company.self) } func encode(to encoder: Encoder) throws { diff --git a/Tests/MacroCodableKitTests/AllOfCodable/AllOfDecodableMacroTests.swift b/Tests/MacroCodableKitTests/AllOfCodable/AllOfDecodableMacroTests.swift index d777dc8..5102906 100644 --- a/Tests/MacroCodableKitTests/AllOfCodable/AllOfDecodableMacroTests.swift +++ b/Tests/MacroCodableKitTests/AllOfCodable/AllOfDecodableMacroTests.swift @@ -69,7 +69,7 @@ final class AllOfDecodableMacroTests: XCTestCase { extension Example__testing__: Decodable { init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - self.brand = try container.decodeIfPresent(Brand.self) + self.brand = try? container.decode(Brand.self) self.company = try container.decode(Company.self) } }