2026-01-04 14:32:47 +00:00
|
|
|
import ClawdbotProtocol
|
2025-12-12 22:26:48 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
enum GatewayPayloadDecoding {
|
2026-01-04 14:32:47 +00:00
|
|
|
static func decode<T: Decodable>(_ payload: ClawdbotProtocol.AnyCodable, as _: T.Type = T.self) throws -> T {
|
2025-12-12 22:26:48 +00:00
|
|
|
let data = try JSONEncoder().encode(payload)
|
|
|
|
|
return try JSONDecoder().decode(T.self, from: data)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 14:32:47 +00:00
|
|
|
static func decodeIfPresent<T: Decodable>(_ payload: ClawdbotProtocol.AnyCodable?, as _: T.Type = T.self) throws
|
2025-12-12 22:26:48 +00:00
|
|
|
-> T?
|
|
|
|
|
{
|
|
|
|
|
guard let payload else { return nil }
|
2025-12-13 04:28:12 +00:00
|
|
|
return try self.decode(payload, as: T.self)
|
2025-12-12 22:26:48 +00:00
|
|
|
}
|
|
|
|
|
}
|