Add coding to family 3
This commit is contained in:
parent
6cc1ba3c68
commit
da94c055f8
|
|
@ -46,6 +46,23 @@ public struct Requires3<A, B, C>: FamilyRequirementsManaging where A: Component,
|
|||
}
|
||||
}
|
||||
|
||||
extension Requires3: FamilyEncoding where A: Encodable, B: Encodable, C: Encodable {
|
||||
public static func encode(components: (A, B, C), 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))
|
||||
}
|
||||
}
|
||||
|
||||
extension Requires3: FamilyDecoding where A: Decodable, B: Decodable, C: Decodable {
|
||||
public static func decode(componentsIn container: KeyedDecodingContainer<DynamicCodingKey>, using strategy: CodingStrategy) throws -> (A, B, C) {
|
||||
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))
|
||||
return Components(compA, compB, compC)
|
||||
}
|
||||
}
|
||||
|
||||
extension Nexus {
|
||||
public func family<A, B, C>(
|
||||
requiresAll componentA: A.Type,
|
||||
|
|
|
|||
|
|
@ -18,14 +18,16 @@ class Name: Component {
|
|||
}
|
||||
}
|
||||
|
||||
class Position: Component {
|
||||
final class Position: Component {
|
||||
var x: Int
|
||||
var y: Int
|
||||
|
||||
init(x: Int, y: Int) {
|
||||
self.x = x
|
||||
self.y = y
|
||||
}
|
||||
}
|
||||
extension Position: Codable { }
|
||||
|
||||
class Velocity: Component {
|
||||
var a: Float
|
||||
|
|
|
|||
|
|
@ -99,4 +99,60 @@ final class FamilyCodingTests: XCTestCase {
|
|||
try family.decodeMembers(from: jsonData, using: &jsonDecoder)
|
||||
XCTAssertEqual(family.count, 2)
|
||||
}
|
||||
|
||||
func testEncodeFamily3() throws {
|
||||
let nexus = Nexus()
|
||||
|
||||
let family = nexus.family(requiresAll: MyComponent.self, YourComponent.self, Position.self)
|
||||
family.createMember(with: (MyComponent(name: "My Name", flag: true), YourComponent(number: 1.23), Position(x: 1, y: 2)))
|
||||
family.createMember(with: (MyComponent(name: "Your Name", flag: false), YourComponent(number: 3.45), Position(x: 3, y: 4)))
|
||||
XCTAssertEqual(family.count, 2)
|
||||
|
||||
var jsonEncoder = JSONEncoder()
|
||||
let encodedData = try family.encodeMembers(using: &jsonEncoder)
|
||||
XCTAssertGreaterThanOrEqual(encodedData.count, 200)
|
||||
}
|
||||
|
||||
func testDecodingFamily3() throws {
|
||||
let jsonString = """
|
||||
[
|
||||
{
|
||||
"MyComponent": {
|
||||
"name": "My Name",
|
||||
"flag": true
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 1.23
|
||||
},
|
||||
"Position": {
|
||||
"x": 1,
|
||||
"y": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"MyComponent": {
|
||||
"name": "Your Name",
|
||||
"flag": false
|
||||
},
|
||||
"YourComponent": {
|
||||
"number": 3.45
|
||||
},
|
||||
"Position": {
|
||||
"x": 3,
|
||||
"y": 4
|
||||
}
|
||||
}
|
||||
]
|
||||
"""
|
||||
|
||||
let jsonData = jsonString.data(using: .utf8)!
|
||||
|
||||
let nexus = Nexus()
|
||||
|
||||
let family = nexus.family(requiresAll: YourComponent.self, MyComponent.self, Position.self)
|
||||
XCTAssertTrue(family.isEmpty)
|
||||
var jsonDecoder = JSONDecoder()
|
||||
try family.decodeMembers(from: jsonData, using: &jsonDecoder)
|
||||
XCTAssertEqual(family.count, 2)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,9 @@ extension FamilyCodingTests {
|
|||
static let __allTests__FamilyCodingTests = [
|
||||
("testDecodingFamily1", testDecodingFamily1),
|
||||
("testDecodingFamily2", testDecodingFamily2),
|
||||
("testDecodingFamily3", testDecodingFamily3),
|
||||
("testEncodeFamily2", testEncodeFamily2),
|
||||
("testEncodeFamily3", testEncodeFamily3),
|
||||
("testEncodingFamily1", testEncodingFamily1)
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue