Skip to content

Commit 8c9a4b4

Browse files
authored
Introduce SwiftFormat (#339)
1 parent 4505c86 commit 8c9a4b4

File tree

79 files changed

+2515
-1834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2515
-1834
lines changed

.swiftformat

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--exclude Sources/MongoSwift/MongoSwiftVersion.swift
2+
3+
# unnecessary
4+
--disable andOperator
5+
6+
# put #if to the first column
7+
--ifdef "outdent"
8+
9+
# 4 spaces
10+
--indent 4
11+
12+
# 0..<3 vs 0 ..< 3
13+
--ranges "no-space"
14+
15+
# always want explicit acl
16+
--disable redundantExtensionACL
17+
18+
# always want explicit self
19+
--self insert
20+
21+
# don't need semicolons
22+
--semicolons "never"
23+
24+
--disable trailingCommas
25+
26+
# first element of collection on newline after opening brace/bracket
27+
--wrapcollections "before-first"
28+
29+
# put first argument on its own line
30+
--wraparguments "before-first"
31+
32+
# if x == 1 vs if 1 == x
33+
--disable yodaConditions
34+
35+
# the ordering doesn't match linter's
36+
--disable specifiers

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ excluded:
5353
- Package.swift
5454
- Examples/*/Package.swift
5555
- Tests/LinuxMain.swift
56+
- SwiftFormat # this is the path we download SwiftFormat to on travis
5657

5758
trailing_whitespace:
5859
ignores_comments: false

.travis.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ jobs:
1919
- make linuxmain SOURCERY=${PWD}/sourcery/bin/sourcery
2020
- git diff --exit-code Tests/LinuxMain.swift
2121

22+
- stage: pre-tests
23+
name: lint
24+
os: osx
25+
osx_image: xcode11.1
26+
install: ./Tests/Scripts/install_dependencies.sh swiftlint && ./Tests/Scripts/install_dependencies.sh swiftformat
27+
before_script: skip
28+
script: ${PWD}/swiftlint/swiftlint --strict && ${PWD}/SwiftFormat/CommandLineTool/swiftformat --verbose --lint .
29+
2230
- stage: tests
2331
os: osx
2432
osx_image: xcode10.2
@@ -35,15 +43,6 @@ jobs:
3543
script: make coverage
3644
after_success: bash <(curl -s https://codecov.io/bash)
3745

38-
- stage: post-tests
39-
name: lint
40-
os: osx
41-
osx_image: xcode11.1
42-
install: ./Tests/Scripts/install_dependencies.sh swiftlint
43-
before_script: skip
44-
script: ${PWD}/swiftlint/swiftlint --strict
45-
46-
4746
install:
4847
- INSTALLER=Tests/Scripts/install_dependencies.sh
4948
- ./${INSTALLER} libmongoc

Examples/BugReport/Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import PackageDescription
44
let package = Package(
55
name: "BugReport",
66
dependencies: [
7-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
7+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
88
],
99
targets: [
1010
.target(
1111
name: "BugReport",
12-
dependencies: ["MongoSwift"])
12+
dependencies: ["MongoSwift"]
13+
)
1314
]
1415
)

Examples/Docs/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ let package = Package(
99
targets: [
1010
.target(name: "DocsExamples", dependencies: ["MongoSwift"])
1111
]
12-
)
12+
)

Examples/Docs/Sources/DocsExamples/main.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ private func causalConsistency() throws {
1111
// Start Causal Consistency Example 1
1212
let s1 = try client1.startSession(options: ClientSessionOptions(causalConsistency: true))
1313
let currentDate = Date()
14-
var dbOptions = DatabaseOptions(readConcern: ReadConcern(.majority),
15-
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000))
14+
var dbOptions = DatabaseOptions(
15+
readConcern: ReadConcern(.majority),
16+
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000)
17+
)
1618
let items = client1.db("test", options: dbOptions).collection("items")
17-
try items.updateOne(filter: ["sku": "111", "end": BSONNull()],
18-
update: ["$set": ["end": currentDate] as Document],
19-
session: s1)
19+
try items.updateOne(
20+
filter: ["sku": "111", "end": BSONNull()],
21+
update: ["$set": ["end": currentDate] as Document],
22+
session: s1
23+
)
2024
try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": currentDate], session: s1)
2125
// End Causal Consistency Example 1
2226

Examples/Perfect/Package.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import PackageDescription
33

44
let package = Package(
5-
name: "PerfectExample",
6-
dependencies: [
7-
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
8-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")),
9-
],
10-
targets: [
11-
.target(name: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"])
12-
]
5+
name: "PerfectExample",
6+
dependencies: [
7+
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
8+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")),
9+
],
10+
targets: [
11+
.target(name: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"])
12+
]
1313
)

Examples/Perfect/Sources/PerfectExample/main.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import PerfectHTTPServer
55

66
/// A Codable type that matches the data in our home.kittens collection.
77
private struct Kitten: Codable {
8-
var name: String
9-
var color: String
8+
var name: String
9+
var color: String
1010
}
1111

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

1616
private var routes = Routes()
1717
routes.add(method: .get, uri: "/kittens") { _, response in
18-
response.setHeader(.contentType, value: "application/json")
19-
do {
20-
let kittens = try collection.find()
21-
let json = try JSONEncoder().encode(Array(kittens))
22-
response.setBody(bytes: Array(json))
23-
} catch {
24-
print("error: \(error)")
25-
}
26-
response.completed()
18+
response.setHeader(.contentType, value: "application/json")
19+
do {
20+
let kittens = try collection.find()
21+
let json = try JSONEncoder().encode(Array(kittens))
22+
response.setBody(bytes: Array(json))
23+
} catch {
24+
print("error: \(error)")
25+
}
26+
response.completed()
2727
}
28+
2829
try HTTPServer.launch(name: "localhost", port: 8080, routes: routes)

Guides/Development.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ To regenerate the files, run `make documentation` from the project's root direct
7878
## Linting and Style
7979
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.
8080

81+
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.
82+
83+
To pass all the formatting stages of our testing matrix, both `swiftlint --strict` and `swiftformat --lint .` must finish successfully.
84+
8185
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/).
8286

8387
### Sublime Text Setup

Sources/MongoSwift/APM.swift

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -348,108 +348,133 @@ public struct ServerHeartbeatFailedEvent: MongoEvent, InitializableFromOpaquePoi
348348

349349
/// A callback that will be set for "command started" events if the user enables command monitoring.
350350
private func commandStarted(_event: OpaquePointer?) {
351-
postNotification(type: CommandStartedEvent.self,
352-
_event: _event,
353-
contextFunc: mongoc_apm_command_started_get_context)
351+
postNotification(
352+
type: CommandStartedEvent.self,
353+
_event: _event,
354+
contextFunc: mongoc_apm_command_started_get_context
355+
)
354356
}
355357

356358
/// A callback that will be set for "command succeeded" events if the user enables command monitoring.
357359
private func commandSucceeded(_event: OpaquePointer?) {
358-
postNotification(type: CommandSucceededEvent.self,
359-
_event: _event,
360-
contextFunc: mongoc_apm_command_succeeded_get_context)
360+
postNotification(
361+
type: CommandSucceededEvent.self,
362+
_event: _event,
363+
contextFunc: mongoc_apm_command_succeeded_get_context
364+
)
361365
}
362366

363367
/// A callback that will be set for "command failed" events if the user enables command monitoring.
364368
private func commandFailed(_event: OpaquePointer?) {
365-
postNotification(type: CommandFailedEvent.self,
366-
_event: _event,
367-
contextFunc: mongoc_apm_command_failed_get_context)
369+
postNotification(
370+
type: CommandFailedEvent.self,
371+
_event: _event,
372+
contextFunc: mongoc_apm_command_failed_get_context
373+
)
368374
}
369375

370376
/// A callback that will be set for "server description changed" events if the user enables server monitoring.
371377
private func serverDescriptionChanged(_event: OpaquePointer?) {
372-
postNotification(type: ServerDescriptionChangedEvent.self,
373-
_event: _event,
374-
contextFunc: mongoc_apm_server_changed_get_context)
378+
postNotification(
379+
type: ServerDescriptionChangedEvent.self,
380+
_event: _event,
381+
contextFunc: mongoc_apm_server_changed_get_context
382+
)
375383
}
376384

377385
/// A callback that will be set for "server opening" events if the user enables server monitoring.
378386
private func serverOpening(_event: OpaquePointer?) {
379-
postNotification(type: ServerOpeningEvent.self,
380-
_event: _event,
381-
contextFunc: mongoc_apm_server_opening_get_context)
387+
postNotification(
388+
type: ServerOpeningEvent.self,
389+
_event: _event,
390+
contextFunc: mongoc_apm_server_opening_get_context
391+
)
382392
}
383393

384394
/// A callback that will be set for "server closed" events if the user enables server monitoring.
385395
private func serverClosed(_event: OpaquePointer?) {
386-
postNotification(type: ServerClosedEvent.self,
387-
_event: _event,
388-
contextFunc: mongoc_apm_server_closed_get_context)
396+
postNotification(
397+
type: ServerClosedEvent.self,
398+
_event: _event,
399+
contextFunc: mongoc_apm_server_closed_get_context
400+
)
389401
}
390402

391403
/// A callback that will be set for "topology description changed" events if the user enables server monitoring.
392404
private func topologyDescriptionChanged(_event: OpaquePointer?) {
393-
postNotification(type: TopologyDescriptionChangedEvent.self,
394-
_event: _event,
395-
contextFunc: mongoc_apm_topology_changed_get_context)
405+
postNotification(
406+
type: TopologyDescriptionChangedEvent.self,
407+
_event: _event,
408+
contextFunc: mongoc_apm_topology_changed_get_context
409+
)
396410
}
397411

398412
/// A callback that will be set for "topology opening" events if the user enables server monitoring.
399413
private func topologyOpening(_event: OpaquePointer?) {
400-
postNotification(type: TopologyOpeningEvent.self,
401-
_event: _event,
402-
contextFunc: mongoc_apm_topology_opening_get_context)
414+
postNotification(
415+
type: TopologyOpeningEvent.self,
416+
_event: _event,
417+
contextFunc: mongoc_apm_topology_opening_get_context
418+
)
403419
}
404420

405421
/// A callback that will be set for "topology closed" events if the user enables server monitoring.
406422
private func topologyClosed(_event: OpaquePointer?) {
407-
postNotification(type: TopologyClosedEvent.self,
408-
_event: _event,
409-
contextFunc: mongoc_apm_topology_closed_get_context)
423+
postNotification(
424+
type: TopologyClosedEvent.self,
425+
_event: _event,
426+
contextFunc: mongoc_apm_topology_closed_get_context
427+
)
410428
}
411429

412430
/// A callback that will be set for "server heartbeat started" events if the user enables server monitoring.
413431
private func serverHeartbeatStarted(_event: OpaquePointer?) {
414-
postNotification(type: ServerHeartbeatStartedEvent.self,
415-
_event: _event,
416-
contextFunc: mongoc_apm_server_heartbeat_started_get_context)
432+
postNotification(
433+
type: ServerHeartbeatStartedEvent.self,
434+
_event: _event,
435+
contextFunc: mongoc_apm_server_heartbeat_started_get_context
436+
)
417437
}
418438

419439
/// A callback that will be set for "server heartbeat succeeded" events if the user enables server monitoring.
420440
private func serverHeartbeatSucceeded(_event: OpaquePointer?) {
421-
postNotification(type: ServerHeartbeatSucceededEvent.self,
422-
_event: _event,
423-
contextFunc: mongoc_apm_server_heartbeat_succeeded_get_context)
441+
postNotification(
442+
type: ServerHeartbeatSucceededEvent.self,
443+
_event: _event,
444+
contextFunc: mongoc_apm_server_heartbeat_succeeded_get_context
445+
)
424446
}
425447

426448
/// A callback that will be set for "server heartbeat failed" events if the user enables server monitoring.
427449
private func serverHeartbeatFailed(_event: OpaquePointer?) {
428-
postNotification(type: ServerHeartbeatFailedEvent.self,
429-
_event: _event,
430-
contextFunc: mongoc_apm_server_heartbeat_failed_get_context)
450+
postNotification(
451+
type: ServerHeartbeatFailedEvent.self,
452+
_event: _event,
453+
contextFunc: mongoc_apm_server_heartbeat_failed_get_context
454+
)
431455
}
432456

433457
/// Posts a Notification with the specified name, containing an event of type T generated using the provided _event
434458
/// and context function.
435-
private func postNotification<T: MongoEvent>(type: T.Type,
436-
_event: OpaquePointer?,
437-
contextFunc: (OpaquePointer) -> UnsafeMutableRawPointer?
438-
) where T: InitializableFromOpaquePointer {
459+
private func postNotification<T: MongoEvent>(
460+
type: T.Type,
461+
_event: OpaquePointer?,
462+
contextFunc: (OpaquePointer) -> UnsafeMutableRawPointer?
463+
) where T: InitializableFromOpaquePointer {
439464
guard let event = _event else {
440465
fatalError("Missing event pointer for \(type)")
441466
}
442467

443468
let eventStruct = type.init(event)
444469

445-
// TODO SWIFT-524: remove workaround for CDRIVER-3256
470+
// TODO: SWIFT-524: remove workaround for CDRIVER-3256
446471
if let tdChanged = eventStruct as? TopologyDescriptionChangedEvent,
447-
tdChanged.previousDescription == tdChanged.newDescription {
472+
tdChanged.previousDescription == tdChanged.newDescription {
448473
return
449474
}
450475

451476
if let sdChanged = eventStruct as? ServerDescriptionChangedEvent,
452-
sdChanged.previousDescription == sdChanged.newDescription {
477+
sdChanged.previousDescription == sdChanged.newDescription {
453478
return
454479
}
455480

0 commit comments

Comments
 (0)