From 8f34dc8c256f66e921fa44e38d60ca35443ab6b7 Mon Sep 17 00:00:00 2001 From: taylorswift Date: Fri, 17 May 2024 20:17:44 +0000 Subject: [PATCH] extract the legacy support conformances out of BSONDecoding and into a BSONLegacy module, to trim down binary size --- Package.swift | 47 ++++++++++--------- Sources/BSON/README.md | 2 +- .../Decoding/BSON.DocumentDecoder.swift | 6 +++ .../BSON.AnyValue (ext).swift | 2 + .../BSON.KeyedDecoder.Super.swift | 4 +- .../BSON.KeyedDecoder.swift | 21 +++++---- .../BSON.SingleValueDecoder.swift | 2 + .../BSON.UnkeyedDecoder.Index.swift | 14 +++--- .../BSON.UnkeyedDecoder.swift | 2 + .../DecodingError (ext).swift} | 4 +- 10 files changed, 64 insertions(+), 40 deletions(-) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.AnyValue (ext).swift (98%) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.KeyedDecoder.Super.swift (58%) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.KeyedDecoder.swift (94%) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.SingleValueDecoder.swift (99%) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.UnkeyedDecoder.Index.swift (75%) rename Sources/{BSONDecoding/Legacy => BSONLegacy}/BSON.UnkeyedDecoder.swift (99%) rename Sources/{BSONDecoding/Legacy/DecodingError.swift => BSONLegacy/DecodingError (ext).swift} (94%) diff --git a/Package.swift b/Package.swift index a352c21a..9c021f9b 100644 --- a/Package.swift +++ b/Package.swift @@ -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"]), @@ -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: [ diff --git a/Sources/BSON/README.md b/Sources/BSON/README.md index e050b045..cd2a8a29 100644 --- a/Sources/BSON/README.md +++ b/Sources/BSON/README.md @@ -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. diff --git a/Sources/BSONDecoding/Decoding/BSON.DocumentDecoder.swift b/Sources/BSONDecoding/Decoding/BSON.DocumentDecoder.swift index 857fa8e8..b573c383 100644 --- a/Sources/BSONDecoding/Decoding/BSON.DocumentDecoder.swift +++ b/Sources/BSONDecoding/Decoding/BSON.DocumentDecoder.swift @@ -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 { diff --git a/Sources/BSONDecoding/Legacy/BSON.AnyValue (ext).swift b/Sources/BSONLegacy/BSON.AnyValue (ext).swift similarity index 98% rename from Sources/BSONDecoding/Legacy/BSON.AnyValue (ext).swift rename to Sources/BSONLegacy/BSON.AnyValue (ext).swift index 8df73866..b9c9809f 100644 --- a/Sources/BSONDecoding/Legacy/BSON.AnyValue (ext).swift +++ b/Sources/BSONLegacy/BSON.AnyValue (ext).swift @@ -1,3 +1,5 @@ +import BSON + extension BSON.AnyValue:Decoder { @inlinable public diff --git a/Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.Super.swift b/Sources/BSONLegacy/BSON.KeyedDecoder.Super.swift similarity index 58% rename from Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.Super.swift rename to Sources/BSONLegacy/BSON.KeyedDecoder.Super.swift index 00d8732c..237266a7 100644 --- a/Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.Super.swift +++ b/Sources/BSONLegacy/BSON.KeyedDecoder.Super.swift @@ -1,6 +1,8 @@ +import BSON + extension BSON.KeyedDecoder { - enum Super:String, CodingKey + enum Super:String, CodingKey { case `super` } diff --git a/Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.swift b/Sources/BSONLegacy/BSON.KeyedDecoder.swift similarity index 94% rename from Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.swift rename to Sources/BSONLegacy/BSON.KeyedDecoder.swift index 9551316b..bc05b02a 100644 --- a/Sources/BSONDecoding/Legacy/BSON.KeyedDecoder.swift +++ b/Sources/BSONLegacy/BSON.KeyedDecoder.swift @@ -1,27 +1,24 @@ +import BSON + extension BSON { struct KeyedDecoder where Key:CodingKey { let codingPath:[any CodingKey] let allKeys:[Key] - let items:[BSON.Key: BSON.AnyValue] + let items:BSON.DocumentDecoder init(_ dictionary:BSON.DocumentDecoder, 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(_ key:some CodingKey, _ decode:(BSON.AnyValue) throws -> T?) throws -> T @@ -30,7 +27,7 @@ extension BSON.KeyedDecoder { self.codingPath + CollectionOfOne.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, @@ -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.Type, forKey key:Key) throws -> T where T:Decodable { diff --git a/Sources/BSONDecoding/Legacy/BSON.SingleValueDecoder.swift b/Sources/BSONLegacy/BSON.SingleValueDecoder.swift similarity index 99% rename from Sources/BSONDecoding/Legacy/BSON.SingleValueDecoder.swift rename to Sources/BSONLegacy/BSON.SingleValueDecoder.swift index bff5303f..260086a0 100644 --- a/Sources/BSONDecoding/Legacy/BSON.SingleValueDecoder.swift +++ b/Sources/BSONLegacy/BSON.SingleValueDecoder.swift @@ -1,3 +1,5 @@ +import BSON + extension BSON { /// A single-value decoding container, for use with compiler-generated ``Decodable`` diff --git a/Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.Index.swift b/Sources/BSONLegacy/BSON.UnkeyedDecoder.Index.swift similarity index 75% rename from Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.Index.swift rename to Sources/BSONLegacy/BSON.UnkeyedDecoder.Index.swift index 176a730e..df138f61 100644 --- a/Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.Index.swift +++ b/Sources/BSONLegacy/BSON.UnkeyedDecoder.Index.swift @@ -1,17 +1,19 @@ +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 @@ -19,9 +21,9 @@ extension BSON.UnkeyedDecoder init?(stringValue:String) { guard let value:Int = Int.init(stringValue) - else + else { - return nil + return nil } self.value = value } diff --git a/Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.swift b/Sources/BSONLegacy/BSON.UnkeyedDecoder.swift similarity index 99% rename from Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.swift rename to Sources/BSONLegacy/BSON.UnkeyedDecoder.swift index 24210b3a..6dc6aedd 100644 --- a/Sources/BSONDecoding/Legacy/BSON.UnkeyedDecoder.swift +++ b/Sources/BSONLegacy/BSON.UnkeyedDecoder.swift @@ -1,3 +1,5 @@ +import BSON + extension BSON { struct UnkeyedDecoder diff --git a/Sources/BSONDecoding/Legacy/DecodingError.swift b/Sources/BSONLegacy/DecodingError (ext).swift similarity index 94% rename from Sources/BSONDecoding/Legacy/DecodingError.swift rename to Sources/BSONLegacy/DecodingError (ext).swift index f086b0c5..c99eaa46 100644 --- a/Sources/BSONDecoding/Legacy/DecodingError.swift +++ b/Sources/BSONLegacy/DecodingError (ext).swift @@ -1,13 +1,13 @@ extension DecodingError { - init(annotating error:any Error, initializing _:T.Type, path:[any CodingKey]) + init(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) }