Skip to content

Commit ce2ad71

Browse files
Make initialValue a string
There isn't a spec PR for this yet, but it's needed in order to implement the write spec [1]. Asked about it in [2]. It's implemented in JS in [3]. Got Cursor to do this. [1] ably/specification#353 [2] https://github.com/ably/specification/pull/353/files#r2228017382 [3] ably/ably-js#2065
1 parent 2887d35 commit ce2ad71

5 files changed

Lines changed: 9 additions & 126 deletions

File tree

Sources/AblyLiveObjects/Protocol/ObjectMessage.swift

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ internal struct ObjectOperation {
3939
internal var map: ObjectsMap? // OOP3e
4040
internal var counter: WireObjectsCounter? // OOP3f
4141
internal var nonce: String? // OOP3g
42-
internal var initialValue: Data? // OOP3h
43-
internal var initialValueEncoding: String? // OOP3i
42+
internal var initialValue: String? // OOP3h
4443
}
4544

4645
internal struct ObjectData {
@@ -157,50 +156,22 @@ internal extension ObjectOperation {
157156
nonce = nil
158157
// Do not access on inbound data, per OOP3h
159158
initialValue = nil
160-
// Do not access on inbound data, per OOP3i
161-
initialValueEncoding = nil
162159
}
163160

164-
/// Converts this `ObjectOperation` to a `WireObjectOperation`, applying the data encoding rules of OD4 and OOP5.
161+
/// Converts this `ObjectOperation` to a `WireObjectOperation`, applying the data encoding rules of OD4.
165162
///
166163
/// - Parameters:
167-
/// - format: The format to use when applying the encoding rules of OD4 and OOP5.
164+
/// - format: The format to use when applying the encoding rules of OD4.
168165
func toWire(format: AblyPlugin.EncodingFormat) -> WireObjectOperation {
169-
// OOP5: Encode initialValue based on format
170-
let wireInitialValue: StringOrData?
171-
let wireInitialValueEncoding: String?
172-
173-
if let initialValue {
174-
switch format {
175-
case .messagePack:
176-
// OOP5a: When the MessagePack protocol is used
177-
// OOP5a1: A binary ObjectOperation.initialValue is encoded as a MessagePack binary type
178-
wireInitialValue = .data(initialValue)
179-
// OOP5a2: Set ObjectOperation.initialValueEncoding to msgpack
180-
wireInitialValueEncoding = "msgpack"
181-
182-
case .json:
183-
// OOP5b: When the JSON protocol is used
184-
// OOP5b1: A binary ObjectOperation.initialValue is Base64-encoded and represented as a JSON string
185-
wireInitialValue = .string(initialValue.base64EncodedString())
186-
// OOP5b2: Set ObjectOperation.initialValueEncoding to json
187-
wireInitialValueEncoding = "json"
188-
}
189-
} else {
190-
wireInitialValue = nil
191-
wireInitialValueEncoding = nil
192-
}
193-
194-
return .init(
166+
.init(
195167
action: action,
196168
objectId: objectId,
197169
mapOp: mapOp?.toWire(format: format),
198170
counterOp: counterOp,
199171
map: map?.toWire(format: format),
200172
counter: counter,
201173
nonce: nonce,
202-
initialValue: wireInitialValue,
203-
initialValueEncoding: wireInitialValueEncoding,
174+
initialValue: initialValue,
204175
)
205176
}
206177
}

Sources/AblyLiveObjects/Protocol/WireObjectMessage.swift

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ internal struct WireObjectOperation {
165165
internal var map: WireObjectsMap? // OOP3e
166166
internal var counter: WireObjectsCounter? // OOP3f
167167
internal var nonce: String? // OOP3g
168-
internal var initialValue: StringOrData? // OOP3h
169-
internal var initialValueEncoding: String? // OOP3i
168+
internal var initialValue: String? // OOP3h
170169
}
171170

172171
extension WireObjectOperation: WireObjectCodable {
@@ -179,7 +178,6 @@ extension WireObjectOperation: WireObjectCodable {
179178
case counter
180179
case nonce
181180
case initialValue
182-
case initialValueEncoding
183181
}
184182

185183
internal init(wireObject: [String: WireValue]) throws(InternalError) {
@@ -194,8 +192,6 @@ extension WireObjectOperation: WireObjectCodable {
194192
nonce = nil
195193
// Do not access on inbound data, per OOP3h
196194
initialValue = nil
197-
// Do not access on inbound data, per OOP3i
198-
initialValueEncoding = nil
199195
}
200196

201197
internal var toWireObject: [String: WireValue] {
@@ -220,10 +216,7 @@ extension WireObjectOperation: WireObjectCodable {
220216
result[WireKey.nonce.rawValue] = .string(nonce)
221217
}
222218
if let initialValue {
223-
result[WireKey.initialValue.rawValue] = initialValue.toWireValue
224-
}
225-
if let initialValueEncoding {
226-
result[WireKey.initialValueEncoding.rawValue] = .string(initialValueEncoding)
219+
result[WireKey.initialValue.rawValue] = .string(initialValue)
227220
}
228221

229222
return result
@@ -486,10 +479,7 @@ extension WireObjectData: WireObjectCodable {
486479

487480
/// A type that can be either a string or binary data.
488481
///
489-
/// Used to represent:
490-
///
491-
/// - the values that `WireObjectData.bytes` might hold, after being encoded per OD4 or before being decoded per OD5
492-
/// - the values that `WireObjectOperation.initialValue` might hold, after being encoded per OOP5
482+
/// Used to represent the values that `WireObjectData.bytes` might hold, after being encoded per OD4 or before being decoded per OD5.
493483
internal enum StringOrData: WireCodable {
494484
case string(String)
495485
case data(Data)

Tests/AblyLiveObjectsTests/Helpers/TestFactories.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ struct TestFactories {
341341
map: ObjectsMap? = nil,
342342
counter: WireObjectsCounter? = nil,
343343
nonce: String? = nil,
344-
initialValue: Data? = nil,
345-
initialValueEncoding: String? = nil,
344+
initialValue: String? = nil,
346345
) -> ObjectOperation {
347346
ObjectOperation(
348347
action: action,
@@ -353,7 +352,6 @@ struct TestFactories {
353352
counter: counter,
354353
nonce: nonce,
355354
initialValue: initialValue,
356-
initialValueEncoding: initialValueEncoding,
357355
)
358356
}
359357

Tests/AblyLiveObjectsTests/ObjectMessageTests.swift

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -498,71 +498,4 @@ struct ObjectMessageTests {
498498
}
499499
}
500500
}
501-
502-
struct ObjectOperationTests {
503-
struct EncodingTests {
504-
@Test(arguments: [EncodingFormat.json.rawValue, EncodingFormat.messagePack.rawValue])
505-
func noInitialValue(formatRawValue: EncodingFormat.RawValue) throws {
506-
let format = try #require(EncodingFormat(rawValue: formatRawValue))
507-
let operation = ObjectOperation(
508-
action: .known(.mapSet),
509-
objectId: "test-id",
510-
)
511-
let wireOperation = operation.toWire(format: format)
512-
513-
#expect(wireOperation.initialValue == nil)
514-
#expect(wireOperation.initialValueEncoding == nil)
515-
}
516-
517-
struct MessagePackTests {
518-
// @spec OOP5a1
519-
// @spec OOP5a2
520-
@Test
521-
func initialValue() {
522-
let testData = Data([1, 2, 3, 4])
523-
let operation = ObjectOperation(
524-
action: .known(.mapSet),
525-
objectId: "test-id",
526-
initialValue: testData,
527-
)
528-
let wireOperation = operation.toWire(format: .messagePack)
529-
530-
// OOP5a1: A binary ObjectOperation.initialValue is encoded as a MessagePack binary type
531-
switch wireOperation.initialValue {
532-
case let .data(data):
533-
#expect(data == testData)
534-
default:
535-
Issue.record("Expected .data case")
536-
}
537-
// OOP5a2: Set ObjectOperation.initialValueEncoding to msgpack
538-
#expect(wireOperation.initialValueEncoding == "msgpack")
539-
}
540-
}
541-
542-
struct JSONTests {
543-
// @spec OOP5b1
544-
// @spec OOP5b2
545-
@Test
546-
func initialValue() {
547-
let testData = Data([1, 2, 3, 4])
548-
let operation = ObjectOperation(
549-
action: .known(.mapSet),
550-
objectId: "test-id",
551-
initialValue: testData,
552-
)
553-
let wireOperation = operation.toWire(format: .json)
554-
555-
// OOP5b1: A binary ObjectOperation.initialValue is Base64-encoded and represented as a JSON string
556-
switch wireOperation.initialValue {
557-
case let .string(base64String):
558-
#expect(base64String == testData.base64EncodedString())
559-
default:
560-
Issue.record("Expected .string case")
561-
}
562-
// OOP5b2: Set ObjectOperation.initialValueEncoding to json
563-
#expect(wireOperation.initialValueEncoding == "json")
564-
}
565-
}
566-
}
567-
}
568501
}

Tests/AblyLiveObjectsTests/WireObjectMessageTests.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ enum WireObjectMessageTests {
118118
counter: nil,
119119
nonce: nil,
120120
initialValue: nil,
121-
initialValueEncoding: nil,
122121
),
123122
object: nil,
124123
serial: "s1",
@@ -173,7 +172,6 @@ enum WireObjectMessageTests {
173172
"map": ["semantics": 0, "entries": ["key1": ["data": ["string": "value1"], "tombstone": false]]],
174173
"counter": ["count": 42],
175174
"nonce": "nonce1",
176-
"initialValueEncoding": "utf8",
177175
]
178176
let op = try WireObjectOperation(wireObject: wire)
179177
#expect(op.action == .known(.mapCreate))
@@ -189,8 +187,6 @@ enum WireObjectMessageTests {
189187
// Per OOP3g we should not try and extract this
190188
#expect(op.nonce == nil)
191189
// Per OOP3h we should not try and extract this
192-
#expect(op.initialValueEncoding == nil)
193-
// Per OOP3i we should not try and extract this
194190
#expect(op.initialValue == nil)
195191
}
196192

@@ -209,7 +205,6 @@ enum WireObjectMessageTests {
209205
#expect(op.counter == nil)
210206
#expect(op.nonce == nil)
211207
#expect(op.initialValue == nil)
212-
#expect(op.initialValueEncoding == nil)
213208
}
214209

215210
@Test
@@ -236,7 +231,6 @@ enum WireObjectMessageTests {
236231
counter: WireObjectsCounter(count: 42),
237232
nonce: "nonce1",
238233
initialValue: nil,
239-
initialValueEncoding: "utf8",
240234
)
241235
let wire = op.toWireObject
242236
#expect(wire == [
@@ -247,7 +241,6 @@ enum WireObjectMessageTests {
247241
"map": ["semantics": 0, "entries": ["key1": ["data": ["string": "value1"], "tombstone": false]]],
248242
"counter": ["count": 42],
249243
"nonce": "nonce1",
250-
"initialValueEncoding": "utf8",
251244
])
252245
}
253246

@@ -262,7 +255,6 @@ enum WireObjectMessageTests {
262255
counter: nil,
263256
nonce: nil,
264257
initialValue: nil,
265-
initialValueEncoding: nil,
266258
)
267259
let wire = op.toWireObject
268260
#expect(wire == [
@@ -326,7 +318,6 @@ enum WireObjectMessageTests {
326318
counter: nil,
327319
nonce: nil,
328320
initialValue: nil,
329-
initialValueEncoding: nil,
330321
),
331322
map: WireObjectsMap(
332323
semantics: .known(.lww),

0 commit comments

Comments
 (0)