You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generic file providers (Implementation of SCO-0001) (#60)
### Motivation
While JSON and YAML support comes built-in in Swift Configuration, it
should be easier to implement additional formats in external extension
packages. Right now it [required
copying](#37 (comment))
a lot of boilerplate code (thank you @finnvoor for pointing that out).
This PR is ready to land, the API was accepted in
https://forums.swift.org/t/proposal-sco-0001-generic-file-providers/82824/9
~~Note that this PR isn't meant to land just yet - I'll first write a
[proposal](https://swiftpackageindex.com/apple/swift-configuration/0.2.0/documentation/configuration/proposals)
to solicit community feedback before moving forward.~~ However, you're
encouraged to test it out by depending on my fork's branch with this
feature, to ensure it works well - if you're considering extending Swift
Configuration with a new format in your own package.
```swift
.package(
url: "https://github.com/czechboy0/swift-configuration",
branch: "hd-generic-file-providers",
traits: [.defaults, "ReloadingSupport"]
)
```
This PR also bumps the minimum Swift version to 6.2, since that's
required by RawSpan.
### Modifications
Refactors JSON and YAML providers to factor out reusable `FileProvider`
and `ReloadingFileProvider` types that are generic over their snapshot
type.
This reduces the amount of code an adopter has to write to add a new
format only to the parsing logic, but they get everything else related
to file reading and reloading for free.
### Result
An API that allows adding more file formats much more easily.
### Test Plan
Adapted tests.
Copy file name to clipboardExpand all lines: Package.swift
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
// swift-tools-version: 6.1
1
+
// swift-tools-version: 6.2
2
2
3
3
import PackageDescription
4
4
#if canImport(FoundationEssentials)
@@ -29,7 +29,7 @@ var traits: Set<Trait> = [
29
29
.trait(
30
30
name:"ReloadingSupport",
31
31
description:
32
-
"Adds support for reloading built-in provider variants, such as ReloadingJSONProvider and ReloadingYAMLProvider (when their respective traits are enabled).",
32
+
"Adds support for reloading built-in provider variants, such as ReloadingFileProvider.",
let httpTimeout = config.int(forKey: "http.timeout", default: 60)
49
49
print(httpTimeout) // prints 15
@@ -100,11 +100,11 @@ To enable an additional trait on the package, update the package dependency:
100
100
```
101
101
102
102
Available traits:
103
-
-**`JSONSupport`** (default): Adds support for `JSONProvider`, a `ConfigProvider` for reading JSON files.
103
+
-**`JSONSupport`** (default): Adds support for `FileProvider<JSONSnapshot>`, a `ConfigProvider` for reading JSON files.
104
104
-**`LoggingSupport`** (opt-in): Adds support for `AccessLogger`, a way to emit access events into a `SwiftLog.Logger`.
105
-
-**`ReloadingSupport`** (opt-in): Adds support for auto-reloading variants of file providers, such as `ReloadingJSONProvider` (when `JSONSupport` is enabled) and `ReloadingYAMLProvider` (when `YAMLSupport` is enabled).
105
+
-**`ReloadingSupport`** (opt-in): Adds support for auto-reloading variants of file providers, such as `ReloadingFileProvider<JSONSnapshot>` (when `JSONSupport` is enabled) and `ReloadingFileProvider<YAMLSnapshot>` (when `YAMLSupport` is enabled).
106
106
-**`CommandLineArgumentsSupport`** (opt-in): Adds support for `CommandLineArgumentsProvider` for parsing command line arguments.
107
-
-**`YAMLSupport`** (opt-in): Adds support for `YAMLProvider`, a `ConfigProvider` for reading YAML files.
107
+
-**`YAMLSupport`** (opt-in): Adds support for `FileProvider<YAMLSnapshot>`, a `ConfigProvider` for reading YAML files.
108
108
109
109
## Supported platforms and minimum versions
110
110
@@ -120,8 +120,8 @@ The library includes comprehensive built-in provider support:
- JSON file: [`JSONProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/jsonprovider) and [`ReloadingJSONProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/reloadingjsonprovider)
124
-
- YAML file: [`YAMLProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/yamlprovider) and [`ReloadingYAMLProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/reloadingyamlprovider)
123
+
- JSON file: [`FileProvider<JSONSnapshot>`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/fileprovider) and [`ReloadingFileProvider<JSONSnapshot>`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/reloadingfileprovider)
124
+
- YAML file: [`FileProvider<YAMLSnapshot>`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/fileprovider) and [`ReloadingFileProvider<YAMLSnapshot>`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/reloadingfileprovider)
125
125
- Directory of files: [`DirectoryFilesProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/directoryfilesprovider)
126
126
- In-memory: [`InMemoryProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/inmemoryprovider) and [`MutableInMemoryProvider`](https://swiftpackageindex.com/apple/swift-configuration/documentation/configuration/mutableinmemoryprovider)
Copy file name to clipboardExpand all lines: Sources/Configuration/Documentation.docc/Documentation.md
+18-17Lines changed: 18 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ For example, to read the timeout configuration value for an HTTP client, check o
44
44
}
45
45
```
46
46
```swift
47
-
let provider = try await JSONProvider(
47
+
let provider = try await FileProvider<JSONSnapshot>(
48
48
filePath: "/etc/config.json"
49
49
)
50
50
let config = ConfigReader(provider: provider)
@@ -61,7 +61,7 @@ For example, to read the timeout configuration value for an HTTP client, check o
61
61
}
62
62
```
63
63
```swift
64
-
let provider = try await ReloadingJSONProvider(
64
+
let provider = try await ReloadingFileProvider<JSONSnapshot>(
65
65
filePath: "/etc/config.json"
66
66
)
67
67
// Omitted: Add `provider` to a ServiceGroup
@@ -76,7 +76,7 @@ For example, to read the timeout configuration value for an HTTP client, check o
76
76
timeout: 30
77
77
```
78
78
```swift
79
-
let provider = try await YAMLProvider(
79
+
let provider = try await FileProvider<YAMLSnapshot>(
80
80
filePath: "/etc/config.yaml"
81
81
)
82
82
let config = ConfigReader(provider: provider)
@@ -90,7 +90,7 @@ For example, to read the timeout configuration value for an HTTP client, check o
90
90
timeout: 30
91
91
```
92
92
```swift
93
-
let provider = try await ReloadingYAMLProvider(
93
+
let provider = try await ReloadingFileProvider<YAMLSnapshot>(
94
94
filePath: "/etc/config.yaml"
95
95
)
96
96
// Omitted: Add `provider` to a ServiceGroup
@@ -187,11 +187,11 @@ To enable an additional trait on the package, update the package dependency:
187
187
```
188
188
189
189
Available traits:
190
-
-**`JSONSupport`** (default): Adds support for ``JSONProvider``, a ``ConfigProvider``for reading JSON files.
190
+
-**`JSONSupport`** (default): Adds support for ``JSONSnapshot``, which enables using ``FileProvider``and ``ReloadingFileProvider`` with JSON files.
191
191
-**`LoggingSupport`** (opt-in): Adds support for ``AccessLogger``, a way to emit access events into a `SwiftLog.Logger`.
192
-
-**`ReloadingSupport`** (opt-in): Adds support for auto-reloading variants of file providers, such as ``ReloadingJSONProvider`` (when `JSONSupport` is enabled) and ``ReloadingYAMLProvider`` (when `YAMLSupport` is enabled).
192
+
-**`ReloadingSupport`** (opt-in): Adds support for ``ReloadingFileProvider``, which provides auto-reloading capability for file-based configuration.
193
193
-**`CommandLineArgumentsSupport`** (opt-in): Adds support for ``CommandLineArgumentsProvider`` for parsing command line arguments.
194
-
-**`YAMLSupport`** (opt-in): Adds support for ``YAMLProvider``, a ``ConfigProvider``for reading YAML files.
194
+
-**`YAMLSupport`** (opt-in): Adds support for ``YAMLSnapshot``, which enables using ``FileProvider``and ``ReloadingFileProvider`` with YAML files.
195
195
196
196
### Supported platforms and minimum versions
197
197
@@ -235,8 +235,8 @@ The library includes comprehensive built-in provider support:
0 commit comments