@@ -17,46 +17,46 @@ public protocol RelationshipType: Codable {
1717/// a JSON API "Resource Linkage."
1818/// See https://jsonapi.org/format/#document-resource-object-linkage
1919/// A convenient typealias might make your code much more legible: `One<EntityDescription>`
20- public struct ToOneRelationship < Relatable : JSONAPI . OptionalRelatable , MetaType: JSONAPI . Meta , LinksType: JSONAPI . Links > : RelationshipType , Equatable {
20+ public struct ToOneRelationship < OptionalRelatable : JSONAPI . OptionalRelatable , MetaType: JSONAPI . Meta , LinksType: JSONAPI . Links > : RelationshipType , Equatable {
2121
22- public let id : Relatable . WrappedIdentifier
22+ public let id : OptionalRelatable . WrappedId
2323
2424 public let meta : MetaType
2525 public let links : LinksType
2626
27- public init ( id: Relatable . WrappedIdentifier , meta: MetaType , links: LinksType ) {
27+ public init ( id: OptionalRelatable . WrappedId , meta: MetaType , links: LinksType ) {
2828 self . id = id
2929 self . meta = meta
3030 self . links = links
3131 }
3232}
3333
3434extension ToOneRelationship where MetaType == NoMetadata , LinksType == NoLinks {
35- public init ( id: Relatable . WrappedIdentifier ) {
35+ public init ( id: OptionalRelatable . WrappedId ) {
3636 self . init ( id: id, meta: . none, links: . none)
3737 }
3838}
3939
40- extension ToOneRelationship where Relatable . WrappedIdentifier == Relatable . Identifier {
41- public init < E: EntityType > ( entity: E , meta: MetaType , links: LinksType ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
40+ extension ToOneRelationship {
41+ public init < E: EntityType > ( entity: E , meta: MetaType , links: LinksType ) where E. Id == OptionalRelatable . WrappedId {
4242 self . init ( id: entity. id, meta: meta, links: links)
4343 }
4444}
4545
46- extension ToOneRelationship where Relatable . WrappedIdentifier == Relatable . Identifier , MetaType == NoMetadata , LinksType == NoLinks {
47- public init < E: EntityType > ( entity: E ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
46+ extension ToOneRelationship where MetaType == NoMetadata , LinksType == NoLinks {
47+ public init < E: EntityType > ( entity: E ) where E. Id == OptionalRelatable . WrappedId {
4848 self . init ( id: entity. id, meta: . none, links: . none)
4949 }
5050}
5151
52- extension ToOneRelationship where Relatable . WrappedIdentifier == Relatable . Identifier ? {
53- public init < E: EntityType > ( entity: E ? , meta: MetaType , links: LinksType ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
52+ extension ToOneRelationship where OptionalRelatable . WrappedId == OptionalRelatable . Wrapped . Identifier ? {
53+ public init < E: EntityType > ( entity: E ? , meta: MetaType , links: LinksType ) where E. Id == OptionalRelatable . Wrapped . Identifier {
5454 self . init ( id: entity? . id, meta: meta, links: links)
5555 }
5656}
5757
58- extension ToOneRelationship where Relatable . WrappedIdentifier == Relatable . Identifier ? , MetaType == NoMetadata , LinksType == NoLinks {
59- public init < E: EntityType > ( entity: E ? ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
58+ extension ToOneRelationship where OptionalRelatable . WrappedId == OptionalRelatable . Wrapped . Identifier ? , MetaType == NoMetadata , LinksType == NoLinks {
59+ public init < E: EntityType > ( entity: E ? ) where E. Id == OptionalRelatable . Wrapped . Identifier {
6060 self . init ( id: entity? . id, meta: . none, links: . none)
6161 }
6262}
@@ -78,20 +78,20 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI
7878 self . links = links
7979 }
8080
81- public init < T: JSONAPI . Relatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] , meta: MetaType , links: LinksType ) where T. WrappedIdentifier == Relatable . Identifier {
81+ public init < T: JSONAPI . OptionalRelatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] , meta: MetaType , links: LinksType ) where T. WrappedId == Relatable . Identifier {
8282 ids = pointers. map { $0. id }
8383 self . meta = meta
8484 self . links = links
8585 }
8686
87- public init < E: EntityType > ( entities: [ E ] , meta: MetaType , links: LinksType ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
87+ public init < E: EntityType > ( entities: [ E ] , meta: MetaType , links: LinksType ) where E. Id == Relatable . Identifier {
8888 self . init ( ids: entities. map { $0. id } , meta: meta, links: links)
8989 }
9090
9191 private init ( meta: MetaType , links: LinksType ) {
9292 self . init ( ids: [ ] , meta: meta, links: links)
9393 }
94-
94+
9595 public static func none( withMeta meta: MetaType , links: LinksType ) -> ToManyRelationship {
9696 return ToManyRelationship ( meta: meta, links: links)
9797 }
@@ -103,36 +103,44 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
103103 self . init ( ids: ids, meta: . none, links: . none)
104104 }
105105
106- public init < T: JSONAPI . Relatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] ) where T. WrappedIdentifier == Relatable . Identifier {
106+ public init < T: JSONAPI . OptionalRelatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] ) where T. WrappedId == Relatable . Identifier {
107107 self . init ( pointers: pointers, meta: . none, links: . none)
108108 }
109109
110110 public static var none : ToManyRelationship {
111111 return . none( withMeta: . none, links: . none)
112112 }
113113
114- public init < E: EntityType > ( entities: [ E ] ) where E. Description == Relatable . Description , E . Id == Relatable . Identifier {
114+ public init < E: EntityType > ( entities: [ E ] ) where E. Id == Relatable . Identifier {
115115 self . init ( entities: entities, meta: . none, links: . none)
116116 }
117117}
118118
119- /// The WrappedRelatable (a.k.a OptionalRelatable) protocol
120- /// describes Optional<T: Relatable> and Relatable types.
121- public protocol WrappedRelatable : Codable , Equatable {
122- associatedtype Description : EntityDescription
119+ /// The Relatable protocol describes anything that
120+ /// has an IdType Identifier
121+ public protocol Relatable : JSONTyped {
123122 associatedtype Identifier : JSONAPI . IdType
124- associatedtype WrappedIdentifier : Codable , Equatable
125123}
126- public typealias OptionalRelatable = WrappedRelatable
127124
128- /// The Relatable protocol describes anything that
129- /// has an IdType Identifier
130- public protocol Relatable : WrappedRelatable { }
125+ /// OptionalRelatable just describes an Optional
126+ /// with a Reltable Wrapped type.
127+ public protocol OptionalRelatable : JSONTyped {
128+ associatedtype Wrapped : JSONAPI . Relatable
129+ associatedtype WrappedId : WrappedIdType where WrappedId. Identifier == Wrapped . Identifier
130+ }
131131
132- extension Optional : OptionalRelatable where Wrapped: Relatable {
133- public typealias Description = Wrapped . Description
134- public typealias Identifier = Wrapped . Identifier
135- public typealias WrappedIdentifier = Identifier ?
132+ public protocol WrappedIdType : Codable , Equatable {
133+ associatedtype Identifier : JSONAPI . IdType
134+ }
135+
136+ extension Optional : WrappedIdType where Wrapped: IdType {
137+ public typealias Identifier = Wrapped
138+ }
139+
140+ extension Optional : OptionalRelatable , JSONTyped where Wrapped: JSONAPI . Relatable {
141+ public typealias WrappedId = Wrapped . Identifier ?
142+
143+ public static var type : String { return Wrapped . type }
136144}
137145
138146// MARK: Codable
@@ -146,14 +154,6 @@ private enum ResourceIdentifierCodingKeys: String, CodingKey {
146154 case entityType = " type "
147155}
148156
149- public enum JSONAPIEncodingError : Swift . Error {
150- case typeMismatch( expected: String , found: String )
151- case illegalEncoding( String )
152- case illegalDecoding( String )
153- case missingOrMalformedMetadata
154- case missingOrMalformedLinks
155- }
156-
157157extension ToOneRelationship {
158158 public init ( from decoder: Decoder ) throws {
159159 let container = try decoder. container ( keyedBy: ResourceLinkageCodingKeys . self)
@@ -177,7 +177,7 @@ extension ToOneRelationship {
177177 // type at which point we can store nil in `id`.
178178 let anyNil : Any ? = nil
179179 if try container. decodeNil ( forKey: . data) ,
180- let val = anyNil as? Relatable . WrappedIdentifier {
180+ let val = anyNil as? OptionalRelatable . WrappedId {
181181 id = val
182182 return
183183 }
@@ -186,11 +186,11 @@ extension ToOneRelationship {
186186
187187 let type = try identifier. decode ( String . self, forKey: . entityType)
188188
189- guard type == Relatable . Description . type else {
190- throw JSONAPIEncodingError . typeMismatch ( expected: Relatable . Description . type, found: type)
189+ guard type == OptionalRelatable . type else {
190+ throw JSONAPIEncodingError . typeMismatch ( expected: OptionalRelatable . type, found: type)
191191 }
192192
193- id = try identifier. decode ( Relatable . WrappedIdentifier . self, forKey: . id)
193+ id = try identifier. decode ( OptionalRelatable . WrappedId . self, forKey: . id)
194194 }
195195
196196 public func encode( to encoder: Encoder ) throws {
@@ -211,7 +211,7 @@ extension ToOneRelationship {
211211 var identifier = container. nestedContainer ( keyedBy: ResourceIdentifierCodingKeys . self, forKey: . data)
212212
213213 try identifier. encode ( id, forKey: . id)
214- try identifier. encode ( Relatable . Description . type, forKey: . entityType)
214+ try identifier. encode ( OptionalRelatable . type, forKey: . entityType)
215215 }
216216}
217217
@@ -239,8 +239,8 @@ extension ToManyRelationship {
239239
240240 let type = try identifier. decode ( String . self, forKey: . entityType)
241241
242- guard type == Relatable . Description . type else {
243- throw JSONAPIEncodingError . typeMismatch ( expected: Relatable . Description . type, found: type)
242+ guard type == Relatable . type else {
243+ throw JSONAPIEncodingError . typeMismatch ( expected: Relatable . type, found: type)
244244 }
245245
246246 newIds. append ( try identifier. decode ( Relatable . Identifier. self, forKey: . id) )
@@ -265,7 +265,7 @@ extension ToManyRelationship {
265265 var identifier = identifiers. nestedContainer ( keyedBy: ResourceIdentifierCodingKeys . self)
266266
267267 try identifier. encode ( id, forKey: . id)
268- try identifier. encode ( Relatable . Description . type, forKey: . entityType)
268+ try identifier. encode ( Relatable . type, forKey: . entityType)
269269 }
270270 }
271271}
0 commit comments