Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Poly14 support #15

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/Poly/Collection+Poly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ extension Collection where Element: _Poly13 {
return compactMap { $0.m }
}
}

// MARK: 14 types
extension Collection where Element: _Poly14 {
public subscript(type: Element.N.Type) -> [Element.N] {
return compactMap { $0.n }
}
}
4 changes: 4 additions & 0 deletions Sources/Poly/Poly+AllTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ extension Poly12 {
extension Poly13 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self, I.self, J.self, K.self, L.self, M.self] }
}

extension Poly14 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self, I.self, J.self, K.self, L.self, M.self, N.self] }
}
74 changes: 74 additions & 0 deletions Sources/Poly/Poly+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,77 @@ extension Poly13: Decodable where A: Decodable, B: Decodable, C: Decodable, D: D
self = val
}
}

// MARK: - 13 types
extension Poly14: Encodable where A: Encodable, B: Encodable, C: Encodable, D: Encodable, E: Encodable, F: Encodable, G: Encodable, H: Encodable, I: Encodable, J: Encodable, K: Encodable, L: Encodable, M: Encodable, N: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
case .c(let c):
try container.encode(c)
case .d(let d):
try container.encode(d)
case .e(let e):
try container.encode(e)
case .f(let f):
try container.encode(f)
case .g(let g):
try container.encode(g)
case .h(let h):
try container.encode(h)
case .i(let i):
try container.encode(i)
case .j(let j):
try container.encode(j)
case .k(let k):
try container.encode(k)
case .l(let l):
try container.encode(l)
case .m(let m):
try container.encode(m)
case .n(let n):
try container.encode(n)
}
}
}

extension Poly14: Decodable where A: Decodable, B: Decodable, C: Decodable, D: Decodable, E: Decodable, F: Decodable, G: Decodable, H: Decodable, I: Decodable, J: Decodable, K: Decodable, L: Decodable, M: Decodable, N: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

let attempts = [
try decode(A.self, from: container).map { Poly14.a($0) },
try decode(B.self, from: container).map { Poly14.b($0) },
try decode(C.self, from: container).map { Poly14.c($0) },
try decode(D.self, from: container).map { Poly14.d($0) },
try decode(E.self, from: container).map { Poly14.e($0) },
try decode(F.self, from: container).map { Poly14.f($0) },
try decode(G.self, from: container).map { Poly14.g($0) },
try decode(H.self, from: container).map { Poly14.h($0) },
try decode(I.self, from: container).map { Poly14.i($0) },
try decode(J.self, from: container).map { Poly14.j($0) },
try decode(K.self, from: container).map { Poly14.k($0) },
try decode(L.self, from: container).map { Poly14.l($0) },
try decode(M.self, from: container).map { Poly14.m($0) },
try decode(N.self, from: container).map { Poly14.n($0) }]

let maybeVal: Poly14<A, B, C, D, E, F, G, H, I, J, K, L, M, N>? = attempts
.lazy
.compactMap { $0.value }
.first

guard let val = maybeVal else {
let individualFailures = attempts.map { $0.error }.compactMap { $0 }

throw PolyDecodeNoTypesMatchedError(codingPath: decoder.codingPath,
individualTypeFailures: individualFailures)
}

self = val
}
}
38 changes: 38 additions & 0 deletions Sources/Poly/Poly+CustomStringConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,41 @@ extension Poly13: CustomStringConvertible {
return "Poly(\(str))"
}
}

extension Poly14: CustomStringConvertible {
public var description: String {
let str: String
switch self {
case .a(let a):
str = String(describing: a)
case .b(let b):
str = String(describing: b)
case .c(let c):
str = String(describing: c)
case .d(let d):
str = String(describing: d)
case .e(let e):
str = String(describing: e)
case .f(let f):
str = String(describing: f)
case .g(let g):
str = String(describing: g)
case .h(let h):
str = String(describing: h)
case .i(let i):
str = String(describing: i)
case .j(let j):
str = String(describing: j)
case .k(let k):
str = String(describing: k)
case .l(let l):
str = String(describing: l)
case .m(let m):
str = String(describing: m)
case .n(let n):
str = String(describing: n)
}

return "Poly(\(str))"
}
}
182 changes: 182 additions & 0 deletions Sources/Poly/Poly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,3 +1428,185 @@ public enum Poly13<A, B, C, D, E, F, G, H, I, J, K, L, M>: _Poly13 {

extension Poly13: Equatable where A: Equatable, B: Equatable, C: Equatable, D: Equatable, E: Equatable, F: Equatable, G: Equatable, H: Equatable, I: Equatable, J: Equatable, K: Equatable, L: Equatable, M: Equatable {}
extension Poly13: Hashable where A: Hashable, B: Hashable, C: Hashable, D: Hashable, E: Hashable, F: Hashable, G: Hashable, H: Hashable, I: Hashable, J: Hashable, K: Hashable, L: Hashable, M: Hashable {}


// MARK: - 14 types
public protocol _Poly14: _Poly13 {
associatedtype N

/// Get the value if it is of type `L`
var n: N? { get }

init(_ n: N)
}

public extension _Poly14 {
subscript(_ lookup: N.Type) -> N? {
return n
}
}
/// See `Poly` for documentation
public enum Poly14<A, B, C, D, E, F, G, H, I, J, K, L, M, N>: _Poly14 {
case a(A)
case b(B)
case c(C)
case d(D)
case e(E)
case f(F)
case g(G)
case h(H)
case i(I)
case j(J)
case k(K)
case l(L)
case m(M)
case n(N)

public var a: A? {
guard case let .a(ret) = self else { return nil }
return ret
}

public init(_ a: A) {
self = .a(a)
}

public var b: B? {
guard case let .b(ret) = self else { return nil }
return ret
}

public init(_ b: B) {
self = .b(b)
}

public var c: C? {
guard case let .c(ret) = self else { return nil }
return ret
}

public init(_ c: C) {
self = .c(c)
}

public var d: D? {
guard case let .d(ret) = self else { return nil }
return ret
}

public init(_ d: D) {
self = .d(d)
}

public var e: E? {
guard case let .e(ret) = self else { return nil }
return ret
}

public init(_ e: E) {
self = .e(e)
}

public var f: F? {
guard case let .f(ret) = self else { return nil }
return ret
}

public init(_ f: F) {
self = .f(f)
}

public var g: G? {
guard case let .g(ret) = self else { return nil }
return ret
}

public init(_ g: G) {
self = .g(g)
}

public var h: H? {
guard case let .h(ret) = self else { return nil }
return ret
}

public init(_ h: H) {
self = .h(h)
}

public var i: I? {
guard case let .i(ret) = self else { return nil }
return ret
}

public init(_ i: I) {
self = .i(i)
}

public var j: J? {
guard case let .j(ret) = self else { return nil }
return ret
}

public init(_ j: J) {
self = .j(j)
}

public var k: K? {
guard case let .k(ret) = self else { return nil }
return ret
}

public init(_ k: K) {
self = .k(k)
}

public var l: L? {
guard case let .l(ret) = self else { return nil }
return ret
}

public init(_ l: L) {
self = .l(l)
}

public var m: M? {
guard case let .m(ret) = self else { return nil }
return ret
}

public init(_ m: M) {
self = .m(m)
}

public var n: N? {
guard case let .n(ret) = self else { return nil }
return ret
}

public init(_ n: N) {
self = .n(n)
}

public var value: Any {
switch self {
case .a(let ret): return ret
case .b(let ret): return ret
case .c(let ret): return ret
case .d(let ret): return ret
case .e(let ret): return ret
case .f(let ret): return ret
case .g(let ret): return ret
case .h(let ret): return ret
case .i(let ret): return ret
case .j(let ret): return ret
case .k(let ret): return ret
case .l(let ret): return ret
case .m(let ret): return ret
case .n(let ret): return ret
}
}
}

extension Poly14: Equatable where A: Equatable, B: Equatable, C: Equatable, D: Equatable, E: Equatable, F: Equatable, G: Equatable, H: Equatable, I: Equatable, J: Equatable, K: Equatable, L: Equatable, M: Equatable, N: Equatable {}
extension Poly14: Hashable where A: Hashable, B: Hashable, C: Hashable, D: Hashable, E: Hashable, F: Hashable, G: Hashable, H: Hashable, I: Hashable, J: Hashable, K: Hashable, L: Hashable, M: Hashable, N: Hashable {}
52 changes: 40 additions & 12 deletions Tests/PolyTests/CollectionPolyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,30 @@ class CollectionPolyTests: XCTestCase {
test_DecodeEncodeEquality(type: [Poly13<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11, TestType12, TestType13>].self,
data: thirteen_different_type_values)
}

func test_FourteenDifferentTypes() {
let values = decoded(type: [Poly14<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11, TestType12, TestType13, TestType14>].self,
data: fourteen_different_type_values)

XCTAssertEqual(values[TestType1.self].count, 1)
XCTAssertEqual(values[TestType2.self].count, 1)
XCTAssertEqual(values[TestType3.self].count, 1)
XCTAssertEqual(values[TestType4.self].count, 1)
XCTAssertEqual(values[TestType5.self].count, 1)
XCTAssertEqual(values[TestType6.self].count, 1)
XCTAssertEqual(values[TestType7.self].count, 1)
XCTAssertEqual(values[TestType8.self].count, 1)
XCTAssertEqual(values[TestType9.self].count, 1)
XCTAssertEqual(values[TestType10.self].count, 1)
XCTAssertEqual(values[TestType11.self].count, 1)
XCTAssertEqual(values[TestType12.self].count, 1)
XCTAssertEqual(values[TestType13.self].count, 1)
}

func test_FourteenDifferentTypes_encode() {
test_DecodeEncodeEquality(type: [Poly14<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11, TestType12, TestType13, TestType14>].self,
data: fourteen_different_type_values)
}
}

// MARK: - Test Types
Expand Down Expand Up @@ -295,19 +319,23 @@ extension CollectionPolyTests {
let i: Int
}

struct TestType10: Codable, Equatable {
let j: Int
}
struct TestType10: Codable, Equatable {
let j: Int
}

struct TestType11: Codable, Equatable {
let k: Int
}
struct TestType11: Codable, Equatable {
let k: Int
}

struct TestType12: Codable, Equatable {
let l: Int
}
struct TestType12: Codable, Equatable {
let l: Int
}

struct TestType13: Codable, Equatable {
let m: Int
}
struct TestType13: Codable, Equatable {
let m: Int
}

struct TestType14: Codable, Equatable {
let n: Int
}
}
Loading
Loading