Skip to content

Commit eb5195c

Browse files
committed
Rename ConfigReader.withSnapshot(_:) to ConfigReader.snapshot()
1 parent 5a82370 commit eb5195c

13 files changed

+100
-73
lines changed

Package.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ for target in package.targets {
188188
// https://docs.swift.org/compiler/documentation/diagnostics/nonisolated-nonsending-by-default/
189189
settings.append(.enableUpcomingFeature("NonisolatedNonsendingByDefault"))
190190

191-
settings.append(.enableExperimentalFeature("AvailabilityMacro=Configuration 1.0:macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0"))
191+
settings.append(
192+
.enableExperimentalFeature(
193+
"AvailabilityMacro=Configuration 1.0:macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0"
194+
)
195+
)
192196

193197
if enableAllCIFlags {
194198
// Ensure all public types are explicitly annotated as Sendable or not Sendable.

Sources/Configuration/ConfigSnapshotReader.swift

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ import Synchronization
2222
///
2323
/// ## Usage
2424
///
25-
/// Get a ``ConfigSnapshotReader`` from a ``ConfigReader`` by using ``ConfigReader/withSnapshot(_:)``
26-
/// to retrieve a snapshot. The values of the snapshot are guaranteed to be from the same point in time:
25+
/// Get a ``ConfigSnapshotReader`` from a ``ConfigReader`` by using ``ConfigReader/snapshot()``
26+
/// to retrieve a snapshot. All values in the snapshot are guaranteed to be from the same point in time:
2727
/// ```swift
2828
/// // Get a snapshot from a ConfigReader
2929
/// let config = ConfigReader(provider: EnvironmentVariablesProvider())
30-
/// let result = config.withSnapshot { snapshot in
31-
/// // Use snapshot to read config values
32-
/// let cert = snapshot.string(forKey: "cert")
33-
/// let privateKey = snapshot.string(forKey: "privateKey")
34-
/// // Ensures that both values are coming from the same
35-
/// // underlying snapshot and that a provider didn't change
36-
/// // its internal state between the two `string(...)` calls.
37-
/// return MyCert(cert: cert, privateKey: privateKey)
38-
/// }
30+
/// let snapshot = config.snapshot()
31+
/// // Use snapshot to read config values
32+
/// let cert = snapshot.string(forKey: "cert")
33+
/// let privateKey = snapshot.string(forKey: "privateKey")
34+
/// // Ensures that both values are coming from the same
35+
/// // underlying snapshot and that a provider didn't change
36+
/// // its internal state between the two `string(...)` calls.
37+
/// let identity = MyIdentity(cert: cert, privateKey: privateKey)
3938
/// ```
4039
///
4140
/// Or you can watch for snapshot updates using the ``ConfigReader/watchSnapshot(fileID:line:updatesHandler:)``:
@@ -297,29 +296,23 @@ public struct ConfigSnapshotReader: Sendable {
297296

298297
@available(Configuration 1.0, *)
299298
extension ConfigReader {
300-
/// Provides a snapshot of the current configuration state and passes it to the provided closure.
299+
/// Returns a snapshot of the current configuration state.
301300
///
302-
/// This method creates a snapshot of the current configuration state and passes it to the
303-
/// provided closure. The snapshot reader provides read-only access to the configuration's state
301+
/// The snapshot reader provides read-only access to the configuration's state
304302
/// at the time the method was called.
305303
///
306304
/// ```swift
307-
/// let result = config.withSnapshot { snapshot in
308-
/// // Use snapshot to read config values
309-
/// let cert = snapshot.string(forKey: "cert")
310-
/// let privateKey = snapshot.string(forKey: "privateKey")
311-
/// // Ensures that both values are coming from the same underlying snapshot and that a provider
312-
/// // didn't change its internal state between the two `string(...)` calls.
313-
/// return MyCert(cert: cert, privateKey: privateKey)
314-
/// }
305+
/// let snapshot = config.snapshot()
306+
/// // Use snapshot to read config values
307+
/// let cert = snapshot.string(forKey: "cert")
308+
/// let privateKey = snapshot.string(forKey: "privateKey")
309+
/// // Ensures that both values are coming from the same underlying snapshot and that a provider
310+
/// // didn't change its internal state between the two `string(...)` calls.
311+
/// let identity = MyIdentity(cert: cert, privateKey: privateKey)
315312
/// ```
316313
///
317-
/// - Parameter body: A closure that takes a `ConfigSnapshotReader` and returns a value.
318-
/// - Returns: The value returned by the closure.
319-
/// - Throws: Rethrows any errors thrown by the provided closure.
320-
public func withSnapshot<Failure: Error, Return>(
321-
_ body: (ConfigSnapshotReader) throws(Failure) -> Return
322-
) throws(Failure) -> Return {
314+
/// - Returns: The snapshot.
315+
public func snapshot() -> ConfigSnapshotReader {
323316
let multiSnapshot = provider.snapshot()
324317
let snapshotReader = ConfigSnapshotReader(
325318
keyPrefix: keyPrefix,
@@ -329,7 +322,7 @@ extension ConfigReader {
329322
accessReporter: accessReporter
330323
)
331324
)
332-
return try body(snapshotReader)
325+
return snapshotReader
333326
}
334327

335328
/// Watches the configuration for changes.

Sources/Configuration/Documentation.docc/Documentation.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,19 +358,18 @@ Read <doc:Handling-secrets-correctly> for guidance on best practices for secrets
358358
#### Consistent snapshots
359359

360360
Retrieve related values from a consistent snapshot using ``ConfigSnapshotReader``, which you
361-
get from calling ``ConfigReader/withSnapshot(_:)``.
361+
get by calling ``ConfigReader/snapshot()``.
362362

363363
This ensures that multiple values are read from a single snapshot inside each provider, even when using
364364
providers that update their internal values.
365365
For example by downloading new data periodically:
366366

367367
```swift
368368
let config = /* a reader with one or more providers that change values over time */
369-
try config.withSnapshot { snapshot in
370-
let certificate = try snapshot.requiredString(forKey: "mtls.certificate")
371-
let privateKey = try snapshot.requiredString(forKey: "mtls.privateKey", isSecret: true)
372-
// `certificate` and `privateKey` are guaranteed to come from the same snapshot in the provider
373-
}
369+
let snapshot = config.snapshot()
370+
let certificate = try snapshot.requiredString(forKey: "mtls.certificate")
371+
let privateKey = try snapshot.requiredString(forKey: "mtls.privateKey", isSecret: true)
372+
// `certificate` and `privateKey` are guaranteed to come from the same snapshot in the provider
374373
```
375374

376375
#### Custom key syntax

Sources/Configuration/Documentation.docc/Reference/ConfigReader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- ``ConfigReader/scoped(to:keyDecoderOverride:)``
1313

1414
### Reading from a snapshot
15-
- ``ConfigReader/withSnapshot(_:)``
15+
- ``ConfigReader/snapshot()``
1616
- ``ConfigReader/watchSnapshot(fileID:line:updatesHandler:)``
1717

1818
- <doc:ConfigReader-Get>

Sources/Configuration/Documentation.docc/Reference/ConfigSnapshotReader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Topics
44

55
### Creating a snapshot
6-
- ``ConfigReader/withSnapshot(_:)``
6+
- ``ConfigReader/snapshot()``
77
- ``ConfigReader/watchSnapshot(fileID:line:updatesHandler:)``
88

99
### Namespacing

Tests/ConfigurationTests/ConfigReaderTests/ConfigSnapshotReaderMethodTestsGet1.swift

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
3232
@Test func get() async throws {
3333
let config = ConfigReaderTests.config
3434

35-
try config.withSnapshot { snapshot in
35+
do {
36+
let snapshot = config.snapshot()
37+
3638
// Optional - success
3739
#expect(snapshot.string(forKey: ConfigKey(["string"])) == Defaults.string)
3840
#expect(snapshot.string(forKey: "string") == Defaults.string)
@@ -78,7 +80,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
7880
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredString(forKey: ConfigKey(["failure"])) }
7981
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredString(forKey: "failure") }
8082
}
81-
try config.withSnapshot { snapshot in
83+
do {
84+
let snapshot = config.snapshot()
85+
8286
// Optional - success
8387
#expect(snapshot.int(forKey: ConfigKey(["int"])) == Defaults.int)
8488
#expect(snapshot.int(forKey: "int") == Defaults.int)
@@ -119,7 +123,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
119123
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredInt(forKey: ConfigKey(["failure"])) }
120124
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredInt(forKey: "failure") }
121125
}
122-
try config.withSnapshot { snapshot in
126+
do {
127+
let snapshot = config.snapshot()
128+
123129
// Optional - success
124130
#expect(snapshot.double(forKey: ConfigKey(["double"])) == Defaults.double)
125131
#expect(snapshot.double(forKey: "double") == Defaults.double)
@@ -165,7 +171,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
165171
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredDouble(forKey: ConfigKey(["failure"])) }
166172
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredDouble(forKey: "failure") }
167173
}
168-
try config.withSnapshot { snapshot in
174+
do {
175+
let snapshot = config.snapshot()
176+
169177
// Optional - success
170178
#expect(snapshot.bool(forKey: ConfigKey(["bool"])) == Defaults.bool)
171179
#expect(snapshot.bool(forKey: "bool") == Defaults.bool)
@@ -206,7 +214,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
206214
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredBool(forKey: ConfigKey(["failure"])) }
207215
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredBool(forKey: "failure") }
208216
}
209-
try config.withSnapshot { snapshot in
217+
do {
218+
let snapshot = config.snapshot()
219+
210220
// Optional - success
211221
#expect(snapshot.bytes(forKey: ConfigKey(["bytes"])) == Defaults.bytes)
212222
#expect(snapshot.bytes(forKey: "bytes") == Defaults.bytes)
@@ -249,7 +259,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
249259
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredBytes(forKey: ConfigKey(["failure"])) }
250260
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredBytes(forKey: "failure") }
251261
}
252-
try config.withSnapshot { snapshot in
262+
do {
263+
let snapshot = config.snapshot()
264+
253265
// Optional - success
254266
#expect(snapshot.stringArray(forKey: ConfigKey(["stringArray"])) == Defaults.stringArray)
255267
#expect(snapshot.stringArray(forKey: "stringArray") == Defaults.stringArray)
@@ -310,7 +322,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
310322
}
311323
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredStringArray(forKey: "failure") }
312324
}
313-
try config.withSnapshot { snapshot in
325+
do {
326+
let snapshot = config.snapshot()
327+
314328
// Optional - success
315329
#expect(snapshot.intArray(forKey: ConfigKey(["intArray"])) == Defaults.intArray)
316330
#expect(snapshot.intArray(forKey: "intArray") == Defaults.intArray)
@@ -363,7 +377,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
363377
}
364378
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredIntArray(forKey: "failure") }
365379
}
366-
try config.withSnapshot { snapshot in
380+
do {
381+
let snapshot = config.snapshot()
382+
367383
// Optional - success
368384
#expect(snapshot.doubleArray(forKey: ConfigKey(["doubleArray"])) == Defaults.doubleArray)
369385
#expect(snapshot.doubleArray(forKey: "doubleArray") == Defaults.doubleArray)
@@ -424,7 +440,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
424440
}
425441
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredDoubleArray(forKey: "failure") }
426442
}
427-
try config.withSnapshot { snapshot in
443+
do {
444+
let snapshot = config.snapshot()
445+
428446
// Optional - success
429447
#expect(snapshot.boolArray(forKey: ConfigKey(["boolArray"])) == Defaults.boolArray)
430448
#expect(snapshot.boolArray(forKey: "boolArray") == Defaults.boolArray)
@@ -479,7 +497,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
479497
}
480498
#expect(throws: TestProvider.TestError.self) { try snapshot.requiredBoolArray(forKey: "failure") }
481499
}
482-
try config.withSnapshot { snapshot in
500+
do {
501+
let snapshot = config.snapshot()
502+
483503
// Optional - success
484504
#expect(snapshot.byteChunkArray(forKey: ConfigKey(["byteChunkArray"])) == Defaults.byteChunkArray)
485505
#expect(snapshot.byteChunkArray(forKey: "byteChunkArray") == Defaults.byteChunkArray)

Tests/ConfigurationTests/ConfigReaderTests/ConfigSnapshotReaderMethodTestsGet1.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ struct ConfigSnapshotReaderMethodTestsGet1 {
3434
% for primitive_type in primitive_types:
3535
% name = primitive_type["name"]
3636
% lower_name = lower_first(name)
37-
try config.withSnapshot { snapshot in
37+
do {
38+
let snapshot = config.snapshot()
39+
3840
// Optional - success
3941
#expect(snapshot.${lower_name}(forKey: ConfigKey(["${lower_name}"])) == Defaults.${lower_name})
4042
#expect(snapshot.${lower_name}(forKey: "${lower_name}") == Defaults.${lower_name})

Tests/ConfigurationTests/ConfigReaderTests/ConfigSnapshotReaderMethodTestsGet2.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ struct ConfigSnapshotReaderMethodTestsGet2 {
3232
@Test func get() async throws {
3333
let config = ConfigReaderTests.config
3434

35-
try config.withSnapshot { snapshot in
35+
do {
36+
let snapshot = config.snapshot()
37+
3638
// Optional - success
3739
#expect(
3840
snapshot.string(forKey: ConfigKey(["stringConvertible"]), as: TestStringConvertible.self)
@@ -132,7 +134,9 @@ struct ConfigSnapshotReaderMethodTestsGet2 {
132134
try snapshot.requiredString(forKey: "failure", as: TestStringConvertible.self)
133135
}
134136
}
135-
try config.withSnapshot { snapshot in
137+
do {
138+
let snapshot = config.snapshot()
139+
136140
// Optional - success
137141
#expect(snapshot.string(forKey: ConfigKey(["stringEnum"]), as: TestEnum.self) == Defaults.stringEnum)
138142
#expect(snapshot.string(forKey: "stringEnum", as: TestEnum.self) == Defaults.stringEnum)

Tests/ConfigurationTests/ConfigReaderTests/ConfigSnapshotReaderMethodTestsGet2.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct ConfigSnapshotReaderMethodTestsGet2 {
3535
% test_type = string_convertible_type["testType"]
3636
% test_suffix = string_convertible_type["testSuffix"]
3737
% lower_test_suffix = lower_first(test_suffix)
38-
try config.withSnapshot { snapshot in
38+
do {
39+
let snapshot = config.snapshot()
40+
3941
// Optional - success
4042
#expect(snapshot.string(forKey: ConfigKey(["${lower_test_suffix}"]), as: ${test_type}.self) == Defaults.${lower_test_suffix})
4143
#expect(snapshot.string(forKey: "${lower_test_suffix}", as: ${test_type}.self) == Defaults.${lower_test_suffix})

Tests/ConfigurationTests/ConfigReaderTests/ConfigSnapshotReaderMethodTestsGet3.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ struct ConfigSnapshotReaderMethodTestsGet3 {
3232
@Test func get() async throws {
3333
let config = ConfigReaderTests.config
3434

35-
try config.withSnapshot { snapshot in
35+
do {
36+
let snapshot = config.snapshot()
37+
3638
// Optional - success
3739
#expect(
3840
snapshot.stringArray(forKey: ConfigKey(["stringConvertibleArray"]), as: TestStringConvertible.self)
@@ -137,7 +139,9 @@ struct ConfigSnapshotReaderMethodTestsGet3 {
137139
try snapshot.requiredStringArray(forKey: "failure", as: TestStringConvertible.self)
138140
}
139141
}
140-
try config.withSnapshot { snapshot in
142+
do {
143+
let snapshot = config.snapshot()
144+
141145
// Optional - success
142146
#expect(
143147
snapshot.stringArray(forKey: ConfigKey(["stringEnumArray"]), as: TestEnum.self)

0 commit comments

Comments
 (0)