Skip to content

Commit

Permalink
extract the legacy support conformances out of BSONDecoding and into …
Browse files Browse the repository at this point in the history
…a BSONLegacy module, to trim down binary size
  • Loading branch information
tayloraswift committed May 17, 2024
1 parent b20c0b4 commit 8f34dc8
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 40 deletions.
47 changes: 26 additions & 21 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let package:Package = .init(name: "swift-mongodb",
],
products: [
.library(name: "BSON", targets: ["BSON"]),
.library(name: "BSONLegacy", targets: ["BSONLegacy"]),
.library(name: "BSONReflection", targets: ["BSONReflection"]),
.library(name: "BSONTesting", targets: ["BSONTesting"]),
.library(name: "BSONABI", targets: ["BSONABI"]),
Expand Down Expand Up @@ -57,28 +58,32 @@ let package:Package = .init(name: "swift-mongodb",
"README.md",
]),

.target(name: "BSONABI",
exclude: [
"README.md",
]),

.target(name: "BSONDecoding",
dependencies: [
.target(name: "BSONABI"),
.product(name: "TraceableErrors", package: "swift-grammar"),
],
exclude: [
"README.md",
]),

.target(name: "BSONEncoding",
dependencies: [
.target(name: "BSONABI"),
],
exclude: [
"README.md",
]),
.target(name: "BSONABI",
exclude: [
"README.md",
]),

.target(name: "BSONDecoding",
dependencies: [
.target(name: "BSONABI"),
.product(name: "TraceableErrors", package: "swift-grammar"),
],
exclude: [
"README.md",
]),

.target(name: "BSONEncoding",
dependencies: [
.target(name: "BSONABI"),
],
exclude: [
"README.md",
]),

.target(name: "BSONLegacy",
dependencies: [
.target(name: "BSON"),
]),

.target(name: "BSONReflection",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/BSON/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ An umbrella module providing a BSON parser, encoder, and decoder.

This module re-exports the ``BSONABI``, ``BSONEncoding``, and ``BSONDecoding`` modules. Importing them directly is discouraged.

Some BSON modules (currently ``BSONReflection`` and ``BSONTesting``) are considered ancillary and are not included in this umbrella module.
Some BSON modules (currently ``BSONLegacy``, ``BSONReflection``, and ``BSONTesting``) are considered ancillary and are not included in this umbrella module.
6 changes: 6 additions & 0 deletions Sources/BSONDecoding/Decoding/BSON.DocumentDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ extension BSON.DocumentDecoder:Sequence
}
extension BSON.DocumentDecoder
{
@inlinable public
func contains(_ key:CodingKey) -> Bool
{
self.index.keys.contains(key)
}

@inlinable public consuming
func single() throws -> BSON.FieldDecoder<CodingKey>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BSON

extension BSON.AnyValue:Decoder
{
@inlinable public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BSON

extension BSON.KeyedDecoder
{
enum Super:String, CodingKey
enum Super:String, CodingKey
{
case `super`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import BSON

extension BSON
{
struct KeyedDecoder<Key> where Key:CodingKey
{
let codingPath:[any CodingKey]
let allKeys:[Key]
let items:[BSON.Key: BSON.AnyValue]
let items:BSON.DocumentDecoder<BSON.Key>

init(_ dictionary:BSON.DocumentDecoder<BSON.Key>,
path:[any CodingKey])
{
self.codingPath = path
self.items = dictionary.index
self.allKeys = self.items.keys.compactMap { .init(stringValue: $0.rawValue) }
self.items = dictionary
self.allKeys = self.items.compactMap { .init(stringValue: $0.key.rawValue) }
}
}
}
extension BSON.KeyedDecoder
{
public
func contains(_ key:Key) -> Bool
{
self.items.keys.contains(.init(key))
}
// local `Key` type may be different from the dictionary’s `Key` type
func diagnose<T>(_ key:some CodingKey,
_ decode:(BSON.AnyValue) throws -> T?) throws -> T
Expand All @@ -30,7 +27,7 @@ extension BSON.KeyedDecoder
{
self.codingPath + CollectionOfOne<any CodingKey>.init(key)
}
guard let value:BSON.AnyValue = self.items[.init(key)]
guard let value:BSON.AnyValue = self.items[.init(key)]?.value
else
{
let context:DecodingError.Context = .init(codingPath: path,
Expand Down Expand Up @@ -60,6 +57,12 @@ extension BSON.KeyedDecoder

extension BSON.KeyedDecoder:KeyedDecodingContainerProtocol
{
public
func contains(_ key:Key) -> Bool
{
self.items.contains(.init(key))
}

public
func decode<T>(_:T.Type, forKey key:Key) throws -> T where T:Decodable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BSON

extension BSON
{
/// A single-value decoding container, for use with compiler-generated ``Decodable``
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import BSON

extension BSON.UnkeyedDecoder
{
struct Index:CodingKey
struct Index:CodingKey
{
let value:Int
var intValue:Int?
var intValue:Int?
{
self.value
self.value
}
var stringValue:String
{
"\(self.value)"
}

init(intValue:Int)
{
self.value = intValue
}
init?(stringValue:String)
{
guard let value:Int = Int.init(stringValue)
else
else
{
return nil
return nil
}
self.value = value
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BSON

extension BSON
{
struct UnkeyedDecoder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extension DecodingError
{
init<T>(annotating error:any Error, initializing _:T.Type, path:[any CodingKey])
init<T>(annotating error:any Error, initializing _:T.Type, path:[any CodingKey])
{
let description:String =
"""
initializer for type '\(String.init(reflecting: T.self))' \
threw an error while validating bson value at coding path \(path)
"""
let context:DecodingError.Context = .init(codingPath: path,
let context:DecodingError.Context = .init(codingPath: path,
debugDescription: description, underlyingError: error)
self = .dataCorrupted(context)
}
Expand Down

0 comments on commit 8f34dc8

Please sign in to comment.