@@ -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 < OptionalRelatable : JSONAPI . OptionalRelatable , MetaType: JSONAPI . Meta , LinksType: JSONAPI . Links > : RelationshipType , Equatable {
20+ public struct ToOneRelationship < Identifiable : JSONAPI . Identifiable , MetaType: JSONAPI . Meta , LinksType: JSONAPI . Links > : RelationshipType , Equatable {
2121
22- public let id : OptionalRelatable . WrappedId
22+ public let id : Identifiable . Identifier
2323
2424 public let meta : MetaType
2525 public let links : LinksType
2626
27- public init ( id: OptionalRelatable . WrappedId , meta: MetaType , links: LinksType ) {
27+ public init ( id: Identifiable . Identifier , 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: OptionalRelatable . WrappedId ) {
35+ public init ( id: Identifiable . Identifier ) {
3636 self . init ( id: id, meta: . none, links: . none)
3737 }
3838}
3939
4040extension ToOneRelationship {
41- public init < E: EntityType > ( entity: E , meta: MetaType , links: LinksType ) where E. Id == OptionalRelatable . WrappedId {
41+ public init < E: EntityType > ( entity: E , meta: MetaType , links: LinksType ) where E. Id == Identifiable . Identifier {
4242 self . init ( id: entity. id, meta: meta, links: links)
4343 }
4444}
4545
4646extension ToOneRelationship where MetaType == NoMetadata , LinksType == NoLinks {
47- public init < E: EntityType > ( entity: E ) where E. Id == OptionalRelatable . WrappedId {
47+ public init < E: EntityType > ( entity: E ) where E. Id == Identifiable . Identifier {
4848 self . init ( id: entity. id, meta: . none, links: . none)
4949 }
5050}
5151
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 {
52+ extension ToOneRelationship where Identifiable : OptionalRelatable {
53+ public init < E: EntityType > ( entity: E ? , meta: MetaType , links: LinksType ) where E. Id == Identifiable . Wrapped . Identifier {
5454 self . init ( id: entity? . id, meta: meta, links: links)
5555 }
5656}
5757
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 {
58+ extension ToOneRelationship where Identifiable : OptionalRelatable , MetaType == NoMetadata , LinksType == NoLinks {
59+ public init < E: EntityType > ( entity: E ? ) where E. Id == Identifiable . Wrapped . Identifier {
6060 self . init ( id: entity? . id, meta: . none, links: . none)
6161 }
6262}
@@ -78,7 +78,7 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI
7878 self . links = links
7979 }
8080
81- public init < T: JSONAPI . OptionalRelatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] , meta: MetaType , links: LinksType ) where T. WrappedId == Relatable . Identifier {
81+ public init < T: JSONAPI . Identifiable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] , meta: MetaType , links: LinksType ) where T. Identifier == Relatable . Identifier {
8282 ids = pointers. map { $0. id }
8383 self . meta = meta
8484 self . links = links
@@ -103,7 +103,7 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
103103 self . init ( ids: ids, meta: . none, links: . none)
104104 }
105105
106- public init < T: JSONAPI . OptionalRelatable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] ) where T. WrappedId == Relatable . Identifier {
106+ public init < T: JSONAPI . Identifiable > ( pointers: [ ToOneRelationship < T , NoMetadata , NoLinks > ] ) where T. Identifier == Relatable . Identifier {
107107 self . init ( pointers: pointers, meta: . none, links: . none)
108108 }
109109
@@ -116,29 +116,23 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
116116 }
117117}
118118
119+ public protocol Identifiable : JSONTyped {
120+ associatedtype Identifier : Equatable , Codable
121+ }
122+
119123/// The Relatable protocol describes anything that
120124/// has an IdType Identifier
121- public protocol Relatable : JSONTyped {
122- associatedtype Identifier : JSONAPI . IdType
125+ public protocol Relatable : Identifiable where Identifier: JSONAPI . IdType {
123126}
124127
125128/// OptionalRelatable just describes an Optional
126129/// with a Reltable Wrapped type.
127- public protocol OptionalRelatable : JSONTyped {
130+ public protocol OptionalRelatable : Identifiable where Identifier == Wrapped . Identifier ? {
128131 associatedtype Wrapped : JSONAPI . Relatable
129- associatedtype WrappedId : WrappedIdType where WrappedId. Identifier == Wrapped . Identifier
130- }
131-
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
138132}
139133
140- extension Optional : OptionalRelatable , JSONTyped where Wrapped: JSONAPI . Relatable {
141- public typealias WrappedId = Wrapped . Identifier ?
134+ extension Optional : Identifiable , OptionalRelatable , JSONTyped where Wrapped: JSONAPI . Relatable {
135+ public typealias Identifier = Wrapped . Identifier ?
142136
143137 public static var type : String { return Wrapped . type }
144138}
@@ -177,7 +171,7 @@ extension ToOneRelationship {
177171 // type at which point we can store nil in `id`.
178172 let anyNil : Any ? = nil
179173 if try container. decodeNil ( forKey: . data) ,
180- let val = anyNil as? OptionalRelatable . WrappedId {
174+ let val = anyNil as? Identifiable . Identifier {
181175 id = val
182176 return
183177 }
@@ -186,11 +180,11 @@ extension ToOneRelationship {
186180
187181 let type = try identifier. decode ( String . self, forKey: . entityType)
188182
189- guard type == OptionalRelatable . type else {
190- throw JSONAPIEncodingError . typeMismatch ( expected: OptionalRelatable . type, found: type)
183+ guard type == Identifiable . type else {
184+ throw JSONAPIEncodingError . typeMismatch ( expected: Identifiable . type, found: type)
191185 }
192186
193- id = try identifier. decode ( OptionalRelatable . WrappedId . self, forKey: . id)
187+ id = try identifier. decode ( Identifiable . Identifier . self, forKey: . id)
194188 }
195189
196190 public func encode( to encoder: Encoder ) throws {
@@ -211,7 +205,7 @@ extension ToOneRelationship {
211205 var identifier = container. nestedContainer ( keyedBy: ResourceIdentifierCodingKeys . self, forKey: . data)
212206
213207 try identifier. encode ( id, forKey: . id)
214- try identifier. encode ( OptionalRelatable . type, forKey: . entityType)
208+ try identifier. encode ( Identifiable . type, forKey: . entityType)
215209 }
216210}
217211
0 commit comments