From 4fb4a8064135247219323d171d8ed130f3ca2a84 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Tue, 7 Feb 2023 06:35:51 -0600 Subject: [PATCH] Provide implementation for superDecoder() and superDecoder(forKey:) in BSONDecoder --- .../Decoding/KeyedBSONDecodingContainer.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/BSON/Codable/Decoding/KeyedBSONDecodingContainer.swift b/Sources/BSON/Codable/Decoding/KeyedBSONDecodingContainer.swift index 5432dfe..6c1d039 100644 --- a/Sources/BSON/Codable/Decoding/KeyedBSONDecodingContainer.swift +++ b/Sources/BSON/Codable/Decoding/KeyedBSONDecodingContainer.swift @@ -219,12 +219,18 @@ internal struct KeyedBSONDecodingContainer: KeyedDecodingContainer } func superDecoder() throws -> Decoder { - // TODO: Use `super` key - return decoder + let value = self.document[self.decoder.converted(BSONKey.super.stringValue)] + let decoderValue: DecoderValue = (value as? Document).map({ .document($0) }) ?? .primitive(value) + let decoder = _BSONDecoder(wrapped: decoderValue, settings: self.decoder.settings, codingPath: self.codingPath + [BSONKey.super], userInfo: self.decoder.userInfo) + + return decoder // N.B.: Can't use `self.superDecoder(forKey: BSONKey.super)` due to generic constraint } func superDecoder(forKey key: K) throws -> Decoder { - // TODO: Respect given key + let value = self.document[self.decoder.converted(key.stringValue)] + let decoderValue: DecoderValue = (value as? Document).map({ .document($0) }) ?? .primitive(value) + let decoder = _BSONDecoder(wrapped: decoderValue, settings: self.decoder.settings, codingPath: self.codingPath + [key], userInfo: self.decoder.userInfo) + return decoder } }