From 68b55195c714f191c5a1944a776d77a5c7f72c93 Mon Sep 17 00:00:00 2001 From: Rehan Ali Date: Tue, 16 Apr 2024 17:32:21 +0500 Subject: [PATCH] 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 } }