Skip to content

Commit 309df0a

Browse files
authored
Remove custom key decoders (#71)
### Motivation **Implementation PR for a change that will be reviewed in a proposal** Fixes #70 There was a flaw in the design of custom key decoders: if one library changed the key decoder on a ConfigReader, and then passed the ConfigReader to another library, that second library would break, because it was expecting the default dot-based key decoder. This means that at least, ConfigReader is the wrong place for the custom keyDecoder property, and possibly there isn't a good place for it at all. ### Modifications This PR removes the whole concept of custom keyDecoders and hardcodes everything to use the dot-based one. This also unlocks making ConfigKey ExpressibleByStringLiteral, which in turn allows us to remove half of all the method overloads on ConfigReader and friends. ### Result No custom keyDecoder feature. If we find a better to way in the future, we can always introduce it back in an API-stable way. But right now it wasn't pulling its weight. ### Test Plan Adapted tests.
1 parent 5a82370 commit 309df0a

File tree

61 files changed

+795
-10840
lines changed

Some content is hidden

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

61 files changed

+795
-10840
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/ConfigKey.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
///
2121
/// Keys can include additional context information that some providers use to
2222
/// refine value lookups or provide more specific results.
23+
@available(Configuration 1.0, *)
2324
public struct ConfigKey: Sendable {
2425

2526
/// The hierarchical components that make up this configuration key.
@@ -42,10 +43,23 @@ public struct ConfigKey: Sendable {
4243
self.components = components
4344
self.context = context
4445
}
46+
47+
/// Creates a new configuration key.
48+
/// - Parameters:
49+
/// - string: The string represenation of the key path, for example `"http.timeout"`.
50+
/// - context: Additional context information for the key.
51+
public init(_ string: String, context: [String: ConfigContextValue] = [:]) {
52+
self = DotSeparatorKeyDecoder.decode(string, context: context)
53+
}
4554
}
4655

56+
@available(Configuration 1.0, *)
4757
extension ConfigKey: Equatable {}
58+
59+
@available(Configuration 1.0, *)
4860
extension ConfigKey: Hashable {}
61+
62+
@available(Configuration 1.0, *)
4963
extension ConfigKey: Comparable {
5064
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
5165
public static func < (lhs: ConfigKey, rhs: ConfigKey) -> Bool {
@@ -67,6 +81,7 @@ extension ConfigKey: Comparable {
6781
}
6882
}
6983

84+
@available(Configuration 1.0, *)
7085
extension ConfigKey: CustomStringConvertible {
7186
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
7287
public var description: String {
@@ -79,6 +94,15 @@ extension ConfigKey: CustomStringConvertible {
7994
}
8095
}
8196

97+
@available(Configuration 1.0, *)
98+
extension ConfigKey: ExpressibleByStringLiteral {
99+
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
100+
public init(stringLiteral value: String) {
101+
self = DotSeparatorKeyDecoder.decode(value, context: [:])
102+
}
103+
}
104+
105+
@available(Configuration 1.0, *)
82106
extension ConfigKey: ExpressibleByArrayLiteral {
83107
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
84108
public init(arrayLiteral elements: String...) {
@@ -94,6 +118,7 @@ extension ConfigKey: ExpressibleByArrayLiteral {
94118
///
95119
/// Like relative keys, absolute keys consist of hierarchical components and
96120
/// optional context information.
121+
@available(Configuration 1.0, *)
97122
public struct AbsoluteConfigKey: Sendable {
98123

99124
/// The hierarchical components that make up this absolute configuration key.
@@ -118,8 +143,13 @@ public struct AbsoluteConfigKey: Sendable {
118143
}
119144
}
120145

146+
@available(Configuration 1.0, *)
121147
extension AbsoluteConfigKey: Equatable {}
148+
149+
@available(Configuration 1.0, *)
122150
extension AbsoluteConfigKey: Hashable {}
151+
152+
@available(Configuration 1.0, *)
123153
extension AbsoluteConfigKey: Comparable {
124154
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
125155
public static func < (lhs: AbsoluteConfigKey, rhs: AbsoluteConfigKey) -> Bool {
@@ -141,6 +171,7 @@ extension AbsoluteConfigKey: Comparable {
141171
}
142172
}
143173

174+
@available(Configuration 1.0, *)
144175
extension AbsoluteConfigKey: CustomStringConvertible {
145176
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
146177
public var description: String {
@@ -153,6 +184,7 @@ extension AbsoluteConfigKey: CustomStringConvertible {
153184
}
154185
}
155186

187+
@available(Configuration 1.0, *)
156188
extension AbsoluteConfigKey {
157189

158190
/// Creates a new absolute configuration key from a relative key.
@@ -162,6 +194,7 @@ extension AbsoluteConfigKey {
162194
}
163195
}
164196

197+
@available(Configuration 1.0, *)
165198
extension AbsoluteConfigKey? {
166199
/// Returns a new absolute configuration key by appending the given relative key.
167200
/// - Parameter relative: The relative configuration key to append to this key.
@@ -178,6 +211,7 @@ extension AbsoluteConfigKey? {
178211
}
179212
}
180213

214+
@available(Configuration 1.0, *)
181215
extension AbsoluteConfigKey {
182216
/// Returns a new absolute configuration key by prepending the given relative key.
183217
/// - Parameter prefix: The relative configuration key to prepend to this key.
@@ -201,6 +235,15 @@ extension AbsoluteConfigKey {
201235
}
202236
}
203237

238+
@available(Configuration 1.0, *)
239+
extension AbsoluteConfigKey: ExpressibleByStringLiteral {
240+
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
241+
public init(stringLiteral value: String) {
242+
self = .init(DotSeparatorKeyDecoder.decode(value, context: [:]))
243+
}
244+
}
245+
246+
@available(Configuration 1.0, *)
204247
extension AbsoluteConfigKey: ExpressibleByArrayLiteral {
205248
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
206249
public init(arrayLiteral elements: String...) {

0 commit comments

Comments
 (0)