Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--exclude Sources/MongoSwift/MongoSwiftVersion.swift

# unnecessary
--disable andOperator

# put #if to the first column
--ifdef "outdent"

# 4 spaces
--indent 4

# 0..<3 vs 0 ..< 3
--ranges "no-space"

# always want explicit acl
--disable redundantExtensionACL

# always want explicit self
--self insert

# don't need semicolons
--semicolons "never"

--disable trailingCommas

# first element of collection on newline after opening brace/bracket
--wrapcollections "before-first"

# put first argument on its own line
--wraparguments "before-first"

# if x == 1 vs if 1 == x
--disable yodaConditions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one seems reasonable to leave on to me, 1 == x looks kinda weird

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yodaConditions actually enforces the 1 == x, so it's being disabled here; I agree, look weird it does. It's called a "yoda" condition because if read it sounds like you're speaking like Yoda from star wars...lol

Copy link
Contributor

@kmahar kmahar Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmao well... good for star wars fans I suppose


# the ordering doesn't match linter's
--disable specifiers
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ excluded:
- Package.swift
- Examples/*/Package.swift
- Tests/LinuxMain.swift
- SwiftFormat # this is the path we download SwiftFormat to on travis

trailing_whitespace:
ignores_comments: false
17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ jobs:
- make linuxmain SOURCERY=${PWD}/sourcery/bin/sourcery
- git diff --exit-code Tests/LinuxMain.swift

- stage: pre-tests
name: lint
os: osx
osx_image: xcode11.1
install: ./Tests/Scripts/install_dependencies.sh swiftlint && ./Tests/Scripts/install_dependencies.sh swiftformat
before_script: skip
script: ${PWD}/swiftlint/swiftlint --strict && ${PWD}/SwiftFormat/CommandLineTool/swiftformat --verbose --lint .

- stage: tests
os: osx
osx_image: xcode10.2
Expand All @@ -35,15 +43,6 @@ jobs:
script: make coverage
after_success: bash <(curl -s https://codecov.io/bash)

- stage: post-tests
name: lint
os: osx
osx_image: xcode11.1
install: ./Tests/Scripts/install_dependencies.sh swiftlint
before_script: skip
script: ${PWD}/swiftlint/swiftlint --strict


install:
- INSTALLER=Tests/Scripts/install_dependencies.sh
- ./${INSTALLER} libmongoc
Expand Down
5 changes: 3 additions & 2 deletions Examples/BugReport/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import PackageDescription
let package = Package(
name: "BugReport",
dependencies: [
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
],
targets: [
.target(
name: "BugReport",
dependencies: ["MongoSwift"])
dependencies: ["MongoSwift"]
)
]
)
2 changes: 1 addition & 1 deletion Examples/Docs/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ let package = Package(
targets: [
.target(name: "DocsExamples", dependencies: ["MongoSwift"])
]
)
)
14 changes: 9 additions & 5 deletions Examples/Docs/Sources/DocsExamples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ private func causalConsistency() throws {
// Start Causal Consistency Example 1
let s1 = try client1.startSession(options: ClientSessionOptions(causalConsistency: true))
let currentDate = Date()
var dbOptions = DatabaseOptions(readConcern: ReadConcern(.majority),
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000))
var dbOptions = DatabaseOptions(
readConcern: ReadConcern(.majority),
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000)
)
let items = client1.db("test", options: dbOptions).collection("items")
try items.updateOne(filter: ["sku": "111", "end": BSONNull()],
update: ["$set": ["end": currentDate] as Document],
session: s1)
try items.updateOne(
filter: ["sku": "111", "end": BSONNull()],
update: ["$set": ["end": currentDate] as Document],
session: s1
)
try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": currentDate], session: s1)
// End Causal Consistency Example 1

Expand Down
16 changes: 8 additions & 8 deletions Examples/Perfect/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import PackageDescription

let package = Package(
name: "PerfectExample",
dependencies: [
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")),
],
targets: [
.target(name: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"])
]
name: "PerfectExample",
dependencies: [
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")),
],
targets: [
.target(name: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"])
]
)
23 changes: 12 additions & 11 deletions Examples/Perfect/Sources/PerfectExample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import PerfectHTTPServer

/// A Codable type that matches the data in our home.kittens collection.
private struct Kitten: Codable {
var name: String
var color: String
var name: String
var color: String
}

/// A single collection with type `Kitten`. This allows us to directly retrieve instances of
Expand All @@ -15,14 +15,15 @@ private let collection = try MongoClient().db("home").collection("kittens", with

private var routes = Routes()
routes.add(method: .get, uri: "/kittens") { _, response in
response.setHeader(.contentType, value: "application/json")
do {
let kittens = try collection.find()
let json = try JSONEncoder().encode(Array(kittens))
response.setBody(bytes: Array(json))
} catch {
print("error: \(error)")
}
response.completed()
response.setHeader(.contentType, value: "application/json")
do {
let kittens = try collection.find()
let json = try JSONEncoder().encode(Array(kittens))
response.setBody(bytes: Array(json))
} catch {
print("error: \(error)")
}
response.completed()
}

try HTTPServer.launch(name: "localhost", port: 8080, routes: routes)
4 changes: 4 additions & 0 deletions Guides/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ To regenerate the files, run `make documentation` from the project's root direct
## Linting and Style
We use [SwiftLint](https://github.com/realm/SwiftLint#using-homebrew) for linting. You can see our configuration in the `.swiftlint.yml` file in the project's root directory. Run `swiftlint` in the `/Sources` directory to lint all of our files. Running `swiftlint autocorrect` will correct some types of violations.

We use [SwiftFormat](https://github.com/nicklockwood/SwiftFormat) for formatting the code. You can see our configuration in the `.swiftformat` file in the project's root directory. Our linter config contains a superset of the rules that our formatter does, so some manual tweaking may be necessary to satisfy both once the formatter is run (e.g. line length enforcement). Most of the time, the formatter should put the code into a format that passes the linter.

To pass all the formatting stages of our testing matrix, both `swiftlint --strict` and `swiftformat --lint .` must finish successfully.

For style guidance, look at Swift's [API design guidelines](https://swift.org/documentation/api-design-guidelines/) and Google's [Swift Style Guide](https://google.github.io/swift/).

### Sublime Text Setup
Expand Down
111 changes: 68 additions & 43 deletions Sources/MongoSwift/APM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,108 +348,133 @@ public struct ServerHeartbeatFailedEvent: MongoEvent, InitializableFromOpaquePoi

/// A callback that will be set for "command started" events if the user enables command monitoring.
private func commandStarted(_event: OpaquePointer?) {
postNotification(type: CommandStartedEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_started_get_context)
postNotification(
type: CommandStartedEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_started_get_context
)
}

/// A callback that will be set for "command succeeded" events if the user enables command monitoring.
private func commandSucceeded(_event: OpaquePointer?) {
postNotification(type: CommandSucceededEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_succeeded_get_context)
postNotification(
type: CommandSucceededEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_succeeded_get_context
)
}

/// A callback that will be set for "command failed" events if the user enables command monitoring.
private func commandFailed(_event: OpaquePointer?) {
postNotification(type: CommandFailedEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_failed_get_context)
postNotification(
type: CommandFailedEvent.self,
_event: _event,
contextFunc: mongoc_apm_command_failed_get_context
)
}

/// A callback that will be set for "server description changed" events if the user enables server monitoring.
private func serverDescriptionChanged(_event: OpaquePointer?) {
postNotification(type: ServerDescriptionChangedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_changed_get_context)
postNotification(
type: ServerDescriptionChangedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_changed_get_context
)
}

/// A callback that will be set for "server opening" events if the user enables server monitoring.
private func serverOpening(_event: OpaquePointer?) {
postNotification(type: ServerOpeningEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_opening_get_context)
postNotification(
type: ServerOpeningEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_opening_get_context
)
}

/// A callback that will be set for "server closed" events if the user enables server monitoring.
private func serverClosed(_event: OpaquePointer?) {
postNotification(type: ServerClosedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_closed_get_context)
postNotification(
type: ServerClosedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_closed_get_context
)
}

/// A callback that will be set for "topology description changed" events if the user enables server monitoring.
private func topologyDescriptionChanged(_event: OpaquePointer?) {
postNotification(type: TopologyDescriptionChangedEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_changed_get_context)
postNotification(
type: TopologyDescriptionChangedEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_changed_get_context
)
}

/// A callback that will be set for "topology opening" events if the user enables server monitoring.
private func topologyOpening(_event: OpaquePointer?) {
postNotification(type: TopologyOpeningEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_opening_get_context)
postNotification(
type: TopologyOpeningEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_opening_get_context
)
}

/// A callback that will be set for "topology closed" events if the user enables server monitoring.
private func topologyClosed(_event: OpaquePointer?) {
postNotification(type: TopologyClosedEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_closed_get_context)
postNotification(
type: TopologyClosedEvent.self,
_event: _event,
contextFunc: mongoc_apm_topology_closed_get_context
)
}

/// A callback that will be set for "server heartbeat started" events if the user enables server monitoring.
private func serverHeartbeatStarted(_event: OpaquePointer?) {
postNotification(type: ServerHeartbeatStartedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_started_get_context)
postNotification(
type: ServerHeartbeatStartedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_started_get_context
)
}

/// A callback that will be set for "server heartbeat succeeded" events if the user enables server monitoring.
private func serverHeartbeatSucceeded(_event: OpaquePointer?) {
postNotification(type: ServerHeartbeatSucceededEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_succeeded_get_context)
postNotification(
type: ServerHeartbeatSucceededEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_succeeded_get_context
)
}

/// A callback that will be set for "server heartbeat failed" events if the user enables server monitoring.
private func serverHeartbeatFailed(_event: OpaquePointer?) {
postNotification(type: ServerHeartbeatFailedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_failed_get_context)
postNotification(
type: ServerHeartbeatFailedEvent.self,
_event: _event,
contextFunc: mongoc_apm_server_heartbeat_failed_get_context
)
}

/// Posts a Notification with the specified name, containing an event of type T generated using the provided _event
/// and context function.
private func postNotification<T: MongoEvent>(type: T.Type,
_event: OpaquePointer?,
contextFunc: (OpaquePointer) -> UnsafeMutableRawPointer?
) where T: InitializableFromOpaquePointer {
private func postNotification<T: MongoEvent>(
type: T.Type,
_event: OpaquePointer?,
contextFunc: (OpaquePointer) -> UnsafeMutableRawPointer?
) where T: InitializableFromOpaquePointer {
guard let event = _event else {
fatalError("Missing event pointer for \(type)")
}

let eventStruct = type.init(event)

// TODO SWIFT-524: remove workaround for CDRIVER-3256
// TODO: SWIFT-524: remove workaround for CDRIVER-3256
if let tdChanged = eventStruct as? TopologyDescriptionChangedEvent,
tdChanged.previousDescription == tdChanged.newDescription {
tdChanged.previousDescription == tdChanged.newDescription {
return
}

if let sdChanged = eventStruct as? ServerDescriptionChangedEvent,
sdChanged.previousDescription == sdChanged.newDescription {
sdChanged.previousDescription == sdChanged.newDescription {
return
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/MongoSwift/BSON/BSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,11 @@ extension BSON: Codable {
}

throw DecodingError.typeMismatch(
BSON.self,
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Encountered a value that could not be decoded to any BSON type")
BSON.self,
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Encountered a value that could not be decoded to any BSON type"
)
)
}
}
Expand Down
Loading