Files
clawdbot/apps/macos/Sources/Clawdis/GatewayPayloadDecoding.swift
2025-12-13 04:28:43 +00:00

17 lines
547 B
Swift

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 self.decode(payload, as: T.self)
}
}