apple / apple/swift-configuration

ExpressibleByConfigStringArray

Open
#138 3 comments 0 reactions 1 assignee Claimed by @woolnd View on GitHub
good first issue kind/feature
Dominant language
Swift
Stars
812
Forks
60
Avg merge
9h 38m
Merged PRs (30d)
6

Description

There are some types, such as `HTTPHeaders` from `NIOHTTP1`, that it would make sense to write in configuration files as arrays of strings, like:
```json
{
"headers": ["X-Custom-Header: CustomValue", "X-Another-Header: AnotherValue"]
}
```

However, for `HTTPHeaders`, there is no base type that can conform to `ExpressibleByConfigString` (you have to add each individual header with `headers.add(name: name, value: value)`).

A new `ExpressibleByConfigStringArray` protocol could be added, with an initializer that takes `configStringArray: [String]` instead of `configString: String`.

```swift
public protocol ExpressibleByConfigStringArray {
/// Creates an instance from a configuration string array value.
///
/// - Parameter configStringArray: The string array value from the configuration provider.
init?(configStringArray: [String])
}
```
I don't know if it would also be necessary to inherit `CustomStringConvertible`.

The `HTTPHeaders` conformance could look like this:
```swift
extension HTTPHeaders: @retroactive ExpressibleByConfigStringArray {
init?(configStringArray: [String]) {
// Just a Proof-of-Concept
var headers = HTTPHeaders()
for configString in configStringArray {
guard let colonIndex = configString.firstIndex(of: ":") else {
return nil
}
let name = String(configString[..

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.