refactor(macos): extract gateway payload decoding

This commit is contained in:
Peter Steinberger
2025-12-12 22:26:48 +00:00
parent 14e3b34a8e
commit c7bd4b5c1d
3 changed files with 19 additions and 9 deletions

View File

@@ -0,0 +1,16 @@
import ClawdisProtocol
import Foundation
enum GatewayPayloadDecoding {
static func decode<T: Decodable>(_ payload: ClawdisProtocol.AnyCodable, as _: T.Type = T.self) throws -> T {
let data = try JSONEncoder().encode(payload)
return try JSONDecoder().decode(T.self, from: data)
}
static func decodeIfPresent<T: Decodable>(_ payload: ClawdisProtocol.AnyCodable?, as _: T.Type = T.self) throws
-> T?
{
guard let payload else { return nil }
return try decode(payload, as: T.self)
}
}