diff --git a/Sources/JSONAPI/Document/Includes.swift b/Sources/JSONAPI/Document/Includes.swift index c3a9ac4..2afb111 100644 --- a/Sources/JSONAPI/Document/Includes.swift +++ b/Sources/JSONAPI/Document/Includes.swift @@ -5,7 +5,7 @@ // Created by Mathew Polzin on 11/10/18. // -public typealias Include = Poly +public typealias Include = JSONPoly public struct Includes: Codable, Equatable { public static var none: Includes { return .init(values: []) } diff --git a/Sources/JSONAPI/Resource/Poly.swift b/Sources/JSONAPI/Resource/Poly.swift index 2f56276..e2a905a 100644 --- a/Sources/JSONAPI/Resource/Poly.swift +++ b/Sources/JSONAPI/Resource/Poly.swift @@ -5,6 +5,8 @@ // Created by Mathew Polzin on 11/22/18. // +public protocol Poly {} + /// Poly is a protocol to which types that /// are polymorphic belong to. Specifically, /// Poly1, Poly2, Poly3, etc. types conform @@ -13,7 +15,7 @@ /// disparate types under one roof for /// the purposes of JSON API compliant /// encoding or decoding. -public protocol Poly: PrimaryResource {} +public typealias JSONPoly = Poly & PrimaryResource // MARK: - Generic Decoding @@ -32,12 +34,16 @@ private func decode(_ type: Thing.Type, from container: SingleVa return ret } +public typealias PolyWrapped = Codable & Equatable + // MARK: - 0 types public protocol _Poly0: Poly { } public struct Poly0: _Poly0 { public init() {} +} +extension Poly0: PrimaryResource { public init(from decoder: Decoder) throws { throw JSONAPIEncodingError.illegalDecoding("Attempted to decode Poly0, which should represent a thing that is not expected to be found in a document.") } @@ -47,11 +53,9 @@ public struct Poly0: _Poly0 { } } -public typealias PolyWrapped = Codable & Equatable - // MARK: - 1 type public protocol _Poly1: _Poly0 { - associatedtype A: PolyWrapped + associatedtype A var a: A? { get } init(_ a: A) @@ -63,7 +67,7 @@ public extension _Poly1 { } } -public enum Poly1: _Poly1 { +public enum Poly1: _Poly1 { case a(A) public var a: A? { @@ -74,7 +78,11 @@ public enum Poly1: _Poly1 { public init(_ a: A) { self = .a(a) } +} +extension Poly1: Equatable where A: Equatable {} + +extension Poly1: Codable where A: Codable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() @@ -102,9 +110,11 @@ extension Poly1: CustomStringConvertible { } } +extension Poly1: PrimaryResource, MaybePrimaryResource where A: Codable & Equatable {} + // MARK: - 2 types public protocol _Poly2: _Poly1 { - associatedtype B: PolyWrapped + associatedtype B var b: B? { get } init(_ b: B) @@ -118,7 +128,7 @@ public extension _Poly2 { public typealias Either = Poly2 -public enum Poly2: _Poly2 { +public enum Poly2: _Poly2 { case a(A) case b(B) @@ -139,7 +149,11 @@ public enum Poly2: _Poly2 { public init(_ b: B) { self = .b(b) } +} +extension Poly2: Equatable where A: Equatable, B: Equatable {} + +extension Poly2: Codable where A: Codable, B: Codable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() @@ -183,9 +197,11 @@ extension Poly2: CustomStringConvertible { } } +extension Poly2: PrimaryResource, MaybePrimaryResource where A: PolyWrapped, B: PolyWrapped {} + // MARK: - 3 types public protocol _Poly3: _Poly2 { - associatedtype C: PolyWrapped + associatedtype C var c: C? { get } init(_ c: C) @@ -197,7 +213,7 @@ public extension _Poly3 { } } -public enum Poly3: _Poly3 { +public enum Poly3: _Poly3 { case a(A) case b(B) case c(C) @@ -228,7 +244,11 @@ public enum Poly3: _Poly3 { public init(_ c: C) { self = .c(c) } +} +extension Poly3: Equatable where A: Equatable, B: Equatable, C:Equatable {} + +extension Poly3: Codable where A: Codable, B: Codable, C: Codable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() @@ -277,9 +297,11 @@ extension Poly3: CustomStringConvertible { } } +extension Poly3: PrimaryResource, MaybePrimaryResource where A: PolyWrapped, B: PolyWrapped, C: PolyWrapped {} + // MARK: - 4 types public protocol _Poly4: _Poly3 { - associatedtype D: PolyWrapped + associatedtype D var d: D? { get } init(_ d: D) @@ -291,7 +313,7 @@ public extension _Poly4 { } } -public enum Poly4: _Poly4 { +public enum Poly4: _Poly4 { case a(A) case b(B) case c(C) @@ -332,7 +354,11 @@ public enum Poly4: _Poly5 { +public enum Poly5: _Poly5 { case a(A) case b(B) case c(C) @@ -451,7 +479,11 @@ public enum Poly5: _Poly6 { +public enum Poly6: _Poly6 { case a(A) case b(B) case c(C) @@ -585,7 +619,11 @@ public enum Poly6: _Poly7 { +public enum Poly7: _Poly7 { case a(A) case b(B) case c(C) @@ -734,7 +774,11 @@ public enum Poly7: _Poly8 { +public enum Poly8: _Poly8 { case a(A) case b(B) case c(C) @@ -899,7 +945,11 @@ public enum Poly8: _Poly9 { +public enum Poly9: _Poly9 { case a(A) case b(B) case c(C) @@ -1080,7 +1132,11 @@ public enum Poly9