Skip to content

Instantly share code, notes, and snippets.

@mattpolzin
Created February 29, 2024 23:17
Show Gist options
  • Save mattpolzin/49373b24a58b545d4ade5b0f6225212e to your computer and use it in GitHub Desktop.
Save mattpolzin/49373b24a58b545d4ade5b0f6225212e to your computer and use it in GitHub Desktop.
attempted_repro
// The Swift Programming Language
// https://docs.swift.org/swift-book
import JSONAPI
import JSONAPIResourceCache
import Foundation
extension Include13: CacheableResource where
A: CacheableResource,
B: CacheableResource,
C: CacheableResource,
D: CacheableResource,
E: CacheableResource,
F: CacheableResource,
G: CacheableResource,
H: CacheableResource,
I: CacheableResource,
J: CacheableResource,
K: CacheableResource,
L: CacheableResource,
M: CacheableResource,
A.Cache == B.Cache,
B.Cache == C.Cache,
C.Cache == D.Cache,
D.Cache == E.Cache,
E.Cache == F.Cache,
F.Cache == G.Cache,
G.Cache == H.Cache,
H.Cache == I.Cache,
I.Cache == J.Cache,
J.Cache == K.Cache,
K.Cache == L.Cache,
L.Cache == M.Cache {
public func cache(in cache: inout A.Cache) {
switch self {
case .a(let resource):
resource.cache(in: &cache)
case .b(let resource):
resource.cache(in: &cache)
case .c(let resource):
resource.cache(in: &cache)
case .d(let resource):
resource.cache(in: &cache)
case .e(let resource):
resource.cache(in: &cache)
case .f(let resource):
resource.cache(in: &cache)
case .g(let resource):
resource.cache(in: &cache)
case .h(let resource):
resource.cache(in: &cache)
case .i(let resource):
resource.cache(in: &cache)
case .j(let resource):
resource.cache(in: &cache)
case .k(let resource):
resource.cache(in: &cache)
case .l(let resource):
resource.cache(in: &cache)
case .m(let resource):
resource.cache(in: &cache)
}
}
}
struct EntryDescription: JSONAPI.ResourceObjectDescription {
static var jsonType: String { "entry" }
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
}
struct TypeDescription: JSONAPI.ResourceObjectDescription {
static var jsonType: String { "type" }
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
}
typealias EntryResource = JSONAPI.ResourceObject<EntryDescription, NoMetadata, NoLinks, String>
typealias TypeResource = JSONAPI.ResourceObject<TypeDescription, NoMetadata, NoLinks, String>
struct EntryCache: Equatable, ResourceCache {
var entries: ResourceHash<EntryResource> = [:] // primary resource
var types: ResourceHash<TypeResource> = [:] // include
mutating func merge(_ other: EntryCache) {
entries.merge(other.entries, uniquingKeysWith: { $1 })
types.merge(other.types, uniquingKeysWith: { $1 })
}
}
extension TypeDescription: Materializable {
static var cachePath: WritableKeyPath<EntryCache, ResourceHash<TypeResource>> { \.types }
}
extension EntryDescription: Materializable {
static var cachePath: WritableKeyPath<EntryCache, ResourceHash<EntryResource>> { \.entries }
}
// we don't need to actually decode the data to reproduce a compiler error.
let mockData =
"""
""".data(using: .utf8)!
typealias Doc = JSONAPI.Document<ManyResourceBody<EntryResource>, NoMetadata, NoLinks, Include13<TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource,TypeResource>, NoAPIDescription, UnknownJSONAPIError>
let doc: Doc = try! JSONDecoder().decode(Doc.self, from: mockData)
let resources = doc.resourceCache()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment