Add coding to family 5
This commit is contained in:
parent
6c5a1d29f7
commit
99710fd5e1
|
|
@ -56,6 +56,27 @@ public struct Requires5<A, B, C, D, E>: FamilyRequirementsManaging where A: Comp
|
|||
}
|
||||
}
|
||||
|
||||
extension Requires5: FamilyEncoding where A: Encodable, B: Encodable, C: Encodable, D: Encodable, E: Encodable {
|
||||
public static func encode(components: (A, B, C, D, E), into container: inout KeyedEncodingContainer<DynamicCodingKey>, using strategy: CodingStrategy) throws {
|
||||
try container.encode(components.0, forKey: strategy.codingKey(for: A.self))
|
||||
try container.encode(components.1, forKey: strategy.codingKey(for: B.self))
|
||||
try container.encode(components.2, forKey: strategy.codingKey(for: C.self))
|
||||
try container.encode(components.3, forKey: strategy.codingKey(for: D.self))
|
||||
try container.encode(components.4, forKey: strategy.codingKey(for: E.self))
|
||||
}
|
||||
}
|
||||
|
||||
extension Requires5: FamilyDecoding where A: Decodable, B: Decodable, C: Decodable, D: Decodable, E: Decodable {
|
||||
public static func decode(componentsIn container: KeyedDecodingContainer<DynamicCodingKey>, using strategy: CodingStrategy) throws -> (A, B, C, D, E) {
|
||||
let compA = try container.decode(A.self, forKey: strategy.codingKey(for: A.self))
|
||||
let compB = try container.decode(B.self, forKey: strategy.codingKey(for: B.self))
|
||||
let compC = try container.decode(C.self, forKey: strategy.codingKey(for: C.self))
|
||||
let compD = try container.decode(D.self, forKey: strategy.codingKey(for: D.self))
|
||||
let compE = try container.decode(E.self, forKey: strategy.codingKey(for: E.self))
|
||||
return Components(compA, compB, compC, compD, compE)
|
||||
}
|
||||
}
|
||||
|
||||
extension Nexus {
|
||||
// swiftlint:disable function_parameter_count
|
||||
public func family<A, B, C, D, E>(
|
||||
|
|
|
|||
|
|
@ -36,12 +36,14 @@ class Velocity: Component {
|
|||
}
|
||||
}
|
||||
|
||||
class Party: Component {
|
||||
final class Party: Component {
|
||||
var partying: Bool
|
||||
|
||||
init(partying: Bool) {
|
||||
self.partying = partying
|
||||
}
|
||||
}
|
||||
extension Party: Codable { }
|
||||
|
||||
final class Color: Component {
|
||||
var r: UInt8
|
||||
|
|
|
|||
|
|
@ -221,4 +221,125 @@ final class FamilyCodingTests: XCTestCase {
|
|||
try family.decodeMembers(from: jsonData, using: &jsonDecoder)
|
||||
XCTAssertEqual(family.count, 2)
|
||||
}
|
||||
|
||||
func testEncodeFamily5() throws {
|
||||
let nexus = Nexus()
|
||||
|
||||
let family = nexus.family(requiresAll: MyComponent.self, YourComponent.self, Position.self, Color.self, Party.self)
|
||||
family.createMember(with: (MyComponent(name: "My Name", flag: true), YourComponent(number: 1.23), Position(x: 1, y: 2), Color(r: 1, g: 2, b: 3), Party(partying: true)))
|
||||
family.createMember(with: (MyComponent(name: "Your Name", flag: false), YourComponent(number: 3.45), Position(x: 3, y: 4), Color(r: 4, g: 5, b: 6), Party(partying: false)))
|
||||
XCTAssertEqual(family.count, 2)
|
||||
|
||||
var jsonEncoder = JSONEncoder()
|
||||
let encodedData = try family.encodeMembers(using: &jsonEncoder)
|
||||
XCTAssertGreaterThanOrEqual(encodedData.count, 320)
|
||||
}
|
||||
|
||||
func testDecodeFamily5() throws {
|
||||
let jsonString = """
|
||||
[
|
||||
{
|
||||
"Color": {
|
||||
"r": 1,
|
||||
"g": 2,
|
||||
"b": 3
|
||||
},
|
||||
"Position": {
|
||||
"x": 1,
|
||||
"y": 2
|
||||
},
|
||||
"MyComponent": {
|
||||
"name": "My Name",
|
||||
"flag": true
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 1.23
|
||||
},
|
||||
"Party": {
|
||||
"partying": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"Color": {
|
||||
"r": 4,
|
||||
"g": 5,
|
||||
"b": 6
|
||||
},
|
||||
"Position": {
|
||||
"x": 3,
|
||||
"y": 4
|
||||
},
|
||||
"MyComponent": {
|
||||
"name": "Your Name",
|
||||
"flag": false
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 3.45
|
||||
},
|
||||
"Party": {
|
||||
"partying": false
|
||||
}
|
||||
}
|
||||
]
|
||||
"""
|
||||
|
||||
let jsonData = jsonString.data(using: .utf8)!
|
||||
|
||||
let nexus = Nexus()
|
||||
|
||||
let family = nexus.family(requiresAll: YourComponent.self, MyComponent.self, Position.self, Color.self, Party.self)
|
||||
XCTAssertTrue(family.isEmpty)
|
||||
var jsonDecoder = JSONDecoder()
|
||||
try family.decodeMembers(from: jsonData, using: &jsonDecoder)
|
||||
XCTAssertEqual(family.count, 2)
|
||||
}
|
||||
|
||||
func testFailDecodingFamily() {
|
||||
|
||||
let jsonString = """
|
||||
[
|
||||
{
|
||||
"Color": {
|
||||
"r": 1,
|
||||
"g": 2,
|
||||
"b": 3
|
||||
},
|
||||
"Position": {
|
||||
"x": 1,
|
||||
"y": 2
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 1.23
|
||||
},
|
||||
"Party": {
|
||||
"partying": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"Color": {
|
||||
"r": 4,
|
||||
"g": 5,
|
||||
"b": 6
|
||||
},
|
||||
"Position": {
|
||||
"x": 3,
|
||||
"y": 4
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 3.45
|
||||
},
|
||||
"Party": {
|
||||
"partying": false
|
||||
}
|
||||
}
|
||||
]
|
||||
"""
|
||||
let jsonData = jsonString.data(using: .utf8)!
|
||||
var jsonDecoder = JSONDecoder()
|
||||
|
||||
let nexus = Nexus()
|
||||
|
||||
let family = nexus.family(requiresAll: YourComponent.self, MyComponent.self)
|
||||
XCTAssertThrowsError(try family.decodeMembers(from: jsonData, using: &jsonDecoder))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,13 +40,16 @@ extension FamilyCodingTests {
|
|||
// to regenerate.
|
||||
static let __allTests__FamilyCodingTests = [
|
||||
("testDecodeFamily4", testDecodeFamily4),
|
||||
("testDecodeFamily5", testDecodeFamily5),
|
||||
("testDecodingFamily1", testDecodingFamily1),
|
||||
("testDecodingFamily2", testDecodingFamily2),
|
||||
("testDecodingFamily3", testDecodingFamily3),
|
||||
("testEncodeFamily2", testEncodeFamily2),
|
||||
("testEncodeFamily3", testEncodeFamily3),
|
||||
("testEncodeFamily4", testEncodeFamily4),
|
||||
("testEncodingFamily1", testEncodingFamily1)
|
||||
("testEncodeFamily5", testEncodeFamily5),
|
||||
("testEncodingFamily1", testEncodingFamily1),
|
||||
("testFailDecodingFamily", testFailDecodingFamily)
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue