Skip to content

Commit ac2dcee

Browse files
committed
Add convenience constructors from success document types that craete non-optional caches.
1 parent 18ca197 commit ac2dcee

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

Sources/JSONAPIResourceCache/ResourceCache.swift

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,76 @@ extension EncodableJSONAPIDocument where
182182
return cache
183183
}
184184
}
185+
186+
// MARK: Success Documents
187+
// success documents can return non-optional caches
188+
189+
extension Document.SuccessDocument where
190+
BodyData.PrimaryResourceBody: SingleResourceBodyProtocol,
191+
BodyData.PrimaryResourceBody.PrimaryResource: CacheableResource,
192+
BodyData.IncludeType: CacheableResource,
193+
BodyData.PrimaryResourceBody.PrimaryResource.Cache == BodyData.IncludeType.Cache {
194+
public func resourceCache() -> BodyData.IncludeType.Cache {
195+
196+
var cache = BodyData.IncludeType.Cache()
197+
198+
data.primary.value.cache(in: &cache)
199+
200+
for include in data.includes.values {
201+
include.cache(in: &cache)
202+
}
203+
204+
return cache
205+
}
206+
}
207+
208+
extension Document.SuccessDocument where
209+
BodyData.PrimaryResourceBody: ManyResourceBodyProtocol,
210+
BodyData.PrimaryResourceBody.PrimaryResource: CacheableResource,
211+
BodyData.IncludeType: CacheableResource,
212+
BodyData.PrimaryResourceBody.PrimaryResource.Cache == BodyData.IncludeType.Cache {
213+
public func resourceCache() -> BodyData.IncludeType.Cache {
214+
215+
var cache = BodyData.IncludeType.Cache()
216+
217+
for resource in data.primary.values {
218+
resource.cache(in: &cache)
219+
}
220+
221+
for include in data.includes.values {
222+
include.cache(in: &cache)
223+
}
224+
225+
return cache
226+
}
227+
}
228+
229+
extension Document.SuccessDocument where
230+
BodyData.PrimaryResourceBody: SingleResourceBodyProtocol,
231+
BodyData.PrimaryResourceBody.PrimaryResource: CacheableResource,
232+
BodyData.IncludeType == NoIncludes {
233+
public func resourceCache() -> BodyData.PrimaryResourceBody.PrimaryResource.Cache {
234+
235+
var cache = BodyData.PrimaryResourceBody.PrimaryResource.Cache()
236+
237+
data.primary.value.cache(in: &cache)
238+
239+
return cache
240+
}
241+
}
242+
243+
extension Document.SuccessDocument where
244+
BodyData.PrimaryResourceBody: ManyResourceBodyProtocol,
245+
BodyData.PrimaryResourceBody.PrimaryResource: CacheableResource,
246+
BodyData.IncludeType == NoIncludes {
247+
public func resourceCache() -> BodyData.PrimaryResourceBody.PrimaryResource.Cache {
248+
249+
var cache = BodyData.PrimaryResourceBody.PrimaryResource.Cache()
250+
251+
for resource in data.primary.values {
252+
resource.cache(in: &cache)
253+
}
254+
255+
return cache
256+
}
257+
}

Tests/JSONAPIResourceCacheTests/ResourceCacheTests.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,82 @@ final class ResourceCacheTests: XCTestCase {
154154
XCTAssertEqual((firstPrimaryResource ~> \.type1).materialize(from: cache!), type1)
155155
XCTAssertEqual((firstPrimaryResource ~> \.type3).materialize(from: cache!), type3)
156156
}
157+
158+
func test_successDocumentCreation() {
159+
let t1Id = "1234"
160+
let t2Id = "5678"
161+
let t2Id2 = "1323221"
162+
let t3Id = "910112"
163+
164+
let date = Date()
165+
166+
let type3 = Type3(
167+
id: .init(rawValue: t3Id),
168+
attributes: .init(int: 1234),
169+
relationships: .init(type2: .init(id: .init(rawValue: t2Id))),
170+
meta: .none,
171+
links: .none
172+
)
173+
174+
let type2s = [
175+
Type2(
176+
id: .init(rawValue: t2Id),
177+
attributes: .init(string: "hello world"),
178+
relationships: .init(
179+
type1: .init(id: .init(rawValue: t1Id)),
180+
type3: .init(id: .init(rawValue: t3Id))
181+
),
182+
meta: .none,
183+
links: .none
184+
),
185+
Type2(
186+
id: .init(rawValue: t2Id2),
187+
attributes: .init(string: "hi"),
188+
relationships: .init(
189+
type1: .init(id: .init(rawValue: t1Id)),
190+
type3: .init(id: .init(rawValue: t3Id))
191+
),
192+
meta: .none,
193+
links: .none
194+
)
195+
]
196+
197+
let type1 = Type1(
198+
id: .init(rawValue: t1Id),
199+
attributes: .init(createdAt: .init(value: date)),
200+
relationships: .init(
201+
type2: .init(id: .init(rawValue: t2Id)),
202+
type4: .init(ids: [])
203+
),
204+
meta: .none,
205+
links: .none
206+
)
207+
208+
let document = JSONAPI.Document<
209+
ManyResourceBody<Type2>,
210+
NoMetadata,
211+
NoLinks,
212+
Include2<Type1, Type3>,
213+
NoAPIDescription,
214+
UnknownJSONAPIError>.SuccessDocument(
215+
apiDescription: .none,
216+
body: .init(resourceObjects: type2s),
217+
includes: .init(values: [.init(type1), .init(type3)]),
218+
meta: .none,
219+
links: .none
220+
)
221+
222+
let cache = document.resourceCache()
223+
224+
XCTAssertEqual(cache[type1.id], type1)
225+
XCTAssertEqual(cache[type2s[0].id], type2s[0])
226+
XCTAssertEqual(cache[type2s[1].id], type2s[1])
227+
XCTAssertEqual(cache[type3.id], type3)
228+
229+
let firstPrimaryResource = document.body.primaryResource!.values.first!
230+
XCTAssertEqual((firstPrimaryResource ~> \.type1).materialize(from: cache), type1)
231+
XCTAssertEqual((firstPrimaryResource ~> \.type3).materialize(from: cache), type3)
232+
}
157233
}
158234

159235
// MARK: - Test Types

0 commit comments

Comments
 (0)