Skip to content

Commit

Permalink
Merge pull request #128 from tayloraswift/0.16
Browse files Browse the repository at this point in the history
0.16
  • Loading branch information
tayloraswift authored May 17, 2024
2 parents b20c0b4 + 377202e commit bc04ba8
Show file tree
Hide file tree
Showing 33 changed files with 715 additions and 402 deletions.
4 changes: 2 additions & 2 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Dianna <kelvin13ma@gmail.com>
Dianna <mdianna1989@gmail.com>
Dianna Ma <kelvin13ma@gmail.com>
Dianna Ma <mdianna1989@gmail.com>
359 changes: 202 additions & 157 deletions LICENSE

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
the `swift-mongodb` package was created by Dianna Ma (@taylorswift), Joannis Orlandos, and Robbert Brandsma.

you are welcome to contribute to this project at:

https://github.com/tayloraswift/swift-mongodb

we do not maintain any other mirrors of this repository, and such
forks of this repository may not carry the most up-to-date code.

contributors:

1. Dianna Ma (@taylorswift, 2021–24)
2. Joannis Orlandos (2016–22)
3. Robbert Brandsma (2016–22)
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
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ print(configuration)
// ...
```

## toolchain requirement

This package requires Swift 5.10 or greater.


## license and acknowledgements

This library is Apache 2.0 licensed. It originally began as a re-write of [*MongoKitten*](https://github.com/orlandos-nl/MongoKitten) by [Joannis Orlandos](https://github.com/Joannis) and [Robbert Brandsma](https://github.com/Obbut).


## external dependencies

I have verified that all products depended-upon by this package are Foundation-free when compiled for a linux target. Note that some package dependencies do vend products that import Foundation, but swift links binaries at the product level, and this library does not depend on any such products.
Expand Down Expand Up @@ -73,19 +83,3 @@ Other packages:
Rationale: networking.

> Note: This library depends on the `NIOSSL` product from `swift-nio-ssl`, which imports Foundation on Apple platforms only. `NIOSSL` is Foundation-free on all other platforms.
## toolchain requirement

This package requires swift 5.10 or greater.

## acknowledgements

This library originally started out as a re-write of [Orlandos](https://orlandos.nl/)’s *MongoKitten*; accordingly the `MongoDriver` module retains *MongoKitten*’s original [MIT-license](https://github.com/orlandos-nl/MongoKitten/blob/master/7.0/LICENSE.md).

The [official MongoDB C driver](https://github.com/mongodb/mongo-swift-driver) also served as prior art for this module.

## license

The `MongoDriver` module is MIT-licensed.

The other modules are available under the MPL 2.0 license. This license was chosen as an organizational default, and is not ideological. Please [reach out](https://github.com/tayloraswift/swift-mongodb/discussions) if you have a use-case that requires a more-permissive license!
31 changes: 8 additions & 23 deletions Snippets/ExampleProjectionDocuments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,15 @@ func ExampleProjectionDocuments()
{
let _:Mongo.ProjectionDocument = .init
{
$0["expression"] = .expr
$0["expression"] { $0[.abs] = "$field" }
$0["key1"] = true
$0["key2"] { $0[.literal] = 1 }
$0["a"] { $0[.slice] = 1 }
$0["b"] { $0[.slice] = (1, 1) }
$0["c"] { $0[.meta] = .indexKey }
$0["d"]
{
$0[.abs] = "$field"
}
$0["key1"] = 1
$0["key2"] = .expr
{
$0[.literal] = 1
}
$0["a"] = .init
{
$0[.slice] = 1
}
$0["b"] = .init
{
$0[.slice] = (1, 1)
}
$0["c"] = .init
{
$0[.meta] = .indexKey
}
$0["d"] = .init
{
$0[.first] = .init
$0[.first]
{
$0[.or]
{
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extension Mongo
{
public
protocol PredicateEncodable
{
func encode(to predicate:inout PredicateEncoder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MongoABI
extension Mongo
{
@frozen public
struct ProjectionDocument:Mongo.EncodableDocument, Sendable
struct ProjectionDocument:Sendable
{
public
var bson:BSON.Document
Expand All @@ -16,35 +16,8 @@ extension Mongo
}
}
}
extension Mongo.ProjectionDocument
extension Mongo.ProjectionDocument:Mongo.EncodableDocument
{
/// Encodes a ``ProjectionOperator``.
///
/// This does not require [`@_disfavoredOverload`](), because
/// ``ProjectionOperator`` has no subscripts that accept string
/// literals, so it will never conflict with ``BSON.Document``.
@inlinable public
subscript(path:Mongo.AnyKeyPath) -> Mongo.ProjectionOperator?
{
get
{
nil
}
set(value)
{
value?.encode(to: &self.bson[with: path.stem])
}
}
@inlinable public
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable? where Encodable:BSONEncodable
{
get
{
nil
}
set(value)
{
value?.encode(to: &self.bson[with: path.stem])
}
}
public
typealias Encoder = Mongo.ProjectionEncoder
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extension Mongo
{
public
protocol ProjectionEncodable
{
func encode(to projection:inout ProjectionEncoder)
}
}
Loading

0 comments on commit bc04ba8

Please sign in to comment.