mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Remove prior experimentation with property wrappers -- feature was not baked when that experimentation was done.
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
//
|
||||
// PropertyWrappers.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 6/20/19.
|
||||
//
|
||||
|
||||
|
||||
// MARK: - Transformed
|
||||
@propertyWrapper
|
||||
public struct Transformed<Transformer: JSONAPI.Transformer> {
|
||||
|
||||
public typealias RawValue = Transformer.From
|
||||
public typealias Value = Transformer.To
|
||||
|
||||
private var _value: Value?
|
||||
|
||||
public var wrappedValue: Value {
|
||||
get {
|
||||
guard let ret = _value else {
|
||||
fatalError("Attribute read from before initialization.")
|
||||
}
|
||||
return ret
|
||||
}
|
||||
set {
|
||||
_value = newValue
|
||||
}
|
||||
}
|
||||
|
||||
public init(initialValue: Value, _ transformer: Transformer.Type) {
|
||||
self._value = initialValue
|
||||
}
|
||||
|
||||
public init(_ transformer: Transformer.Type) {
|
||||
self._value = nil
|
||||
}
|
||||
|
||||
public init(rawValue: RawValue, _ transformer: Transformer.Type) throws {
|
||||
self._value = try Transformer.transform(rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
extension Transformed: Decodable where Transformer.From: Decodable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
let rawVal = try container.decode(Transformer.From.self)
|
||||
|
||||
_value = try Transformer.transform(rawVal)
|
||||
}
|
||||
}
|
||||
|
||||
extension Transformed: Encodable where Transformer: ReversibleTransformer, Transformer.From: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
guard let value = _value else {
|
||||
fatalError("Attribute encoded before initialization.")
|
||||
}
|
||||
|
||||
try container.encode(Transformer.reverse(value))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Nullable
|
||||
|
||||
public protocol _Optional {
|
||||
static var nilValue: Self { get }
|
||||
var isNilValue: Bool { get }
|
||||
}
|
||||
|
||||
extension Optional: _Optional {
|
||||
public static var nilValue: Self {
|
||||
return .none
|
||||
}
|
||||
|
||||
public var isNilValue: Bool { return self == nil }
|
||||
}
|
||||
|
||||
protocol _Nullable {}
|
||||
|
||||
@propertyWrapper
|
||||
public struct Nullable<T: Decodable>: Decodable, _Optional, _Nullable {
|
||||
public var wrappedValue: T?
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
if container.decodeNil() {
|
||||
wrappedValue = nil
|
||||
return
|
||||
}
|
||||
|
||||
wrappedValue = try container.decode(T.self)
|
||||
}
|
||||
|
||||
public init(initialValue: T? = nil) {
|
||||
wrappedValue = initialValue
|
||||
}
|
||||
|
||||
public static var nilValue: Self {
|
||||
return .init()
|
||||
}
|
||||
|
||||
public var isNilValue: Bool {
|
||||
return wrappedValue == nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Nullable: Encodable where T: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(wrappedValue)
|
||||
}
|
||||
}
|
||||
@@ -62,70 +62,6 @@ class AttributeTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Property Wrappers
|
||||
extension AttributeTests {
|
||||
func test_Transformed() {
|
||||
|
||||
struct Test: Codable {
|
||||
@Transformed(IntToString.self)
|
||||
var value: String = ""
|
||||
}
|
||||
|
||||
let test = Test(value: "hello")
|
||||
XCTAssertEqual(test.value, "hello")
|
||||
|
||||
let test2 = try! JSONDecoder().decode(Test.self,
|
||||
from: #"{"value": 12}"#.data(using: .utf8)!)
|
||||
|
||||
XCTAssertEqual(test2.value, "12")
|
||||
try! print(String(data: JSONEncoder().encode(test2), encoding: .utf8)!)
|
||||
|
||||
let test3 = try? JSONDecoder().decode(Test.self,
|
||||
from: #"{"value": null}"#.data(using: .utf8)!)
|
||||
|
||||
XCTAssertNil(test3)
|
||||
}
|
||||
|
||||
func test_Nullable() {
|
||||
struct Test: Codable {
|
||||
@Nullable
|
||||
var value: String?
|
||||
}
|
||||
|
||||
let test = Test(value: nil)
|
||||
XCTAssertNil(test.value)
|
||||
|
||||
let test2 = Test(value: "hello")
|
||||
XCTAssertEqual(test2.value, "hello")
|
||||
|
||||
let test3 = try! JSONDecoder().decode(Test.self,
|
||||
from: #"{"value": "world"}"#.data(using: .utf8)!)
|
||||
|
||||
XCTAssertEqual(test3.value, "world")
|
||||
try! print(String(data: JSONEncoder().encode(test2), encoding: .utf8)!)
|
||||
|
||||
let test4 = try? JSONDecoder().decode(Test.self,
|
||||
from: #"{"value": null}"#.data(using: .utf8)!)
|
||||
|
||||
XCTAssertNotNil(test4)
|
||||
XCTAssertNil(test4?.value)
|
||||
}
|
||||
|
||||
func test_NullableTransformed() {
|
||||
struct Test: Codable {
|
||||
// Nullable<Transformed<IntToString>>
|
||||
let x: Transformed<IntToString>
|
||||
// @Nullable @Transformed(IdentityTransformer.self)
|
||||
@Transformed(IntToString.self) @Nullable
|
||||
var value: String?
|
||||
}
|
||||
|
||||
let test = Test(x: .init(initialValue: "12", IntToString.self))
|
||||
|
||||
print(test.x.wrappedValue)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Test types
|
||||
extension AttributeTests {
|
||||
enum TestTransformer: ReversibleTransformer {
|
||||
|
||||
Reference in New Issue
Block a user