Files
clawdbot/apps/shared/ClawdbotKit/Sources/ClawdbotKit/GatewayPayloadDecoding.swift
2026-01-19 10:09:04 +00:00

37 lines
1.1 KiB
Swift

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