Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rehannali committed Apr 16, 2024
2 parents d8e275d + 0ce1147 commit 96f3142
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 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
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -341,30 +341,30 @@ fileprivate let jsonData = """

fileprivate struct Base64Encoded: SGCodable {
@SGTransformSerializable<StringToBase64Data>(default: "".data(using: .utf8) , key: "base64Encoded")
var encoded: Data?
var encoded: Data
}

fileprivate struct ISODate: SGCodable {
@SGTransformSerializable<StringToDateISO8601>(key: "datetime")
var date: Date?
var date: Date
}

fileprivate struct RFCDate: SGCodable {
@SGTransformSerializable<StringToDateRFC3339>(key: "datetime")
var date: Date?
var date: Date
}

fileprivate struct TimeIntervalDate: SGCodable {
@SGTransformSerializable<TimeIntervalToDate>(key: "time")
var date: Date?
var date: Date
}

fileprivate struct MilliSecondDate: SGCodable {
@SGTransformSerializable<MilliSecondsToDate>(key: "timeMilli")
var date: Date?
var date: Date
}

fileprivate struct StringURL: SGCodable {
@SGTransformSerializable<StringToURL>
var url: URL?
var url: URL
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ fileprivate let jsonData = """

fileprivate struct Test: SGCodable {
@SGTransformSerializable<IntToString>(default: "")
var num: String?
var num: String

@SGTransformSerializable<IntToString>
var sum: String?
var sum: String
}


Expand Down

0 comments on commit 96f3142

Please sign in to comment.