Skip to content

Commit

Permalink
Include consistency for wrapped and projected value
Browse files Browse the repository at this point in the history
  • Loading branch information
rehannali committed Apr 16, 2024
1 parent d8e275d commit 68b5519
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
17 changes: 11 additions & 6 deletions Sources/SGSerializable/Transformable/SGTransformSerializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ import Foundation
/// You must provide custom implementation of `SGTranformable` which is needed for transformation.
@propertyWrapper
public final class SGTransformSerializable<Transform: SGTranformable> {
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
}
}
Expand Down

0 comments on commit 68b5519

Please sign in to comment.