Update AnyNotification to include notification params (#86)

* Update AnyNotification to not wipe out params that come with a notification

* Add test coverage for notification with params

* Allow params to be omitted when empty

---------

Co-authored-by: Stephen Tallent <stallent@mercury.io>
Co-authored-by: Mattt Zmuda <mattt@loopwork.com>
This commit is contained in:
Stephen Tallent 2025-04-27 08:47:38 -05:00 • committed by GitHub
commit 99517b0564
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -278,7 +278,7 @@ public protocol Notification: Hashable, Codable, Sendable {
/// A type-erased notification for message handling
struct AnyNotification: Notification, Sendable {
static var name: String { "" }
typealias Parameters = Empty
typealias Parameters = Value
}
extension AnyNotification {
@ -325,10 +325,11 @@ public struct Message<N: Notification>: Hashable, Codable, Sendable {
}
method = try container.decode(String.self, forKey: .method)
// Handle params field being optional
if N.Parameters.self == Empty.self {
// For Empty parameters, use Empty() if params is missing or null
params = Empty() as! N.Parameters
if N.Parameters.self is NotRequired.Type {
// For NotRequired parameters, use decodeIfPresent or init()
params =
(try container.decodeIfPresent(N.Parameters.self, forKey: .params)
?? (N.Parameters.self as! NotRequired.Type).init() as! N.Parameters)
} else if let value = try? container.decode(N.Parameters.self, forKey: .params) {
// If params exists and can be decoded, use it
params = value