Skip to content

Commit 780b888

Browse files
authored
Remove the ConfigKeyEncoder API (#86)
### Motivation Fixes #84. This API is a pretty niche customization point and we don't want to commit to it just yet. ### Modifications Pulled `ConfigKeyEncoder` and implementations from API to internal, we can make it public in the future. ### Result Smaller public API. ### Test Plan Tests pass.
1 parent 1becb8e commit 780b888

File tree

9 files changed

+19
-87
lines changed

9 files changed

+19
-87
lines changed

Sources/Configuration/Documentation.docc/Documentation.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ Any package can implement a ``ConfigProvider``, making the ecosystem extensible
421421
- ``ConfigKey``
422422
- ``AbsoluteConfigKey``
423423
- ``ConfigContextValue``
424-
- ``ConfigKeyEncoder``
425-
- ``SeparatorKeyEncoder``
426-
- ``DirectoryFileKeyEncoder``
427424

428425
### Troubleshooting and access reporting
429426
- <doc:Troubleshooting>

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
### Creating a directory files provider
66

7-
- ``init(directoryPath:allowMissing:secretsSpecifier:arraySeparator:keyEncoder:)``
7+
- ``init(directoryPath:allowMissing:secretsSpecifier:arraySeparator:)``

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

Sources/Configuration/KeyCoders/ConfigKeyEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
///
4141
/// - ``AbsoluteConfigKey`` - The structured key representation
4242
@available(Configuration 1.0, *)
43-
public protocol ConfigKeyEncoder: Sendable {
43+
internal protocol ConfigKeyEncoder: Sendable {
4444

4545
/// Encodes a structured configuration key into its string representation.
4646
///

Sources/Configuration/KeyCoders/SeparatorKeyEncoder.swift

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,20 @@
3737
/// let dashEncoder = ConfigKeyEncoder.dashSeparated
3838
/// ```
3939
@available(Configuration 1.0, *)
40-
public struct SeparatorKeyEncoder {
40+
internal struct SeparatorKeyEncoder {
4141

4242
/// The string used to join key components.
4343
///
4444
/// This separator is inserted between each component when encoding hierarchical
4545
/// keys into flat strings. Common separators include "." for dot notation and
4646
/// "-" for dash notation.
47-
public var separator: String
48-
49-
/// Creates a new separator-based key encoder.
50-
///
51-
/// ```swift
52-
/// let encoder = SeparatorKeyEncoder(separator: "_")
53-
/// let key = AbsoluteConfigKey(components: ["app", "config", "debug"], context: context)
54-
/// let encoded = encoder.encode(key)
55-
/// // Results in "app_config_debug"
56-
/// ```
57-
///
58-
/// - Parameter separator: The string to use for joining key components.
59-
public init(separator: String) {
60-
self.separator = separator
61-
}
47+
var separator: String
6248
}
6349

6450
@available(Configuration 1.0, *)
6551
extension SeparatorKeyEncoder: ConfigKeyEncoder {
6652
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
67-
public func encode(_ key: AbsoluteConfigKey) -> String {
53+
func encode(_ key: AbsoluteConfigKey) -> String {
6854
key.components.joined(separator: separator)
6955
}
7056
}
@@ -82,7 +68,7 @@ extension ConfigKeyEncoder where Self == SeparatorKeyEncoder {
8268
/// let encoded = encoder.encode(key)
8369
/// // Results in "app.database.host"
8470
/// ```
85-
public static var dotSeparated: Self {
71+
static var dotSeparated: Self {
8672
SeparatorKeyEncoder(separator: ".")
8773
}
8874

@@ -97,7 +83,7 @@ extension ConfigKeyEncoder where Self == SeparatorKeyEncoder {
9783
/// let encoded = encoder.encode(key)
9884
/// // Results in "app-database-host"
9985
/// ```
100-
public static var dashSeparated: Self {
86+
static var dashSeparated: Self {
10187
SeparatorKeyEncoder(separator: "-")
10288
}
10389
}

Sources/Configuration/Providers/Files/DirectoryFilesProvider.swift

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ public import SystemPackage
2828
///
2929
/// ## Key mapping
3030
///
31-
/// By default, configuration keys are transformed into file names using these rules:
32-
/// - Components are joined with dashes
33-
/// - Non-alphanumeric characters (except dashes) are replaced with underscores
31+
/// Configuration keys are transformed into file names using these rules:
32+
/// - Components are joined with dashes.
33+
/// - Non-alphanumeric characters (except dashes) are replaced with underscores.
3434
///
3535
/// For example:
3636
/// - `database.password` -> `database-password`
3737
///
38-
/// You can customize this key mapping by providing a custom `ConfigKeyEncoder` implementation
39-
/// to the initializer.
40-
///
4138
/// ## Value handling
4239
///
4340
/// The provider reads file contents as UTF-8 strings and converts them to the requested
@@ -103,24 +100,6 @@ public import SystemPackage
103100
/// // ["host1.example.com", "host2.example.com", "host3.example.com"]
104101
/// ```
105102
///
106-
/// ### Custom key encoding
107-
///
108-
/// ```swift
109-
/// // Custom key encoder that uses underscores instead of dashes
110-
/// struct CustomKeyEncoder: ConfigKeyEncoder {
111-
/// func encode(_ key: AbsoluteConfigKey) -> String {
112-
/// key.components.joined(separator: "_")
113-
/// }
114-
/// }
115-
///
116-
/// let provider = try await DirectoryFilesProvider(
117-
/// directoryPath: "/etc/config",
118-
/// keyEncoder: CustomKeyEncoder()
119-
/// )
120-
///
121-
/// // Now "database.password" maps to file "database_password" instead of "database-password"
122-
/// ```
123-
///
124103
/// ## Configuration context
125104
///
126105
/// This provider ignores the context passed in ``AbsoluteConfigKey/context``.
@@ -152,7 +131,7 @@ public struct DirectoryFilesProvider: Sendable {
152131
var arrayDecoder: DirectoryFilesValueArrayDecoder
153132

154133
/// The key encoder for converting config keys to file names.
155-
var keyEncoder: any ConfigKeyEncoder
134+
var keyEncoder: any ConfigKeyEncoder = .directoryFiles
156135
}
157136

158137
/// The underlying snapshot of the provider.
@@ -178,22 +157,19 @@ public struct DirectoryFilesProvider: Sendable {
178157
/// - When `true`, if the directory is missing, treats it as empty.
179158
/// - secretsSpecifier: Specifies which values should be treated as secrets.
180159
/// - arraySeparator: The character used to separate elements in array values.
181-
/// - keyEncoder: The encoder to use for converting configuration keys to file names.
182160
/// - Throws: If the directory cannot be found or read.
183161
public init(
184162
directoryPath: FilePath,
185163
allowMissing: Bool = false,
186164
secretsSpecifier: SecretsSpecifier<String, Data> = .all,
187-
arraySeparator: Character = ",",
188-
keyEncoder: some ConfigKeyEncoder = .directoryFiles
165+
arraySeparator: Character = ","
189166
) async throws {
190167
try await self.init(
191168
directoryPath: directoryPath,
192169
allowMissing: allowMissing,
193170
fileSystem: LocalCommonProviderFileSystem(),
194171
secretsSpecifier: secretsSpecifier,
195-
arraySeparator: arraySeparator,
196-
keyEncoder: keyEncoder
172+
arraySeparator: arraySeparator
197173
)
198174
}
199175

@@ -210,15 +186,13 @@ public struct DirectoryFilesProvider: Sendable {
210186
/// - fileSystem: The file system implementation to use.
211187
/// - secretsSpecifier: Specifies which values should be treated as secrets. Defaults to `.all`.
212188
/// - arraySeparator: The character used to separate elements in array values. Defaults to comma.
213-
/// - keyEncoder: The encoder to use for converting configuration keys to file names.
214189
/// - Throws: If the directory cannot be found or read.
215190
internal init(
216191
directoryPath: FilePath,
217192
allowMissing: Bool,
218193
fileSystem: some CommonProviderFileSystem,
219194
secretsSpecifier: SecretsSpecifier<String, Data> = .all,
220-
arraySeparator: Character = ",",
221-
keyEncoder: some ConfigKeyEncoder = .directoryFiles
195+
arraySeparator: Character = ","
222196
) async throws {
223197
let fileValues = try await Self.loadDirectory(
224198
at: directoryPath,
@@ -228,8 +202,7 @@ public struct DirectoryFilesProvider: Sendable {
228202
)
229203
self._snapshot = .init(
230204
fileValues: fileValues,
231-
arrayDecoder: DirectoryFilesValueArrayDecoder(separator: arraySeparator),
232-
keyEncoder: keyEncoder
205+
arrayDecoder: DirectoryFilesValueArrayDecoder(separator: arraySeparator)
233206
)
234207
}
235208

@@ -478,15 +451,15 @@ extension DirectoryFilesProvider: ConfigProvider {
478451
/// - Components are joined with dashes
479452
/// - Non-alphanumeric characters (except dashes) are replaced with underscores
480453
@available(Configuration 1.0, *)
481-
public struct DirectoryFileKeyEncoder {
454+
internal struct DirectoryFileKeyEncoder {
482455
/// Creates a default directory key encoder that follows standard file naming conventions.
483456
public init() {}
484457
}
485458

486459
@available(Configuration 1.0, *)
487460
extension DirectoryFileKeyEncoder: ConfigKeyEncoder {
488461
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
489-
public func encode(_ key: AbsoluteConfigKey) -> String {
462+
func encode(_ key: AbsoluteConfigKey) -> String {
490463
key.components
491464
.map { component in
492465
component
@@ -513,7 +486,7 @@ extension ConfigKeyEncoder where Self == DirectoryFileKeyEncoder {
513486
/// - Non-alphanumeric characters (except dashes) are replaced with underscores
514487
///
515488
/// - Returns: A new key encoder.
516-
public static var directoryFiles: Self {
489+
static var directoryFiles: Self {
517490
DirectoryFileKeyEncoder()
518491
}
519492
}

Tests/ConfigurationTests/SeparatorKeyEncoderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Configuration
15+
@testable import Configuration
1616
import Testing
1717

1818
struct SeparatorKeyEncoderTests {

0 commit comments

Comments
 (0)