From c794df6970553d70f88bea4bc859f3308581925a Mon Sep 17 00:00:00 2001 From: Alexey Demin Date: Sat, 31 Jul 2021 13:40:30 -0700 Subject: [PATCH] Support up to 32 output tuple elements. Tests for array to tuple conversion. --- Sources/SQLiteCombine/SQLiteCombine.swift | 24 ++++++++++++++++++- .../SQLiteCombineTests.swift | 12 ++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Sources/SQLiteCombine/SQLiteCombine.swift b/Sources/SQLiteCombine/SQLiteCombine.swift index 0d83af4..3ee9fc0 100644 --- a/Sources/SQLiteCombine/SQLiteCombine.swift +++ b/Sources/SQLiteCombine/SQLiteCombine.swift @@ -340,7 +340,7 @@ extension Result where Success == SQLite.DBResult, Failure == SQLite.DBError { -extension Array { +public extension Array { func tuple() -> Any? { switch count { @@ -355,6 +355,28 @@ extension Array { case 8: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7]) case 9: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8]) case 10: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9]) + case 11: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10]) + case 12: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11]) + case 13: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12]) + case 14: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13]) + case 15: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14]) + case 16: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15]) + case 17: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16]) + case 18: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17]) + case 19: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18]) + case 20: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19]) + case 21: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20]) + case 22: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21]) + case 23: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22]) + case 24: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23]) + case 25: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24]) + case 26: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25]) + case 27: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26]) + case 28: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26], self[27]) + case 29: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26], self[27], self[28]) + case 30: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26], self[27], self[28], self[29]) + case 31: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26], self[27], self[28], self[29], self[30]) + case 32: return (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9], self[10], self[11], self[12], self[13], self[14], self[15], self[16], self[17], self[18], self[19], self[20], self[21], self[22], self[23], self[24], self[25], self[26], self[27], self[28], self[29], self[30], self[31]) default: return nil } } diff --git a/Tests/SQLiteCombineTests/SQLiteCombineTests.swift b/Tests/SQLiteCombineTests/SQLiteCombineTests.swift index 9465a5d..f38785d 100644 --- a/Tests/SQLiteCombineTests/SQLiteCombineTests.swift +++ b/Tests/SQLiteCombineTests/SQLiteCombineTests.swift @@ -399,6 +399,18 @@ final class SQLiteCombineTests: XCTestCase { waitForExpectations(timeout: 0, handler: nil) } + + + func testArray2Tuple() throws { + + XCTAssert([].tuple() as! () == ()) + XCTAssertEqual([1].tuple() as? Int, 1) + for i in 2...32 { + let array = Array(1...i) + XCTAssertEqual(array.tuple().map(Mirror.init)?.children.map(\.value) as? [Int], array) + } + XCTAssertNil(Array(1...33).tuple()) + } }