diff --git a/Sources/SwiftletModel/Model/EntityModelProtocol.swift b/Sources/SwiftletModel/Model/EntityModelProtocol.swift index f133080..5e9a03b 100644 --- a/Sources/SwiftletModel/Model/EntityModelProtocol.swift +++ b/Sources/SwiftletModel/Model/EntityModelProtocol.swift @@ -9,7 +9,7 @@ import Foundation public protocol EntityModelProtocol { // swiftlint:disable:next type_name - associatedtype ID: Hashable, Codable, LosslessStringConvertible + associatedtype ID: Hashable, LosslessStringConvertible var id: ID { get } diff --git a/Sources/SwiftletModel/Relationship/Relation+Codable.swift b/Sources/SwiftletModel/Relationship/Relation+Codable.swift index 1de0391..ef4a683 100644 --- a/Sources/SwiftletModel/Relationship/Relation+Codable.swift +++ b/Sources/SwiftletModel/Relationship/Relation+Codable.swift @@ -9,7 +9,7 @@ import Foundation // MARK: - Codable -extension Relation: Codable where Entity: Codable { +extension Relation: Codable where Entity: Codable, Entity.ID: Codable { public func encode(to encoder: any Encoder) throws { switch encoder.relationEncodingStrategy { case .plain: @@ -42,7 +42,7 @@ extension Relation: Codable where Entity: Codable { // MARK: - Codable Explicitly -extension Relation where Entity: Codable { +extension Relation where Entity: Codable, Entity.ID: Codable { enum RelationCodingKeys: String, CodingKey { case id = "id" case entity = "object" @@ -99,7 +99,7 @@ extension Relation where Entity: Codable { // MARK: - Codable Exactly -extension Relation where Entity: Codable { +extension Relation where Entity: Codable, Entity.ID: Codable { enum RelationExplicitCodingKeys: String, CodingKey { case id = "id" case entity = "object" @@ -169,21 +169,25 @@ extension Relation where Entity: Codable { // MARK: - Codable Flattaned -extension Relation where Entity: Codable { +extension Relation.ID: Codable where Entity.ID: Codable { + +} + +extension Relation where Entity: Codable, Entity.ID: Codable { // swiftlint:disable:next type_name - struct ID: Codable { - let id: T.ID + struct ID { + let id: Entity.ID } func encodePlainContainer(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch state { case .id(let value): - try container.encode(value.map { ID(id: $0) }) + try container.encode(value.map { ID(id: $0) }) case .entity(let value, _): try container.encode(value) case .ids(let value, _): - try container.encode(value.map { ID(id: $0) }) + try container.encode(value.map { ID(id: $0) }) case .entities(let value, _, _): try container.encode(value) case .none: @@ -200,7 +204,7 @@ extension Relation where Entity: Codable { return Relation(state: .entity(entity: value, fragment: false)) } - if let value = try? container.decode(ID?.self) { + if let value = try? container.decode(ID?.self) { return Relation(state: .id(id: value.id)) } @@ -208,7 +212,7 @@ extension Relation where Entity: Codable { return Relation(state: .entities(entities: value, slice: false, fragment: false)) } - if let value = try? container.decode([ID].self) { + if let value = try? container.decode([ID].self) { return Relation(state: .ids(ids: value.map { $0.id }, slice: false)) } diff --git a/Sources/SwiftletModel/Relationship/Relation.swift b/Sources/SwiftletModel/Relationship/Relation.swift index d0566fd..786b0fd 100644 --- a/Sources/SwiftletModel/Relationship/Relation.swift +++ b/Sources/SwiftletModel/Relationship/Relation.swift @@ -106,7 +106,7 @@ extension Relation { // MARK: - Codable -extension Relation.State: Codable where T: Codable { } +extension Relation.State: Codable where T: Codable, T.ID: Codable { } // MARK: - Sendable diff --git a/Sources/SwiftletModel/Relationship/Relationship.swift b/Sources/SwiftletModel/Relationship/Relationship.swift index 3ba7fa5..5f45cf0 100644 --- a/Sources/SwiftletModel/Relationship/Relationship.swift +++ b/Sources/SwiftletModel/Relationship/Relationship.swift @@ -97,7 +97,7 @@ public extension Relationship where Directionality == Relations.OneWay, // MARK: - Codable -extension Relationship: Codable where Value: Codable, Entity: Codable { +extension Relationship: Codable where Value: Codable, Entity: Codable, Entity.ID: Codable { public func encode(to encoder: Encoder) throws { try relation.encode(to: encoder)