@@ -139,43 +139,16 @@ public class BSONEncoder {
139
139
* - Throws: `EncodingError` if any value throws an error during encoding.
140
140
*/
141
141
public func encode< T: Encodable > ( _ value: T ) throws -> BSONDocument {
142
- // if the value being encoded is already a `BSONDocument` we're done
143
- if let doc = value as? BSONDocument {
142
+ let encodedBSON : BSON = try self . encode ( value)
143
+ switch encodedBSON {
144
+ case let . document( doc) :
144
145
return doc
145
- } else if let bson = value as? BSON , let doc = bson. documentValue {
146
- return doc
147
- }
148
-
149
- let encoder = _BSONEncoder ( options: self . options)
150
-
151
- do {
152
- guard let boxedValue = try encoder. box_ ( value) else {
153
- throw EncodingError . invalidValue (
154
- value,
155
- EncodingError . Context (
156
- codingPath: [ ] ,
157
- debugDescription: " Top-level \( T . self) did not encode any values. "
158
- )
159
- )
160
- }
161
-
162
- guard let dict = boxedValue as? MutableDictionary else {
163
- throw EncodingError . invalidValue (
164
- value,
165
- EncodingError . Context (
166
- codingPath: [ ] ,
167
- debugDescription: " Top-level \( T . self) was not encoded as a complete document. "
168
- )
169
- )
170
- }
171
-
172
- return try dict. toDocument ( )
173
- } catch let error as BSONErrorProtocol {
146
+ default :
174
147
throw EncodingError . invalidValue (
175
148
value,
176
149
EncodingError . Context (
177
150
codingPath: [ ] ,
178
- debugDescription: error . errorDescription ?? " Unknown Error occurred while encoding BSON "
151
+ debugDescription: " Top-level \( T . self ) was not encoded as a complete document. "
179
152
)
180
153
)
181
154
}
@@ -193,7 +166,7 @@ public class BSONEncoder {
193
166
guard let value = value else {
194
167
return nil
195
168
}
196
- let encoded = try self . encode ( value)
169
+ let encoded : BSONDocument = try self . encode ( value)
197
170
return encoded == [ : ] ? nil : encoded
198
171
}
199
172
@@ -220,6 +193,41 @@ public class BSONEncoder {
220
193
public func encode< T: Encodable > ( _ values: [ T ? ] ) throws -> [ BSONDocument ? ] {
221
194
try values. map { try self . encode ( $0) }
222
195
}
196
+
197
+ /**
198
+ * Encodes the given top-level value and returns its BSON representation.
199
+ *
200
+ * - Parameter value: The value to encode.
201
+ * - Returns: A new `BSON` containing the encoded BSON data.
202
+ * - Throws: `EncodingError` if any value throws an error during encoding.
203
+ */
204
+ internal func encode< T: Encodable > ( _ value: T ) throws -> BSON {
205
+ let encoder = _BSONEncoder ( options: self . options)
206
+
207
+ do {
208
+ guard let boxedValue = try encoder. box_ ( value) else {
209
+ throw EncodingError . invalidValue (
210
+ value,
211
+ EncodingError . Context (
212
+ codingPath: [ ] ,
213
+ debugDescription: " Top-level \( T . self) did not encode any values. "
214
+ )
215
+ )
216
+ }
217
+ if let mutableDict = boxedValue as? MutableDictionary {
218
+ return . document( try mutableDict. toDocument ( ) )
219
+ }
220
+ return boxedValue. bson
221
+ } catch let error as BSONErrorProtocol {
222
+ throw EncodingError . invalidValue (
223
+ value,
224
+ EncodingError . Context (
225
+ codingPath: [ ] ,
226
+ debugDescription: error. errorDescription ?? " Unknown Error occurred while encoding BSON "
227
+ )
228
+ )
229
+ }
230
+ }
223
231
}
224
232
225
233
/// :nodoc: An internal class to implement the `Encoder` protocol.
0 commit comments