Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 46
[ECO-5139] feat: add action and serial fields#1048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -46,9 +46,41 @@ public class Message extends BaseMessage { | ||
| */ | ||
| public String connectionKey; | ||
| /** | ||
| * (TM2k) serial string – an opaque string that uniquely identifies the message. If a message received from Ably | ||
| * (whether over realtime or REST, eg history) with an action of MESSAGE_CREATE does not contain a serial, | ||
| * the SDK must set it equal to its version. | ||
| */ | ||
| public String serial; | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * (TM2p) version string – an opaque string that uniquely identifies the message, and is different for different versions. | ||
| * If a message received from Ably over a realtime transport does not contain a version, | ||
| * the SDK must set it to <channelSerial>:<padded_index> from the channelSerial field of the enclosing ProtocolMessage, | ||
| * and padded_index is the index of the message inside the messages array of the ProtocolMessage, | ||
| * left-padded with 0s to three digits (for example, the second entry might be foo:001) | ||
| */ | ||
| public String version; | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * (TM2j) action enum | ||
| */ | ||
| public MessageAction action; | ||
| /** | ||
| * (TM2o) createdAt time in milliseconds since epoch. If a message received from Ably | ||
| * (whether over realtime or REST, eg history) with an action of MESSAGE_CREATE does not contain a createdAt, | ||
| * the SDK must set it equal to the TM2f timestamp. | ||
| */ | ||
| public Long createdAt; | ||
| private static final String NAME = "name"; | ||
| private static final String EXTRAS = "extras"; | ||
| private static final String CONNECTION_KEY = "connectionKey"; | ||
| private static final String SERIAL = "serial"; | ||
| private static final String VERSION = "version"; | ||
| private static final String ACTION = "action"; | ||
| private static final String CREATED_AT = "createdAt"; | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Default constructor | ||
| @@ -128,6 +160,10 @@ void writeMsgpack(MessagePacker packer) throws IOException { | ||
| int fieldCount = super.countFields(); | ||
| if(name != null) ++fieldCount; | ||
| if(extras != null) ++fieldCount; | ||
| if(serial != null) ++fieldCount; | ||
| if(version != null) ++fieldCount; | ||
| if(action != null) ++fieldCount; | ||
| if(createdAt != null) ++fieldCount; | ||
| packer.packMapHeader(fieldCount); | ||
| super.writeFields(packer); | ||
| if(name != null) { | ||
| @@ -138,6 +174,22 @@ void writeMsgpack(MessagePacker packer) throws IOException { | ||
| packer.packString(EXTRAS); | ||
| extras.write(packer); | ||
| } | ||
| if(serial != null) { | ||
| packer.packString(SERIAL); | ||
| packer.packString(serial); | ||
| } | ||
| if(version != null) { | ||
| packer.packString(VERSION); | ||
| packer.packString(version); | ||
| } | ||
| if(action != null) { | ||
| packer.packString(ACTION); | ||
| packer.packInt(action.ordinal()); | ||
| } | ||
| if(createdAt != null) { | ||
| packer.packString(CREATED_AT); | ||
| packer.packLong(createdAt); | ||
| } | ||
| } | ||
| Message readMsgpack(MessageUnpacker unpacker) throws IOException { | ||
| @@ -157,6 +209,14 @@ Message readMsgpack(MessageUnpacker unpacker) throws IOException { | ||
| name = unpacker.unpackString(); | ||
| } else if (fieldName.equals(EXTRAS)) { | ||
| extras = MessageExtras.read(unpacker); | ||
| } else if (fieldName.equals(SERIAL)) { | ||
| serial = unpacker.unpackString(); | ||
| } else if (fieldName.equals(VERSION)) { | ||
| version = unpacker.unpackString(); | ||
| } else if (fieldName.equals(ACTION)) { | ||
| action = MessageAction.tryFindByOrdinal(unpacker.unpackInt()); | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } else if (fieldName.equals(CREATED_AT)) { | ||
| createdAt = unpacker.unpackLong(); | ||
| } else { | ||
| Log.v(TAG, "Unexpected field: " + fieldName); | ||
| unpacker.skipValue(); | ||
| @@ -313,6 +373,12 @@ protected void read(final JsonObject map) throws MessageDecodeException { | ||
| } | ||
| extras = MessageExtras.read((JsonObject) extrasElement); | ||
| } | ||
| serial = readString(map, SERIAL); | ||
| version = readString(map, VERSION); | ||
| Integer actionOrdinal = readInt(map, ACTION); | ||
| action = actionOrdinal == null ? null : MessageAction.tryFindByOrdinal(actionOrdinal); | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| createdAt = readLong(map, CREATED_AT); | ||
| } | ||
| public static class Serializer implements JsonSerializer<Message>, JsonDeserializer<Message> { | ||
| @@ -328,6 +394,18 @@ public JsonElement serialize(Message message, Type typeOfMessage, JsonSerializat | ||
| if (message.connectionKey != null) { | ||
| json.addProperty(CONNECTION_KEY, message.connectionKey); | ||
| } | ||
| if (message.serial != null) { | ||
| json.addProperty(SERIAL, message.serial); | ||
| } | ||
| if (message.version != null) { | ||
| json.addProperty(VERSION, message.version); | ||
| } | ||
| if (message.action != null) { | ||
| json.addProperty(ACTION, message.action.ordinal()); | ||
| } | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (message.createdAt != null) { | ||
| json.addProperty(CREATED_AT, message.createdAt); | ||
| } | ||
| return json; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.ably.lib.types; | ||
| public enum MessageAction { | ||
| MESSAGE_UNSET, // 0 | ||
| MESSAGE_CREATE, // 1 | ||
| MESSAGE_UPDATE, // 2 | ||
| MESSAGE_DELETE, // 3 | ||
| ANNOTATION_CREATE, // 4 | ||
| ANNOTATION_DELETE, // 5 | ||
| META_OCCUPANCY; // 6 | ||
| static MessageAction tryFindByOrdinal(int ordinal) { | ||
| return values().length <= ordinal ? null: values()[ordinal]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -46,4 +46,69 @@ public void serialize_message_with_name_and_data() { | ||
| assertEquals("test-data", serializedObject.get("data").getAsString()); | ||
| assertEquals("test-name", serializedObject.get("name").getAsString()); | ||
| } | ||
| @Test | ||
| public void serialize_message_with_serial() { | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Given | ||
| Message message = new Message("test-name", "test-data"); | ||
| message.clientId = "test-client-id"; | ||
| message.connectionKey = "test-key"; | ||
| message.action = MessageAction.MESSAGE_CREATE; | ||
| message.serial = "01826232498871-001@abcdefghij:001"; | ||
| // When | ||
| JsonElement serializedElement = serializer.serialize(message, null, null); | ||
| // Then | ||
| JsonObject serializedObject = serializedElement.getAsJsonObject(); | ||
| assertEquals("test-client-id", serializedObject.get("clientId").getAsString()); | ||
| assertEquals("test-key", serializedObject.get("connectionKey").getAsString()); | ||
| assertEquals("test-data", serializedObject.get("data").getAsString()); | ||
| assertEquals("test-name", serializedObject.get("name").getAsString()); | ||
| assertEquals(1, serializedObject.get("action").getAsInt()); | ||
| assertEquals("01826232498871-001@abcdefghij:001", serializedObject.get("serial").getAsString()); | ||
| } | ||
| @Test | ||
| public void deserialize_message_with_serial() throws Exception { | ||
| // Given | ||
| JsonObject jsonObject = new JsonObject(); | ||
| jsonObject.addProperty("clientId", "test-client-id"); | ||
| jsonObject.addProperty("data", "test-data"); | ||
| jsonObject.addProperty("name", "test-name"); | ||
| jsonObject.addProperty("action", 1); | ||
| jsonObject.addProperty("serial", "01826232498871-001@abcdefghij:001"); | ||
| // When | ||
| Message message = Message.fromEncoded(jsonObject, new ChannelOptions()); | ||
| // Then | ||
| assertEquals("test-client-id", message.clientId); | ||
| assertEquals("test-data", message.data); | ||
| assertEquals("test-name", message.name); | ||
| assertEquals(MessageAction.MESSAGE_CREATE, message.action); | ||
| assertEquals("01826232498871-001@abcdefghij:001", message.serial); | ||
| } | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @Test | ||
| public void deserialize_message_with_unknown_action() throws Exception { | ||
| // Given | ||
| JsonObject jsonObject = new JsonObject(); | ||
| jsonObject.addProperty("clientId", "test-client-id"); | ||
| jsonObject.addProperty("data", "test-data"); | ||
| jsonObject.addProperty("name", "test-name"); | ||
| jsonObject.addProperty("action", 10); | ||
| jsonObject.addProperty("serial", "01826232498871-001@abcdefghij:001"); | ||
| // When | ||
| Message message = Message.fromEncoded(jsonObject, new ChannelOptions()); | ||
| // Then | ||
| assertEquals("test-client-id", message.clientId); | ||
| assertEquals("test-data", message.data); | ||
| assertEquals("test-name", message.name); | ||
| assertNull(message.action); | ||
| assertEquals("01826232498871-001@abcdefghij:001", message.serial); | ||
| } | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.