@@ -16,11 +16,15 @@ public typealias Attributes = Codable & Equatable
1616
1717/// Can be used as `Relationships` Type for Entities that do not
1818/// have any Relationships.
19- public struct NoRelationships : Relationships { }
19+ public struct NoRelationships : Relationships {
20+ public static var none : NoRelationships { return . init( ) }
21+ }
2022
2123/// Can be used as `Attributes` Type for Entities that do not
2224/// have any Attributes.
23- public struct NoAttributes : Attributes { }
25+ public struct NoAttributes : Attributes {
26+ public static var none : NoAttributes { return . init( ) }
27+ }
2428
2529/// An `EntityDescription` describes a JSON API
2630/// Resource Object. The Resource Object
@@ -34,10 +38,10 @@ public protocol EntityDescription {
3438 static var type : String { get }
3539}
3640
37- /// EntityType is the protocol that Entity conforms to. This
38- /// protocol lets other types accept any Entity as a generic
39- /// specialization .
40- public protocol EntityType : PrimaryResource {
41+ /// EntityProxy is a protocol that can be used to create
42+ /// types that _act_ like Entities but cannot be encoded
43+ /// or decoded as Entities .
44+ public protocol EntityProxy : Equatable {
4145 associatedtype Description : EntityDescription
4246 associatedtype EntityRawIdType : JSONAPI . MaybeRawId
4347
@@ -59,17 +63,24 @@ public protocol EntityType: PrimaryResource {
5963 var relationships : Relationships { get }
6064}
6165
66+ extension EntityProxy {
67+ /// The JSON API compliant "type" of this `Entity`.
68+ public static var type : String { return Description . type }
69+ }
70+
71+ /// EntityType is the protocol that Entity conforms to. This
72+ /// protocol lets other types accept any Entity as a generic
73+ /// specialization.
74+ public protocol EntityType : EntityProxy , PrimaryResource {
75+ }
76+
6277public protocol IdentifiableEntityType : EntityType , Relatable where EntityRawIdType: JSONAPI . RawIdType { }
6378
6479/// An `Entity` is a single model type that can be
6580/// encoded to or decoded from a JSON API
6681/// "Resource Object."
6782/// See https://jsonapi.org/format/#document-resource-objects
6883public struct Entity < Description: JSONAPI . EntityDescription , EntityRawIdType: JSONAPI . MaybeRawId > : EntityType {
69-
70- /// The JSON API compliant "type" of this `Entity`.
71- public static var type : String { return Description . type }
72-
7384 /// The `Entity`'s Id. This can be of type `Unidentified` if
7485 /// the entity is being created clientside and the
7586 /// server is being asked to create a unique Id. Otherwise,
@@ -155,21 +166,21 @@ public extension Entity where EntityRawIdType: JSONAPI.RawIdType {
155166}
156167
157168// MARK: Attribute Access
158- public extension Entity {
169+ public extension EntityProxy {
159170 /// Access the attribute at the given keypath. This just
160171 /// allows you to write `entity[\.propertyName]` instead
161172 /// of `entity.relationships.propertyName`.
162173 subscript< T, TFRM: Transformer > ( _ path: KeyPath < Description . Attributes , TransformedAttribute < T , TFRM > > ) -> TFRM . To {
163174 return attributes [ keyPath: path] . value
164175 }
165-
176+
166177 /// Access the attribute at the given keypath. This just
167178 /// allows you to write `entity[\.propertyName]` instead
168179 /// of `entity.relationships.propertyName`.
169180 subscript< T, TFRM: Transformer > ( _ path: KeyPath < Description . Attributes , TransformedAttribute < T , TFRM > ? > ) -> TFRM . To ? {
170181 return attributes [ keyPath: path] ? . value
171182 }
172-
183+
173184 /// Access the attribute at the given keypath. This just
174185 /// allows you to write `entity[\.propertyName]` instead
175186 /// of `entity.relationships.propertyName`.
@@ -179,18 +190,18 @@ public extension Entity {
179190}
180191
181192// MARK: Relationship Access
182- public extension Entity {
193+ public extension EntityProxy {
183194 /// Access to an Id of a `ToOneRelationship`.
184195 /// This allows you to write `entity ~> \.other` instead
185196 /// of `entity.relationships.other.id`.
186- public static func ~> < OtherEntity: OptionalRelatable > ( entity: Entity , path: KeyPath < Description . Relationships , ToOneRelationship < OtherEntity > > ) -> OtherEntity . WrappedIdentifier {
197+ public static func ~> < OtherEntity: OptionalRelatable > ( entity: Self , path: KeyPath < Description . Relationships , ToOneRelationship < OtherEntity > > ) -> OtherEntity . WrappedIdentifier {
187198 return entity. relationships [ keyPath: path] . id
188199 }
189200
190201 /// Access to all Ids of a `ToManyRelationship`.
191202 /// This allows you to write `entity ~> \.others` instead
192203 /// of `entity.relationships.others.ids`.
193- public static func ~> < OtherEntity: Relatable > ( entity: Entity , path: KeyPath < Description . Relationships , ToManyRelationship < OtherEntity > > ) -> [ OtherEntity . Identifier ] {
204+ public static func ~> < OtherEntity: Relatable > ( entity: Self , path: KeyPath < Description . Relationships , ToManyRelationship < OtherEntity > > ) -> [ OtherEntity . Identifier ] {
194205 return entity. relationships [ keyPath: path] . ids
195206 }
196207}
0 commit comments