Skip to content

Commit ee6f6b0

Browse files
committed
Check boxed Kotlin type to map
1 parent bae0924 commit ee6f6b0

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

Sources/PowerSync/Kotlin/db/KotlinJsonParam.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,24 @@ extension JsonValue {
2626
return PowerSyncKotlin.JsonParam.Map(value: anyDict)
2727
}
2828
}
29+
30+
static func kotlinValueToJsonParam(raw: Any?) -> JsonValue {
31+
if let string = raw as? String {
32+
return Self.string(string)
33+
} else if let bool = raw as? KotlinBoolean {
34+
return Self.bool(bool.boolValue)
35+
} else if let int = raw as? KotlinInt {
36+
return Self.int(int.intValue)
37+
} else if let double = raw as? KotlinDouble {
38+
return Self.double(double.doubleValue)
39+
} else if let array = raw as? [Any?] {
40+
return Self.array(array.map(kotlinValueToJsonParam))
41+
} else if let object = raw as? [String: Any?] {
42+
return Self.object(object.mapValues(kotlinValueToJsonParam))
43+
} else {
44+
// fatalError is fine here because this function is internal, so this being reached
45+
// is an SDK bug.
46+
fatalError("fromValue must only be called on outputs of JsonValue.toValue()");
47+
}
48+
}
2949
}

Sources/PowerSync/Kotlin/sync/KotlinSyncStreams.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class KotlinStreamDescription<T: PowerSyncKotlin.SyncStreamDescription> {
1111
self.inner = inner
1212
self.name = inner.name
1313
self.kotlinParameters = inner.parameters
14-
self.parameters = inner.parameters?.mapValues { JsonValue.fromValue(raw: $0) }
14+
self.parameters = inner.parameters?.mapValues { JsonValue.kotlinValueToJsonParam(raw: $0) }
1515
}
1616
}
1717

@@ -104,7 +104,7 @@ func mapSyncStreamStatus(_ status: PowerSyncKotlin.SyncStreamStatus) -> SyncStre
104104
progress: progress,
105105
subscription: SyncSubscriptionDescription(
106106
name: subscription.name,
107-
parameters: subscription.parameters?.mapValues { JsonValue.fromValue(raw: $0) },
107+
parameters: subscription.parameters?.mapValues { JsonValue.kotlinValueToJsonParam(raw: $0) },
108108
active: subscription.active,
109109
isDefault: subscription.isDefault,
110110
hasExplicitSubscription: subscription.hasExplicitSubscription,

Sources/PowerSync/Protocol/db/JsonParam.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,6 @@ public enum JsonValue: Codable, Sendable, Equatable {
5050
return anyDict
5151
}
5252
}
53-
54-
/// Converts a raw Swift value into a ``JsonValue``.
55-
///
56-
/// The value must be one of the types returned by ``JsonValue/toValue()``.
57-
internal static func fromValue(raw: Any?) -> Self {
58-
if let string = raw as? String {
59-
return Self.string(string)
60-
} else if let bool = raw as? Bool {
61-
return Self.bool(bool)
62-
} else if let int = raw as? Int {
63-
return Self.int(int)
64-
} else if let double = raw as? Double {
65-
return Self.double(double)
66-
} else if let array = raw as? [Any?] {
67-
return Self.array(array.map(fromValue))
68-
} else if let object = raw as? [String: Any?] {
69-
return Self.object(object.mapValues(fromValue))
70-
} else {
71-
// fatalError is fine here because this function is internal, so this being reached
72-
// is an SDK bug.
73-
fatalError("fromValue must only be called on outputs of JsonValue.toValue()");
74-
}
75-
}
7653
}
7754

7855
/// A typealias representing a top-level JSON object with string keys and `JSONValue` values.

Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
741741

742742
let _ = try await database.syncStream(name: "foo", params: [
743743
"text": JsonValue.string("text"),
744-
"int": JsonValue.int(42),
744+
"int1": JsonValue.int(1),
745+
"int0": JsonValue.int(0),
745746
"double": JsonValue.double(1.23),
746747
"bool": JsonValue.bool(true),
747748
]).subscribe()
@@ -751,7 +752,8 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
751752
let stream = updatedStatus!.syncStreams![0]
752753
let params = stream.subscription.parameters!
753754
XCTAssertEqual(params["text"], JsonValue.string("text"))
754-
XCTAssertEqual(params["int"], JsonValue.int(42))
755+
XCTAssertEqual(params["int1"], JsonValue.int(1))
756+
XCTAssertEqual(params["int0"], JsonValue.int(0))
755757
XCTAssertEqual(params["double"], JsonValue.double(1.23))
756758
XCTAssertEqual(params["bool"], JsonValue.bool(true))
757759
}

0 commit comments

Comments
 (0)