Skip to content

Commit 5b0a016

Browse files
authored
Merge pull request #72 from mattpolzin/change-course-slightly
Change course slightly
2 parents 0c48ddf + e9b9dbc commit 5b0a016

File tree

12 files changed

+58
-45
lines changed

12 files changed

+58
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.ID(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.ID()
133-
let authorId2 = Author.ID()
134-
let authorId3 = Author.ID()
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.ID(), Article.ID()],
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.ID()],
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/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.ID? = 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.ID? = 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.ID(), Person.ID()]
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -151,7 +151,7 @@ extension String: CreatableRawIdType {
151151

152152
// Create a typealias because we do not expect JSON:API Resource
153153
// Objects for this particular API to have Metadata or Links associated
154-
// with them. We also expect them to have String Identifiers.
154+
// with them. We also expect them to have String Ids.
155155
typealias JSONEntity<Description: ResourceObjectDescription> = JSONAPI.ResourceObject<Description, NoMetadata, NoLinks, String>
156156

157157
// Similarly, create a typealias for unidentified entities. JSON:API
@@ -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.ID(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: 8 additions & 8 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
}
@@ -96,13 +96,13 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI
9696
self.links = links
9797
}
9898

99-
public init<T: JSONAPI.JSONAPIIdentifiable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>], meta: MetaType, links: LinksType) where T.ID == Relatable.ID {
99+
public init<T: JSONAPIIdentifiable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>], meta: MetaType, links: LinksType) where T.ID == Relatable.ID {
100100
ids = pointers.map(\.id)
101101
self.meta = meta
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

@@ -121,15 +121,15 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
121121
self.init(ids: ids, meta: .none, links: .none)
122122
}
123123

124-
public init<T: JSONAPI.JSONAPIIdentifiable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>]) where T.ID == Relatable.ID {
124+
public init<T: JSONAPIIdentifiable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>]) where T.ID == Relatable.ID {
125125
self.init(pointers: pointers, meta: .none, links: .none)
126126
}
127127

128128
public static var none: ToManyRelationship {
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ public protocol ResourceObjectProxy: Equatable, JSONTyped {
7373
associatedtype Description: ResourceObjectProxyDescription
7474
associatedtype EntityRawIdType: JSONAPI.MaybeRawId
7575

76+
typealias Id = JSONAPI.Id<EntityRawIdType, Self>
77+
7678
typealias Attributes = Description.Attributes
7779
typealias Relationships = Description.Relationships
7880

7981
/// The `Entity`'s Id. This can be of type `Unidentified` if
8082
/// the entity is being created clientside and the
8183
/// server is being asked to create a unique Id. Otherwise,
8284
/// this should be of a type conforming to `IdType`.
83-
var id: JSONAPI.Id<EntityRawIdType, Self> { get }
85+
var id: Id { get }
8486

8587
/// The JSON API compliant attributes of this `Entity`.
8688
var attributes: Attributes { get }
@@ -89,10 +91,6 @@ public protocol ResourceObjectProxy: Equatable, JSONTyped {
8991
var relationships: Relationships { get }
9092
}
9193

92-
extension ResourceObjectProxy {
93-
public typealias ID = JSONAPI.Id<EntityRawIdType, Self>
94-
}
95-
9694
extension ResourceObjectProxy {
9795
/// The JSON API compliant "type" of this `ResourceObject`.
9896
public static var jsonType: String { return Description.jsonType }
@@ -130,7 +128,7 @@ public struct ResourceObject<Description: JSONAPI.ResourceObjectDescription, Met
130128
/// the entity is being created clientside and the
131129
/// server is being asked to create a unique Id. Otherwise,
132130
/// this should be of a type conforming to `IdType`.
133-
public let id: ID
131+
public let id: ResourceObject.Id
134132

135133
/// The JSON API compliant attributes of this `ResourceObject`.
136134
public let attributes: Description.Attributes
@@ -144,7 +142,7 @@ public struct ResourceObject<Description: JSONAPI.ResourceObjectDescription, Met
144142
/// Links related to the entity.
145143
public let links: LinksType
146144

147-
public init(id: ID, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
145+
public init(id: ResourceObject.Id, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
148146
self.id = id
149147
self.attributes = attributes
150148
self.relationships = relationships
@@ -165,7 +163,9 @@ extension ResourceObject: Hashable where EntityRawIdType: RawIdType {
165163
}
166164
}
167165

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

170170
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
171171
extension ResourceObject: Swift.Identifiable where EntityRawIdType: JSONAPI.RawIdType {}
@@ -179,7 +179,7 @@ extension ResourceObject: CustomStringConvertible {
179179
// MARK: - Convenience initializers
180180
extension ResourceObject where EntityRawIdType: CreatableRawIdType {
181181
public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
182-
self.id = ResourceObject.ID()
182+
self.id = ResourceObject.Id()
183183
self.attributes = attributes
184184
self.relationships = relationships
185185
self.meta = meta
@@ -410,7 +410,7 @@ public extension ResourceObject {
410410
}
411411

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

415415
do {
416416
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

Tests/JSONAPITests/ResourceObject/ResourceObjectTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class ResourceObjectTests: XCTestCase {
9292
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(meta: .init(meta: .init(x: "hello", y: 5), links: .none), optionalMeta: nil, one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(resourceObject: entity1, meta: .none, links: .none), optionalMany: nil), meta: .none, links: .none)
9393
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(meta: .init(meta: .init(x: "hello", y: 5), links: .none), optionalMeta: nil, one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(resourceObject: entity1, meta: .none, links: .none), optionalMany: .init(resourceObjects: [], meta: .none, links: .none)), meta: .none, links: .none)
9494
let e10id1 = TestEntity10.ID(rawValue: "hello")
95-
let e10id2 = TestEntity10.ID(rawValue: "world")
96-
let e10id3: TestEntity10.ID = "!"
95+
let e10id2 = TestEntity10.Id(rawValue: "world")
96+
let e10id3: TestEntity10.Id = "!"
9797
let _ = TestEntity10(id: .init(rawValue: "10"), attributes: .none, relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])), meta: .none, links: .none)
9898
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11)), relationships: .none, meta: .none, links: .none))
9999
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")), relationships: .none, meta: .none, links: .none)

0 commit comments

Comments
 (0)