mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-01 23:41:40 +00:00
feat(ios): HapiProtocol wire models, catalogs, session patching (A-M1a)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import Foundation
|
||||
|
||||
/// A pass-through coding key used to enumerate whatever keys a JSON object
|
||||
/// actually carries (the synthesized `CodingKeys` enums cannot represent
|
||||
/// unknown keys).
|
||||
struct AnyCodingKey: CodingKey {
|
||||
let stringValue: String
|
||||
let intValue: Int?
|
||||
|
||||
init?(stringValue: String) {
|
||||
self.stringValue = stringValue
|
||||
self.intValue = nil
|
||||
}
|
||||
|
||||
init?(intValue: Int) {
|
||||
self.stringValue = String(intValue)
|
||||
self.intValue = intValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Mirror of zod's `.strict()`: throw when the payload carries a key outside
|
||||
/// `known`. Used by the patch types whose strictness discriminates the
|
||||
/// `Session | SessionPatch` and `Machine | MachinePatch | null` unions.
|
||||
func rejectUnknownKeys(in decoder: Decoder, known: Set<String>, payloadName: String) throws {
|
||||
let container = try decoder.container(keyedBy: AnyCodingKey.self)
|
||||
for key in container.allKeys where !known.contains(key.stringValue) {
|
||||
throw DecodingError.dataCorrupted(DecodingError.Context(
|
||||
codingPath: decoder.codingPath,
|
||||
debugDescription: "Unknown key '\(key.stringValue)' in strict \(payloadName) payload"
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user