From 4f43c25872fbcbda4b33c5aa1f0ef1b7ffdab8e0 Mon Sep 17 00:00:00 2001 From: Jairon Terrero Date: Fri, 24 Oct 2025 11:35:35 -0600 Subject: [PATCH 1/2] feat: Add PowerSyncDataConvertible protocol This enables downstream consumers to define how certain types should be mapped to those that are supported by the PowerSync Kotlin Multiplatform SDK. --- .../Kotlin/db/KotlinConnectionContext.swift | 7 +++- .../db/PowerSyncDataTypeConvertible.swift | 32 +++++++++++++++++++ .../KotlinPowerSyncDatabaseImplTests.swift | 27 ++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift diff --git a/Sources/PowerSync/Kotlin/db/KotlinConnectionContext.swift b/Sources/PowerSync/Kotlin/db/KotlinConnectionContext.swift index 0b7f314..c280a26 100644 --- a/Sources/PowerSync/Kotlin/db/KotlinConnectionContext.swift +++ b/Sources/PowerSync/Kotlin/db/KotlinConnectionContext.swift @@ -97,6 +97,11 @@ final class KotlinTransactionContext: Transaction, KotlinConnectionContextProtoc // Allows nil values to be passed to the Kotlin [Any] params func mapParameters(_ parameters: [Any?]?) -> [Any] { parameters?.map { item in - item ?? NSNull() + switch item { + case .none: NSNull() + case let item as PowerSyncDataTypeConvertible: + item.psDataType?.unwrap() ?? NSNull() + default: item + } } ?? [] } diff --git a/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift b/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift new file mode 100644 index 0000000..9b0f204 --- /dev/null +++ b/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift @@ -0,0 +1,32 @@ +import struct Foundation.Data + +/// Represents the set of types that are supported +/// by the PowerSync Kotlin Multiplatform SDK +public enum PowerSyncDataType { + case bool(Bool) + case string(String) + case int64(Int64) + case int32(Int32) + case double(Double) + case data(Data) +} + +/// Types conforming to this protocol will be +/// mapped to the specified ``PowerSyncDataType`` +/// before use by the PowerSync Kotlin Multiplatform SDK +public protocol PowerSyncDataTypeConvertible { + var psDataType: PowerSyncDataType? { get } +} + +extension PowerSyncDataType { + func unwrap() -> Any { + switch self { + case let .bool(bool): bool + case let .string(string): string + case let .int32(int32): int32 + case let .int64(int64): int64 + case let .double(double): double + case let .data(data): data + } + } +} \ No newline at end of file diff --git a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift index 0c3b48e..d5cf888 100644 --- a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift +++ b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift @@ -1,4 +1,5 @@ @testable import PowerSync +import struct Foundation.UUID import XCTest final class KotlinPowerSyncDatabaseImplTests: XCTestCase { @@ -156,6 +157,25 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { } } + func testCustomDataTypeConvertible() async throws { + let uuid = UUID() + try await database.execute( + sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", + parameters: [uuid, "Test User", "test@example.com"] + ) + + let _ = try await database.getOptional( + sql: "SELECT id, name, email FROM users WHERE id = ?", + parameters: [uuid] + ) { cursor throws in + try ( + cursor.getString(name: "id"), + cursor.getString(name: "name"), + cursor.getString(name: "email") + ) + } + } + func testGetAll() async throws { try await database.execute( sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?), (?, ?, ?)", @@ -625,3 +645,10 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { XCTAssertEqual(result[1], JoinOutput(name: "Test User", description: "task 2", comment: "comment 2")) } } + + +extension UUID: @retroactive PowerSyncDataTypeConvertible { + public var psDataType: PowerSyncDataType? { + .string(uuidString) + } +} \ No newline at end of file From 8b7665d1c224051637669773e5f4c3714b91f1ea Mon Sep 17 00:00:00 2001 From: stevensJourney <51082125+stevensJourney@users.noreply.github.com> Date: Thu, 6 Nov 2025 02:56:45 -0700 Subject: [PATCH 2/2] Update Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift --- Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift b/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift index 9b0f204..bfe6d05 100644 --- a/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift +++ b/Sources/PowerSync/Kotlin/db/PowerSyncDataTypeConvertible.swift @@ -13,7 +13,7 @@ public enum PowerSyncDataType { /// Types conforming to this protocol will be /// mapped to the specified ``PowerSyncDataType`` -/// before use by the PowerSync Kotlin Multiplatform SDK +/// before use by SQLite public protocol PowerSyncDataTypeConvertible { var psDataType: PowerSyncDataType? { get } }