From 68b55195c714f191c5a1944a776d77a5c7f72c93 Mon Sep 17 00:00:00 2001 From: Rehan Ali Date: Tue, 16 Apr 2024 17:32:21 +0500 Subject: [PATCH 1/2] Include consistency for wrapped and projected value --- .../Transformable/SGTransformDecoder.swift | 2 +- .../Transformable/SGTransformEncoder.swift | 2 +- .../Transformable/SGTransformSerializable.swift | 17 +++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Sources/SGSerializable/Transformable/SGTransformDecoder.swift b/Sources/SGSerializable/Transformable/SGTransformDecoder.swift index a920eb0..3d96f83 100644 --- a/Sources/SGSerializable/Transformable/SGTransformDecoder.swift +++ b/Sources/SGSerializable/Transformable/SGTransformDecoder.swift @@ -11,6 +11,6 @@ import Foundation extension SGTransformSerializable: SGDecoder where Transform.FromType: Decodable { func decodeValue(from container: DecodeContainer, with key: String) throws { let value = try container.decodeIfPresent(Transform.FromType.self, forKey: getKey(with: key)) - wrappedValue = Transform.transform(from: value) + _wrappedValue = Transform.transform(from: value) } } diff --git a/Sources/SGSerializable/Transformable/SGTransformEncoder.swift b/Sources/SGSerializable/Transformable/SGTransformEncoder.swift index 1d76c88..b1156d9 100644 --- a/Sources/SGSerializable/Transformable/SGTransformEncoder.swift +++ b/Sources/SGSerializable/Transformable/SGTransformEncoder.swift @@ -10,6 +10,6 @@ import Foundation extension SGTransformSerializable: SGEncoder where Transform.FromType: Encodable { func encodeValue(from container: inout EncodeContainer, with key: String) throws { - try container.encodeIfPresent(Transform.transform(from: wrappedValue), forKey: getKey(with: key)) + try container.encodeIfPresent(Transform.transform(from: _wrappedValue), forKey: getKey(with: key)) } } diff --git a/Sources/SGSerializable/Transformable/SGTransformSerializable.swift b/Sources/SGSerializable/Transformable/SGTransformSerializable.swift index 5f1b3a8..453ba43 100644 --- a/Sources/SGSerializable/Transformable/SGTransformSerializable.swift +++ b/Sources/SGSerializable/Transformable/SGTransformSerializable.swift @@ -13,21 +13,26 @@ import Foundation /// You must provide custom implementation of `SGTranformable` which is needed for transformation. @propertyWrapper public final class SGTransformSerializable { - public var wrappedValue: Transform.ToType? + internal var _wrappedValue: Transform.ToType? public var name: String? - public var projectedValue: Transform.ToType { - set { wrappedValue = newValue } + public var wrappedValue: Transform.ToType { + set { _wrappedValue = newValue } get { - guard let wrappedValue = wrappedValue else { + guard let safeValue = _wrappedValue else { return getFallBack(Transform.ToType.self) } - return wrappedValue + return safeValue } } + public var projectedValue: Transform.ToType? { + set { _wrappedValue = newValue } + get { return _wrappedValue } + } + public init(default value: Transform.ToType? = nil, key: String? = nil) { - self.wrappedValue = value + self._wrappedValue = value self.name = key } } From 0ce1147958f06f3449209a314469f937e340ce01 Mon Sep 17 00:00:00 2001 From: Rehan Ali Date: Tue, 16 Apr 2024 17:32:44 +0500 Subject: [PATCH 2/2] update test for consistency introduced --- .../SGAdvancedTransformSerializable.swift | 28 +++++++++---------- .../SGBasicTransformSerializable.swift | 4 +-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Tests/SGSerializableTests/Transform/SGAdvancedTransformSerializable.swift b/Tests/SGSerializableTests/Transform/SGAdvancedTransformSerializable.swift index f46c719..80cded6 100644 --- a/Tests/SGSerializableTests/Transform/SGAdvancedTransformSerializable.swift +++ b/Tests/SGSerializableTests/Transform/SGAdvancedTransformSerializable.swift @@ -111,8 +111,8 @@ class SGAdvancedTransformSerializable: QuickSpec, EncodableTestSpec, DecodableTe it("should access default value before [En/De]Coding") { object = RFCDate() expect(object).toNot(beNil()) - expect(object?.date).to(beNil()) - expect(object?.$date) == Date(timeIntervalSince1970: 0) + expect(object?.$date).to(beNil()) + expect(object?.date) == Date(timeIntervalSince1970: 0) } it("should decode properly using Default") { @@ -166,8 +166,8 @@ class SGAdvancedTransformSerializable: QuickSpec, EncodableTestSpec, DecodableTe it("should access default value before [En/De]Coding") { object = ISODate() expect(object).toNot(beNil()) - expect(object?.date).to(beNil()) - expect(object?.$date).to(equal(Date(timeIntervalSince1970: 0))) + expect(object?.$date).to(beNil()) + expect(object?.date).to(equal(Date(timeIntervalSince1970: 0))) } it("should decode properly using Default") { @@ -221,8 +221,8 @@ class SGAdvancedTransformSerializable: QuickSpec, EncodableTestSpec, DecodableTe it("should access default value before [En/De]Coding") { object = TimeIntervalDate() expect(object).toNot(beNil()) - expect(object?.date).to(beNil()) - expect(object?.$date).to(equal(Date(timeIntervalSince1970: 0))) + expect(object?.$date).to(beNil()) + expect(object?.date).to(equal(Date(timeIntervalSince1970: 0))) } it("should decode properly using Default") { @@ -276,8 +276,8 @@ class SGAdvancedTransformSerializable: QuickSpec, EncodableTestSpec, DecodableTe it("should access default value before [En/De]Coding") { object = MilliSecondDate() expect(object).toNot(beNil()) - expect(object?.date).to(beNil()) - expect(object?.$date).to(equal(Date(timeIntervalSince1970: 0))) + expect(object?.$date).to(beNil()) + expect(object?.date).to(equal(Date(timeIntervalSince1970: 0))) } it("should decode properly using Default") { @@ -341,30 +341,30 @@ fileprivate let jsonData = """ fileprivate struct Base64Encoded: SGCodable { @SGTransformSerializable(default: "".data(using: .utf8) , key: "base64Encoded") - var encoded: Data? + var encoded: Data } fileprivate struct ISODate: SGCodable { @SGTransformSerializable(key: "datetime") - var date: Date? + var date: Date } fileprivate struct RFCDate: SGCodable { @SGTransformSerializable(key: "datetime") - var date: Date? + var date: Date } fileprivate struct TimeIntervalDate: SGCodable { @SGTransformSerializable(key: "time") - var date: Date? + var date: Date } fileprivate struct MilliSecondDate: SGCodable { @SGTransformSerializable(key: "timeMilli") - var date: Date? + var date: Date } fileprivate struct StringURL: SGCodable { @SGTransformSerializable - var url: URL? + var url: URL } diff --git a/Tests/SGSerializableTests/Transform/SGBasicTransformSerializable.swift b/Tests/SGSerializableTests/Transform/SGBasicTransformSerializable.swift index 300d402..dddca5e 100644 --- a/Tests/SGSerializableTests/Transform/SGBasicTransformSerializable.swift +++ b/Tests/SGSerializableTests/Transform/SGBasicTransformSerializable.swift @@ -90,10 +90,10 @@ fileprivate let jsonData = """ fileprivate struct Test: SGCodable { @SGTransformSerializable(default: "") - var num: String? + var num: String @SGTransformSerializable - var sum: String? + var sum: String }