Skip to content

Commit b7b79d5

Browse files
Add a format to the LiveObjects encoding and decoding methods
The plugin will use this to apply the spec's rules (see [1]) regarding the encoding and decoding of binary data. This was initially generated by Cursor at my instructions, but I ended up rewriting most of the documentation it generated because it didn't have enough context to understand how the plugin would use the format. [1] ably/specification#335
1 parent ae0f213 commit b7b79d5

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// The wire format to which a plugin object should encode an object or from which a plugin should be decode an object.
2+
///
3+
/// The requirements for encoding to and decoding from each format are as follows:
4+
///
5+
/// - `APEncodingFormatJSON`:
6+
/// - Encoding: Plugins should generate an object that ably-cocoa can pass to Foundation's `JSONSerialization.data(withJSONObject:,…)`.
7+
/// - Decoding: Plugins should expect to find the same types of values as you would in the output of Foundation's `JSONSerialization`.
8+
/// - `APEncodingFormatMessagePack`:
9+
/// - Encoding: As for `APEncodingFormatJSON`, but arrays and dictionaries are allowed to additionally contain `NSData` values.
10+
/// - Decoding: As for `APEncodingFormatJSON`, but arrays and dictionaries may also additionally contain `NSData` values.
11+
///
12+
/// We intentionally use a closed enum so that plugins do not have to include an `@unknown default` (since there is no sensible default for them to use; if we introduce a new encoding format in the future then we will have to define new logic — and since it's a closed enum, a new enum).
13+
typedef NS_CLOSED_ENUM(NSInteger, APEncodingFormat) {
14+
APEncodingFormatJSON NS_SWIFT_NAME(json),
15+
APEncodingFormatMessagePack
16+
} NS_SWIFT_NAME(EncodingFormat);

AblyPlugin/include/APLiveObjectsPlugin.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import <Foundation/Foundation.h>
2+
#import "APEncodingFormat.h"
23

34
@class ARTRealtimeChannel;
45
@class ARTErrorInfo;
@@ -35,21 +36,25 @@ NS_SWIFT_SENDABLE
3536
/// Decodes an `ObjectMessage` received over the wire.
3637
///
3738
/// Parameters:
38-
/// - serialized: A dictionary that contains the representation of the `ObjectMessage` received over the wire. You should expect to find the same types of values here as you would in the output of Foundation's `JSONSerialization`.
39+
/// - serialized: A dictionary that contains the representation of the `ObjectMessage` received over the wire. To find out what kinds of values you should expect to find here for a given `format`, see th decoding rules described in `APEncodingFormat`.
3940
/// - context: Contains information that may be needed in the decoding, such as information about the containing `ProtocolMessage`.
41+
/// - format: The format that was used to create `serialized`, and whose rules should be used when decoding the `ObjectMessage`.
4042
///
4143
/// Returns: A `ObjectMessageProtocol` object that ably-cocoa can later pass to this plugin's `-handleObjectProtocolMessageWithObjectMessages:channel:` method, or `nil` if decoding fails (in which case `error` must be populated).
4244
- (nullable id<APObjectMessageProtocol>)decodeObjectMessage:(NSDictionary<NSString *, id> *)serialized
4345
context:(id<APDecodingContextProtocol>)context
46+
format:(APEncodingFormat)format
4447
error:(ARTErrorInfo *_Nullable *_Nullable)error;
4548

4649
/// Encodes an `ObjectMessage` to be sent over the wire.
4750
///
4851
/// Parameters:
4952
/// - objectMessage: An `ObjectMessage` that this plugin earlier passed to `APPluginAPI`'s `-sendStateWithObjectMessages:channel:completion:`.
53+
/// - format: The format whose rules should be used when encoding the `ObjectMessage`.
5054
///
51-
/// Returns: An object that ably-cocoa can pass to Foundation's `JSONSerialization`.
52-
- (NSDictionary<NSString *, id> *)encodeObjectMessage:(id<APObjectMessageProtocol>)objectMessage;
55+
/// Returns: See the encoding rules described in `APEncodingFormat`.
56+
- (NSDictionary<NSString *, id> *)encodeObjectMessage:(id<APObjectMessageProtocol>)objectMessage
57+
format:(APEncodingFormat)format;
5358

5459
/// Called when a channel received an `ATTACHED` `ProtocolMessage`. (This is copied from ably-js, will document this method properly once exact meaning decided.)
5560
///

Source/ARTJsonLikeEncoder.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,16 @@ - (NSData *)encode:(id)obj error:(NSError **)error {
11021102
return encoded;
11031103
}
11041104

1105+
/// Converts an `ARTEncoderFormat` to an `APEncodingFormat`.
1106+
- (APEncodingFormat)apEncodingFormatFromARTEncoderFormat:(ARTEncoderFormat)format {
1107+
switch (format) {
1108+
case ARTEncoderFormatJson:
1109+
return APEncodingFormatJSON;
1110+
case ARTEncoderFormatMsgPack:
1111+
return APEncodingFormatMessagePack;
1112+
}
1113+
}
1114+
11051115
/// Uses the LiveObjects plugin to decode an array of `ObjectMessage`s.
11061116
///
11071117
/// Returns `nil` if the LiveObjects plugin has not been supplied, or if we fail to decode any of the `ObjectMessage`s.
@@ -1134,9 +1144,11 @@ - (NSData *)encode:(id)obj error:(NSError **)error {
11341144
indexInParent:i];
11351145

11361146
ARTErrorInfo *error;
1147+
APEncodingFormat format = [self apEncodingFormatFromARTEncoderFormat:[self format]];
11371148

11381149
id<APObjectMessageProtocol> objectMessage = [liveObjectsPlugin decodeObjectMessage:item
11391150
context:decodingContext
1151+
format:format
11401152
error:&error];
11411153

11421154
if (!objectMessage) {
@@ -1167,9 +1179,10 @@ - (NSData *)encode:(id)obj error:(NSError **)error {
11671179
}
11681180

11691181
NSMutableArray<NSDictionary *> *result = [NSMutableArray array];
1182+
APEncodingFormat format = [self apEncodingFormatFromARTEncoderFormat:[self format]];
11701183

11711184
for (id objectMessage in objectMessages) {
1172-
[result addObject:[liveObjectsPlugin encodeObjectMessage:objectMessage]];
1185+
[result addObject:[liveObjectsPlugin encodeObjectMessage:objectMessage format:format]];
11731186
}
11741187

11751188
return result;

0 commit comments

Comments
 (0)