Added encoding support to Includes and tests

This commit is contained in:
Mathew Polzin
2018-11-16 23:15:25 -08:00
parent 9802efd917
commit 04bd0421cd
5 changed files with 185 additions and 8 deletions
+102 -2
View File
@@ -7,9 +7,9 @@
import Result
public protocol IncludeDecoder: Decodable {}
public protocol IncludeDecoder: Codable, Equatable {}
public struct Includes<I: IncludeDecoder>: Decodable {
public struct Includes<I: IncludeDecoder>: Codable, Equatable {
public static var none: Includes { return .init(values: []) }
let values: [I]
@@ -34,6 +34,18 @@ public struct Includes<I: IncludeDecoder>: Decodable {
values = valueAggregator
}
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
guard I.self != NoIncludes.self else {
throw JSONAPIEncodingError.illegalEncoding("Attempting to encode Include0, which should be represented by the absense of an 'included' entry altogether.")
}
for value in values {
try container.encode(value)
}
}
public var count: Int {
return values.count
@@ -64,6 +76,10 @@ public struct Include0: _Include0 {
public init(from decoder: Decoder) throws {
}
public func encode(to encoder: Encoder) throws {
throw JSONAPIEncodingError.illegalEncoding("Attempted to encode Include0, which should be represented by the absence of an 'included' entry altogether.")
}
}
public typealias NoIncludes = Include0
@@ -85,6 +101,15 @@ public enum Include1<A: EntityType>: _Include1 {
self = .a(try container.decode(A.self))
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
}
}
}
extension Includes where I: _Include1 {
@@ -129,6 +154,17 @@ public enum Include2<A: EntityType, B: EntityType>: _Include2 {
self = val
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
}
}
}
extension Includes where I: _Include2 {
@@ -180,6 +216,19 @@ public enum Include3<A: EntityType, B: EntityType, C: EntityType>: _Include3 {
self = val
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
case .c(let c):
try container.encode(c)
}
}
}
extension Includes where I: _Include3 {
@@ -238,6 +287,21 @@ public enum Include4<A: EntityType, B: EntityType, C: EntityType, D: EntityType>
self = val
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
case .c(let c):
try container.encode(c)
case .d(let d):
try container.encode(d)
}
}
}
extension Includes where I: _Include4 {
@@ -303,6 +367,23 @@ public enum Include5<A: EntityType, B: EntityType, C: EntityType, D: EntityType,
self = val
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
case .c(let c):
try container.encode(c)
case .d(let d):
try container.encode(d)
case .e(let e):
try container.encode(e)
}
}
}
extension Includes where I: _Include5 {
@@ -375,6 +456,25 @@ public enum Include6<A: EntityType, B: EntityType, C: EntityType, D: EntityType,
self = val
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .a(let a):
try container.encode(a)
case .b(let b):
try container.encode(b)
case .c(let c):
try container.encode(c)
case .d(let d):
try container.encode(d)
case .e(let e):
try container.encode(e)
case .f(let f):
try container.encode(f)
}
}
}
extension Includes where I: _Include6 {
@@ -91,6 +91,7 @@ private enum ResourceIdentifierCodingKeys: String, CodingKey {
public enum JSONAPIEncodingError: Swift.Error {
case typeMismatch(expected: String, found: String)
case illegalEncoding(String)
}
extension ToOneRelationship {