-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix sending and receiving of reaction metadata and headers
Similar issue to the one which was fixed in 7fcab5c. Haven’t added tests for the handling of the incoming message, since there were no existing tests to update and I didn’t feel like writing them; will leave this for whoever writes these tests in #88. The approach that I’ve taken here of using a DTO is consistent with the approach that we use for presence data. I should have done this in 7fcab5c too; will do it separately. The spec doesn’t describe how to behave if the user doesn’t pass headers or metadata; I’ve taken the behaviour of passing an empty object from the JS Chat SDK at 6d1b04a. Have created spec issue [1] for specifying this. Resolves #198. [1] ably/specification#256
- Loading branch information
1 parent
8b8a084
commit 6035a1e
Showing
7 changed files
with
220 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// CHA-ER3a | ||
internal struct RoomReactionDTO { | ||
internal var data: Data | ||
internal var extras: Extras | ||
|
||
internal struct Data: Equatable { | ||
internal var type: String | ||
internal var metadata: ReactionMetadata? | ||
} | ||
|
||
internal struct Extras: Equatable { | ||
internal var headers: ReactionHeaders? | ||
} | ||
} | ||
|
||
internal extension RoomReactionDTO { | ||
init(type: String, metadata: ReactionMetadata?, headers: ReactionHeaders?) { | ||
data = .init(type: type, metadata: metadata) | ||
extras = .init(headers: headers) | ||
} | ||
|
||
var type: String { | ||
data.type | ||
} | ||
|
||
var metadata: ReactionMetadata? { | ||
data.metadata | ||
} | ||
|
||
var headers: ReactionHeaders? { | ||
extras.headers | ||
} | ||
} | ||
|
||
// MARK: - JSONCodable | ||
|
||
extension RoomReactionDTO.Data: JSONObjectCodable { | ||
internal enum JSONKey: String { | ||
case type | ||
case metadata | ||
} | ||
|
||
internal init(jsonObject: [String: JSONValue]) throws { | ||
type = try jsonObject.stringValueForKey(JSONKey.type.rawValue) | ||
metadata = try jsonObject.optionalObjectValueForKey(JSONKey.metadata.rawValue)?.mapValues { try .init(jsonValue: $0) } | ||
} | ||
|
||
internal var toJSONObject: [String: JSONValue] { | ||
[ | ||
JSONKey.type.rawValue: .string(type), | ||
JSONKey.metadata.rawValue: .object(metadata?.mapValues(\.toJSONValue) ?? [:]), | ||
] | ||
} | ||
} | ||
|
||
extension RoomReactionDTO.Extras: JSONObjectCodable { | ||
internal enum JSONKey: String { | ||
case headers | ||
} | ||
|
||
internal init(jsonObject: [String: JSONValue]) throws { | ||
headers = try jsonObject.optionalObjectValueForKey(JSONKey.headers.rawValue)?.mapValues { try .init(jsonValue: $0) } | ||
} | ||
|
||
internal var toJSONObject: [String: JSONValue] { | ||
[ | ||
JSONKey.headers.rawValue: .object(headers?.mapValues(\.toJSONValue) ?? [:]), | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
@testable import AblyChat | ||
import Testing | ||
|
||
enum RoomReactionDTOTests { | ||
struct DataTests { | ||
// MARK: - JSONDecodable | ||
|
||
@Test | ||
func initWithJSONValue_failsIfNotObject() { | ||
#expect(throws: JSONValueDecodingError.self) { | ||
try RoomReactionDTO.Data(jsonValue: "hello") | ||
} | ||
} | ||
|
||
@Test | ||
func initWithJSONValue_withNoTypeKey() { | ||
#expect(throws: JSONValueDecodingError.self) { | ||
try RoomReactionDTO.Data(jsonValue: [:]) | ||
} | ||
} | ||
|
||
@Test | ||
func initWithJSONValue_withNoMetadataKey() throws { | ||
#expect(try RoomReactionDTO.Data(jsonValue: ["type": "" /* arbitrary */ ]).metadata == nil) | ||
} | ||
|
||
@Test | ||
func initWithJSONValue() throws { | ||
let data = try RoomReactionDTO.Data( | ||
jsonValue: [ | ||
"type": "someType", | ||
"metadata": [ | ||
"someStringKey": "someStringValue", | ||
"someNumberKey": 123, | ||
], | ||
] | ||
) | ||
|
||
#expect(data == .init(type: "someType", metadata: ["someStringKey": .string("someStringValue"), "someNumberKey": .number(123)])) | ||
} | ||
|
||
// MARK: - JSONCodable | ||
|
||
@Test | ||
func toJSONValue_withNilMetadata() { | ||
// i.e. should create an empty object for metadata | ||
#expect(RoomReactionDTO.Data(type: "" /* arbitrary */, metadata: nil).toJSONValue == .object(["type": "", "metadata": .object([:])])) | ||
} | ||
|
||
@Test | ||
func toJSONValue() { | ||
let data = RoomReactionDTO.Data(type: "someType", metadata: ["someStringKey": .string("someStringValue"), "someNumberKey": .number(123)]) | ||
|
||
#expect(data.toJSONValue == [ | ||
"type": "someType", | ||
"metadata": [ | ||
"someStringKey": "someStringValue", | ||
"someNumberKey": 123, | ||
], | ||
]) | ||
} | ||
} | ||
|
||
struct ExtrasTests { | ||
// MARK: - JSONDecodable | ||
|
||
@Test | ||
func initWithJSONValue_failsIfNotObject() { | ||
#expect(throws: JSONValueDecodingError.self) { | ||
try RoomReactionDTO.Extras(jsonValue: "hello") | ||
} | ||
} | ||
|
||
@Test | ||
func initWithJSONValue_withNoHeadersKey() throws { | ||
#expect(try RoomReactionDTO.Extras(jsonValue: [:]).headers == nil) | ||
} | ||
|
||
@Test | ||
func initWithJSONValue() throws { | ||
let data = try RoomReactionDTO.Extras( | ||
jsonValue: [ | ||
"headers": [ | ||
"someStringKey": "someStringValue", | ||
"someNumberKey": 123, | ||
], | ||
] | ||
) | ||
|
||
#expect(data == .init(headers: ["someStringKey": .string("someStringValue"), "someNumberKey": .number(123)])) | ||
} | ||
|
||
// MARK: - JSONCodable | ||
|
||
@Test | ||
func toJSONValue_withNilHeaders() { | ||
// i.e. should create an empty object for headers | ||
#expect(RoomReactionDTO.Extras(headers: nil).toJSONValue == .object(["headers": .object([:])])) | ||
} | ||
|
||
@Test | ||
func toJSONValue() { | ||
let data = RoomReactionDTO.Extras(headers: ["someStringKey": .string("someStringValue"), "someNumberKey": .number(123)]) | ||
|
||
#expect(data.toJSONValue == [ | ||
"headers": [ | ||
"someStringKey": "someStringValue", | ||
"someNumberKey": 123, | ||
], | ||
]) | ||
} | ||
} | ||
} |