Skip to content

Commit 754255b

Browse files
committed
rename ResourceObject.Id to ResourceObject.ID so there is only one typealias for a ResourceObject identifying type.
1 parent 3c8bdca commit 754255b

12 files changed

Lines changed: 44 additions & 45 deletions

File tree

JSONAPI.playground/Pages/Full Client & Server Example.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typealias UnidentifiedJSONEntity<Description: ResourceObjectDescription> = JSONA
3030
// Create relationship typealiases because we do not expect
3131
// JSON:API Relationships for this particular API to have
3232
// Metadata or Links associated with them.
33-
typealias ToOneRelationship<Entity: Identifiable> = JSONAPI.ToOneRelationship<Entity, NoMetadata, NoLinks>
33+
typealias ToOneRelationship<Entity: JSONAPIIdentifiable> = JSONAPI.ToOneRelationship<Entity, NoMetadata, NoLinks>
3434
typealias ToManyRelationship<Entity: Relatable> = JSONAPI.ToManyRelationship<Entity, NoMetadata, NoLinks>
3535

3636
// Create a typealias for a Document because we do not expect
@@ -86,7 +86,7 @@ typealias SingleArticleDocument = Document<SingleResourceBody<Article>, NoInclud
8686
func articleDocument(includeAuthor: Bool) -> Either<SingleArticleDocument, SingleArticleDocumentWithIncludes> {
8787
// Let's pretend all of this is coming from a database:
8888

89-
let authorId = Author.Identifier(rawValue: "1234")
89+
let authorId = Author.ID(rawValue: "1234")
9090

9191
let article = Article(id: .init(rawValue: "5678"),
9292
attributes: .init(title: .init(value: "JSON:API in Swift"),

JSONAPI.playground/Pages/Full Document Verbose Generation.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ enum ArticleDocumentError: String, JSONAPIError, Codable {
129129
typealias SingleArticleDocument = JSONAPI.Document<SingleResourceBody<Article>, DocumentMetadata, SingleArticleDocumentLinks, Include1<Author>, APIDescription<APIDescriptionMetadata>, ArticleDocumentError>
130130

131131
// MARK: - Instantiations
132-
let authorId1 = Author.Identifier()
133-
let authorId2 = Author.Identifier()
134-
let authorId3 = Author.Identifier()
132+
let authorId1 = Author.ID()
133+
let authorId2 = Author.ID()
134+
let authorId3 = Author.ID()
135135

136136
let now = Date()
137137
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: now)!
@@ -155,7 +155,7 @@ let author1Links = EntityLinks(selfLink: .init(url: URL(string: "https://article
155155
meta: .init(expiry: tomorrow)))
156156
let author1 = Author(id: authorId1,
157157
attributes: .init(name: .init(value: "James Kinney")),
158-
relationships: .init(articles: .init(ids: [article.id, Article.Identifier(), Article.Identifier()],
158+
relationships: .init(articles: .init(ids: [article.id, Article.ID(), Article.ID()],
159159
meta: .init(pagination: .init(total: 3,
160160
limit: 50,
161161
offset: 0)),
@@ -167,7 +167,7 @@ let author2Links = EntityLinks(selfLink: .init(url: URL(string: "https://article
167167
meta: .init(expiry: tomorrow)))
168168
let author2 = Author(id: authorId2,
169169
attributes: .init(name: .init(value: "James Kinney")),
170-
relationships: .init(articles: .init(ids: [article.id, Article.Identifier()],
170+
relationships: .init(articles: .init(ids: [article.id, Article.ID()],
171171
meta: .init(pagination: .init(total: 2,
172172
limit: 50,
173173
offset: 0)),

JSONAPI.playground/Pages/PATCHing.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ print("Received dog with owner: \(dog3 ~> \.owner)")
108108

109109
// give the dog an owner
110110
let changedDog3 = dog3.replacingRelationships { _ in
111-
return .init(owner: .init(id: Id(rawValue: "1")))
111+
return .init(owner: .init(id: ID(rawValue: "1")))
112112
}
113113

114114
// create a document to be used as a request body for a PATCH request

JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ let singleDogData = try! JSONEncoder().encode(singleDogDocument)
2020
// MARK: - Parse a request or response body with one Dog in it
2121
let dogResponse = try! JSONDecoder().decode(SingleDogDocument.self, from: singleDogData)
2222
let dogFromData = dogResponse.body.primaryResource?.value
23-
let dogOwner: Person.Identifier? = dogFromData.flatMap { $0 ~> \.owner }
23+
let dogOwner: Person.ID? = dogFromData.flatMap { $0 ~> \.owner }
2424

2525

2626
// MARK: - Parse a request or response body with one Dog in it using an alternative model
2727
typealias AltSingleDogDocument = JSONAPI.Document<SingleResourceBody<AlternativeDog>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, BasicJSONAPIError<String>>
2828
let altDogResponse = try! JSONDecoder().decode(AltSingleDogDocument.self, from: singleDogData)
2929
let altDogFromData = altDogResponse.body.primaryResource?.value
30-
let altDogHuman: Person.Identifier? = altDogFromData.flatMap { $0 ~> \.human }
30+
let altDogHuman: Person.ID? = altDogFromData.flatMap { $0 ~> \.human }
3131

3232

3333
// MARK: - Create a request or response with multiple people and dogs and houses included
34-
let personIds = [Person.Identifier(), Person.Identifier()]
34+
let personIds = [Person.ID(), Person.ID()]
3535
let dogs = try! [Dog(name: "Buddy", owner: personIds[0]), Dog(name: "Joy", owner: personIds[0]), Dog(name: "Travis", owner: personIds[1])]
3636
let houses = [House(attributes: .none, relationships: .none, meta: .none, links: .none), House(attributes: .none, relationships: .none, meta: .none, links: .none)]
3737
let people = try! [Person(id: personIds[0], name: ["Gary", "Doe"], favoriteColor: "Orange-Red", friends: [], dogs: [dogs[0], dogs[1]], home: houses[0]), Person(id: personIds[1], name: ["Elise", "Joy"], favoriteColor: "Red", friends: [], dogs: [dogs[2]], home: houses[1])]

JSONAPI.playground/Sources/Entities.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension String: CreatableRawIdType {
2525

2626
// MARK: - typealiases for convenience
2727
public typealias ExampleEntity<Description: ResourceObjectDescription> = ResourceObject<Description, NoMetadata, NoLinks, String>
28-
public typealias ToOne<E: Identifiable> = ToOneRelationship<E, NoMetadata, NoLinks>
28+
public typealias ToOne<E: JSONAPIIdentifiable> = ToOneRelationship<E, NoMetadata, NoLinks>
2929
public typealias ToMany<E: Relatable> = ToManyRelationship<E, NoMetadata, NoLinks>
3030

3131
// MARK: - A few resource objects (entities)
@@ -63,8 +63,8 @@ public enum PersonDescription: ResourceObjectDescription {
6363
public typealias Person = ExampleEntity<PersonDescription>
6464

6565
public extension ResourceObject where Description == PersonDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String {
66-
init(id: Person.Id? = nil,name: [String], favoriteColor: String, friends: [Person], dogs: [Dog], home: House) throws {
67-
self = Person(id: id ?? Person.Id(), attributes: .init(name: .init(value: name), favoriteColor: .init(value: favoriteColor)), relationships: .init(friends: .init(resourceObjects: friends), dogs: .init(resourceObjects: dogs), home: .init(resourceObject: home)), meta: .none, links: .none)
66+
init(id: Person.ID? = nil,name: [String], favoriteColor: String, friends: [Person], dogs: [Dog], home: House) throws {
67+
self = Person(id: id ?? Person.ID(), attributes: .init(name: .init(value: name), favoriteColor: .init(value: favoriteColor)), relationships: .init(friends: .init(resourceObjects: friends), dogs: .init(resourceObjects: dogs), home: .init(resourceObject: home)), meta: .none, links: .none)
6868
}
6969
}
7070

@@ -147,7 +147,7 @@ public extension ResourceObject where Description == DogDescription, MetaType ==
147147
self = Dog(attributes: .init(name: .init(value: name)), relationships: DogDescription.Relationships(owner: .init(resourceObject: owner)), meta: .none, links: .none)
148148
}
149149

150-
init(name: String, owner: Person.Id) throws {
150+
init(name: String, owner: Person.ID) throws {
151151
self = Dog(attributes: .init(name: .init(value: name)), relationships: .init(owner: .init(id: owner)), meta: .none, links: .none)
152152
}
153153
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ typealias UnidentifiedJSONEntity<Description: ResourceObjectDescription> = JSONA
163163
// Create relationship typealiases because we do not expect
164164
// JSON:API Relationships for this particular API to have
165165
// Metadata or Links associated with them.
166-
typealias ToOneRelationship<Entity: Identifiable> = JSONAPI.ToOneRelationship<Entity, NoMetadata, NoLinks>
166+
typealias ToOneRelationship<Entity: JSONAPIIdentifiable> = JSONAPI.ToOneRelationship<Entity, NoMetadata, NoLinks>
167167
typealias ToManyRelationship<Entity: Relatable> = JSONAPI.ToManyRelationship<Entity, NoMetadata, NoLinks>
168168

169169
// Create a typealias for a Document because we do not expect
@@ -220,7 +220,7 @@ typealias SingleArticleDocument = Document<SingleResourceBody<Article>, NoInclud
220220
func articleDocument(includeAuthor: Bool) -> Either<SingleArticleDocument, SingleArticleDocumentWithIncludes> {
221221
// Let's pretend all of this is coming from a database:
222222

223-
let authorId = Author.Identifier(rawValue: "1234")
223+
let authorId = Author.ID(rawValue: "1234")
224224

225225
let article = Article(id: .init(rawValue: "5678"),
226226
attributes: .init(title: .init(value: "JSON:API in Swift"),

Sources/JSONAPI/Resource/Relationship.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ extension ToOneRelationship where MetaType == NoMetadata, LinksType == NoLinks {
5656
}
5757

5858
extension ToOneRelationship {
59-
public init<T: ResourceObjectType>(resourceObject: T, meta: MetaType, links: LinksType) where T.Id == Identifiable.ID {
59+
public init<T: ResourceObjectType>(resourceObject: T, meta: MetaType, links: LinksType) where T.ID == Identifiable.ID {
6060
self.init(id: resourceObject.id, meta: meta, links: links)
6161
}
6262
}
6363

6464
extension ToOneRelationship where MetaType == NoMetadata, LinksType == NoLinks {
65-
public init<T: ResourceObjectType>(resourceObject: T) where T.Id == Identifiable.ID {
65+
public init<T: ResourceObjectType>(resourceObject: T) where T.ID == Identifiable.ID {
6666
self.init(id: resourceObject.id, meta: .none, links: .none)
6767
}
6868
}
6969

7070
extension ToOneRelationship where Identifiable: OptionalRelatable {
71-
public init<T: ResourceObjectType>(resourceObject: T?, meta: MetaType, links: LinksType) where T.Id == Identifiable.Wrapped.ID {
71+
public init<T: ResourceObjectType>(resourceObject: T?, meta: MetaType, links: LinksType) where T.ID == Identifiable.Wrapped.ID {
7272
self.init(id: resourceObject?.id, meta: meta, links: links)
7373
}
7474
}
7575

7676
extension ToOneRelationship where Identifiable: OptionalRelatable, MetaType == NoMetadata, LinksType == NoLinks {
77-
public init<T: ResourceObjectType>(resourceObject: T?) where T.Id == Identifiable.Wrapped.ID {
77+
public init<T: ResourceObjectType>(resourceObject: T?) where T.ID == Identifiable.Wrapped.ID {
7878
self.init(id: resourceObject?.id, meta: .none, links: .none)
7979
}
8080
}
@@ -102,7 +102,7 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI
102102
self.links = links
103103
}
104104

105-
public init<T: ResourceObjectType>(resourceObjects: [T], meta: MetaType, links: LinksType) where T.Id == Relatable.ID {
105+
public init<T: ResourceObjectType>(resourceObjects: [T], meta: MetaType, links: LinksType) where T.ID == Relatable.ID {
106106
self.init(ids: resourceObjects.map(\.id), meta: meta, links: links)
107107
}
108108

@@ -129,7 +129,7 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
129129
return .none(withMeta: .none, links: .none)
130130
}
131131

132-
public init<T: ResourceObjectType>(resourceObjects: [T]) where T.Id == Relatable.ID {
132+
public init<T: ResourceObjectType>(resourceObjects: [T]) where T.ID == Relatable.ID {
133133
self.init(resourceObjects: resourceObjects, meta: .none, links: .none)
134134
}
135135
}

Sources/JSONAPI/Resource/Resource Object/ResourceObject.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public protocol ResourceObjectProxy: Equatable, JSONTyped {
7373
associatedtype Description: ResourceObjectProxyDescription
7474
associatedtype EntityRawIdType: JSONAPI.MaybeRawId
7575

76-
typealias Id = JSONAPI.Id<EntityRawIdType, Self>
76+
typealias ID = JSONAPI.Id<EntityRawIdType, Self>
7777

7878
typealias Attributes = Description.Attributes
7979
typealias Relationships = Description.Relationships
@@ -82,7 +82,7 @@ public protocol ResourceObjectProxy: Equatable, JSONTyped {
8282
/// the entity is being created clientside and the
8383
/// server is being asked to create a unique Id. Otherwise,
8484
/// this should be of a type conforming to `IdType`.
85-
var id: Id { get }
85+
var id: ID { get }
8686

8787
/// The JSON API compliant attributes of this `Entity`.
8888
var attributes: Attributes { get }
@@ -121,14 +121,15 @@ public protocol IdentifiableResourceObjectType: ResourceObjectType, Relatable wh
121121
/// See https://jsonapi.org/format/#document-resource-objects
122122
public struct ResourceObject<Description: JSONAPI.ResourceObjectDescription, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, EntityRawIdType: JSONAPI.MaybeRawId>: ResourceObjectType {
123123

124+
public typealias ID = JSONAPI.Id<EntityRawIdType, Self>
124125
public typealias Meta = MetaType
125126
public typealias Links = LinksType
126127

127128
/// The `ResourceObject`'s Id. This can be of type `Unidentified` if
128129
/// the entity is being created clientside and the
129130
/// server is being asked to create a unique Id. Otherwise,
130131
/// this should be of a type conforming to `IdType`.
131-
public let id: ResourceObject.Id
132+
public let id: ID
132133

133134
/// The JSON API compliant attributes of this `ResourceObject`.
134135
public let attributes: Description.Attributes
@@ -142,7 +143,7 @@ public struct ResourceObject<Description: JSONAPI.ResourceObjectDescription, Met
142143
/// Links related to the entity.
143144
public let links: LinksType
144145

145-
public init(id: ResourceObject.Id, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
146+
public init(id: ID, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
146147
self.id = id
147148
self.attributes = attributes
148149
self.relationships = relationships
@@ -163,9 +164,7 @@ extension ResourceObject: Hashable where EntityRawIdType: RawIdType {
163164
}
164165
}
165166

166-
extension ResourceObject: JSONAPIIdentifiable, IdentifiableResourceObjectType, Relatable where EntityRawIdType: JSONAPI.RawIdType {
167-
public typealias ID = ResourceObject.Id
168-
}
167+
extension ResourceObject: JSONAPIIdentifiable, IdentifiableResourceObjectType, Relatable where EntityRawIdType: JSONAPI.RawIdType {}
169168

170169
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
171170
extension ResourceObject: Swift.Identifiable where EntityRawIdType: JSONAPI.RawIdType {}
@@ -179,7 +178,7 @@ extension ResourceObject: CustomStringConvertible {
179178
// MARK: - Convenience initializers
180179
extension ResourceObject where EntityRawIdType: CreatableRawIdType {
181180
public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
182-
self.id = ResourceObject.Id()
181+
self.id = ResourceObject.ID()
183182
self.attributes = attributes
184183
self.relationships = relationships
185184
self.meta = meta
@@ -410,7 +409,7 @@ public extension ResourceObject {
410409
}
411410

412411
let maybeUnidentified = Unidentified() as? EntityRawIdType
413-
id = try maybeUnidentified.map { ResourceObject.Id(rawValue: $0) } ?? container.decode(ResourceObject.Id.self, forKey: .id)
412+
id = try maybeUnidentified.map { ResourceObject.ID(rawValue: $0) } ?? container.decode(ResourceObject.ID.self, forKey: .id)
414413

415414
do {
416415
attributes = try (NoAttributes() as? Description.Attributes)

Tests/JSONAPITests/Document/DocumentCompoundResourceTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ final class DocumentCompoundResourceTests: XCTestCase {
114114
)
115115

116116
let ids = [
117-
DocumentTests.Book.Id(),
118-
DocumentTests.Book.Id(),
119-
DocumentTests.Book.Id()
117+
DocumentTests.Book.ID(),
118+
DocumentTests.Book.ID(),
119+
DocumentTests.Book.ID()
120120
]
121121

122122
let book = DocumentTests.Book(

Tests/JSONAPITests/Poly/PolyProxyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ extension Poly2: ResourceObjectProxy, JSONTyped where A == PolyProxyTests.UserA,
111111
public var id: Id<EntityRawIdType, PolyProxyTests.User> {
112112
switch self {
113113
case .a(let a):
114-
return Id(rawValue: a.id.rawValue)
114+
return ID(rawValue: a.id.rawValue)
115115
case .b(let b):
116-
return Id(rawValue: b.id.rawValue)
116+
return ID(rawValue: b.id.rawValue)
117117
}
118118
}
119119

0 commit comments

Comments
 (0)