Skip to content

Commit

Permalink
Merge pull request #76 from gwynne/fix-superDecoder-8.x
Browse files Browse the repository at this point in the history
Add missing superDecoder() and superDecoder(forKey:) for BSONDecoder
  • Loading branch information
Joannis authored Feb 7, 2023
2 parents 1e98bdf + 6d5e215 commit e24ce41
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Sources/BSON/Codable/Decoding/KeyedBSONDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,18 @@ internal struct KeyedBSONDecodingContainer<K: CodingKey>: 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
}
}

0 comments on commit e24ce41

Please sign in to comment.