From 559747056f84aeab41d9b5d37702f196b103d2be Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 25 Nov 2025 15:20:42 +0100 Subject: [PATCH] Remove the ConfigKeyEncoder API --- .../Documentation.docc/Documentation.md | 3 -- .../Reference/ConfigKeyEncoder.md | 14 ------ .../Reference/DirectoryFileKeyEncoder.md | 7 --- .../Reference/DirectoryFilesProvider.md | 2 +- .../Reference/SeparatorKeyEncoder.md | 3 -- .../KeyCoders/ConfigKeyEncoder.swift | 2 +- .../KeyCoders/SeparatorKeyEncoder.swift | 24 ++------- .../Files/DirectoryFilesProvider.swift | 49 +++++-------------- .../SeparatorKeyEncoderTests.swift | 2 +- 9 files changed, 19 insertions(+), 87 deletions(-) delete mode 100644 Sources/Configuration/Documentation.docc/Reference/ConfigKeyEncoder.md delete mode 100644 Sources/Configuration/Documentation.docc/Reference/DirectoryFileKeyEncoder.md delete mode 100644 Sources/Configuration/Documentation.docc/Reference/SeparatorKeyEncoder.md diff --git a/Sources/Configuration/Documentation.docc/Documentation.md b/Sources/Configuration/Documentation.docc/Documentation.md index d84cfa9..23fcbbf 100644 --- a/Sources/Configuration/Documentation.docc/Documentation.md +++ b/Sources/Configuration/Documentation.docc/Documentation.md @@ -421,9 +421,6 @@ Any package can implement a ``ConfigProvider``, making the ecosystem extensible - ``ConfigKey`` - ``AbsoluteConfigKey`` - ``ConfigContextValue`` -- ``ConfigKeyEncoder`` -- ``SeparatorKeyEncoder`` -- ``DirectoryFileKeyEncoder`` ### Troubleshooting and access reporting - diff --git a/Sources/Configuration/Documentation.docc/Reference/ConfigKeyEncoder.md b/Sources/Configuration/Documentation.docc/Reference/ConfigKeyEncoder.md deleted file mode 100644 index 324acc2..0000000 --- a/Sources/Configuration/Documentation.docc/Reference/ConfigKeyEncoder.md +++ /dev/null @@ -1,14 +0,0 @@ -# ``Configuration/ConfigKeyEncoder`` - -## Topics - -### Built-in key encoders - -- ``dashSeparated`` -- ``dotSeparated`` -- ``directoryFiles`` - -### Encoding keys - -- ``encode(_:)`` - diff --git a/Sources/Configuration/Documentation.docc/Reference/DirectoryFileKeyEncoder.md b/Sources/Configuration/Documentation.docc/Reference/DirectoryFileKeyEncoder.md deleted file mode 100644 index c7c6cd1..0000000 --- a/Sources/Configuration/Documentation.docc/Reference/DirectoryFileKeyEncoder.md +++ /dev/null @@ -1,7 +0,0 @@ -# ``Configuration/DirectoryFileKeyEncoder`` - -## Topics - -### Creating a directory file key encoder - -- ``init()`` diff --git a/Sources/Configuration/Documentation.docc/Reference/DirectoryFilesProvider.md b/Sources/Configuration/Documentation.docc/Reference/DirectoryFilesProvider.md index 19c3477..4711198 100644 --- a/Sources/Configuration/Documentation.docc/Reference/DirectoryFilesProvider.md +++ b/Sources/Configuration/Documentation.docc/Reference/DirectoryFilesProvider.md @@ -4,4 +4,4 @@ ### Creating a directory files provider -- ``init(directoryPath:allowMissing:secretsSpecifier:arraySeparator:keyEncoder:)`` +- ``init(directoryPath:allowMissing:secretsSpecifier:arraySeparator:)`` diff --git a/Sources/Configuration/Documentation.docc/Reference/SeparatorKeyEncoder.md b/Sources/Configuration/Documentation.docc/Reference/SeparatorKeyEncoder.md deleted file mode 100644 index e0cafbb..0000000 --- a/Sources/Configuration/Documentation.docc/Reference/SeparatorKeyEncoder.md +++ /dev/null @@ -1,3 +0,0 @@ -# ``Configuration/SeparatorKeyEncoder`` - -## Topics diff --git a/Sources/Configuration/KeyCoders/ConfigKeyEncoder.swift b/Sources/Configuration/KeyCoders/ConfigKeyEncoder.swift index 3a57f60..02f1e2d 100644 --- a/Sources/Configuration/KeyCoders/ConfigKeyEncoder.swift +++ b/Sources/Configuration/KeyCoders/ConfigKeyEncoder.swift @@ -40,7 +40,7 @@ /// /// - ``AbsoluteConfigKey`` - The structured key representation @available(Configuration 1.0, *) -public protocol ConfigKeyEncoder: Sendable { +internal protocol ConfigKeyEncoder: Sendable { /// Encodes a structured configuration key into its string representation. /// diff --git a/Sources/Configuration/KeyCoders/SeparatorKeyEncoder.swift b/Sources/Configuration/KeyCoders/SeparatorKeyEncoder.swift index d843d0c..984148e 100644 --- a/Sources/Configuration/KeyCoders/SeparatorKeyEncoder.swift +++ b/Sources/Configuration/KeyCoders/SeparatorKeyEncoder.swift @@ -37,34 +37,20 @@ /// let dashEncoder = ConfigKeyEncoder.dashSeparated /// ``` @available(Configuration 1.0, *) -public struct SeparatorKeyEncoder { +internal struct SeparatorKeyEncoder { /// The string used to join key components. /// /// This separator is inserted between each component when encoding hierarchical /// keys into flat strings. Common separators include "." for dot notation and /// "-" for dash notation. - public var separator: String - - /// Creates a new separator-based key encoder. - /// - /// ```swift - /// let encoder = SeparatorKeyEncoder(separator: "_") - /// let key = AbsoluteConfigKey(components: ["app", "config", "debug"], context: context) - /// let encoded = encoder.encode(key) - /// // Results in "app_config_debug" - /// ``` - /// - /// - Parameter separator: The string to use for joining key components. - public init(separator: String) { - self.separator = separator - } + var separator: String } @available(Configuration 1.0, *) extension SeparatorKeyEncoder: ConfigKeyEncoder { // swift-format-ignore: AllPublicDeclarationsHaveDocumentation - public func encode(_ key: AbsoluteConfigKey) -> String { + func encode(_ key: AbsoluteConfigKey) -> String { key.components.joined(separator: separator) } } @@ -82,7 +68,7 @@ extension ConfigKeyEncoder where Self == SeparatorKeyEncoder { /// let encoded = encoder.encode(key) /// // Results in "app.database.host" /// ``` - public static var dotSeparated: Self { + static var dotSeparated: Self { SeparatorKeyEncoder(separator: ".") } @@ -97,7 +83,7 @@ extension ConfigKeyEncoder where Self == SeparatorKeyEncoder { /// let encoded = encoder.encode(key) /// // Results in "app-database-host" /// ``` - public static var dashSeparated: Self { + static var dashSeparated: Self { SeparatorKeyEncoder(separator: "-") } } diff --git a/Sources/Configuration/Providers/Files/DirectoryFilesProvider.swift b/Sources/Configuration/Providers/Files/DirectoryFilesProvider.swift index f34e3d0..b36ba52 100644 --- a/Sources/Configuration/Providers/Files/DirectoryFilesProvider.swift +++ b/Sources/Configuration/Providers/Files/DirectoryFilesProvider.swift @@ -28,16 +28,13 @@ public import SystemPackage /// /// ## Key mapping /// -/// By default, configuration keys are transformed into file names using these rules: -/// - Components are joined with dashes -/// - Non-alphanumeric characters (except dashes) are replaced with underscores +/// Configuration keys are transformed into file names using these rules: +/// - Components are joined with dashes. +/// - Non-alphanumeric characters (except dashes) are replaced with underscores. /// /// For example: /// - `database.password` -> `database-password` /// -/// You can customize this key mapping by providing a custom `ConfigKeyEncoder` implementation -/// to the initializer. -/// /// ## Value handling /// /// The provider reads file contents as UTF-8 strings and converts them to the requested @@ -103,24 +100,6 @@ public import SystemPackage /// // ["host1.example.com", "host2.example.com", "host3.example.com"] /// ``` /// -/// ### Custom key encoding -/// -/// ```swift -/// // Custom key encoder that uses underscores instead of dashes -/// struct CustomKeyEncoder: ConfigKeyEncoder { -/// func encode(_ key: AbsoluteConfigKey) -> String { -/// key.components.joined(separator: "_") -/// } -/// } -/// -/// let provider = try await DirectoryFilesProvider( -/// directoryPath: "/etc/config", -/// keyEncoder: CustomKeyEncoder() -/// ) -/// -/// // Now "database.password" maps to file "database_password" instead of "database-password" -/// ``` -/// /// ## Configuration context /// /// This provider ignores the context passed in ``AbsoluteConfigKey/context``. @@ -152,7 +131,7 @@ public struct DirectoryFilesProvider: Sendable { var arrayDecoder: DirectoryFilesValueArrayDecoder /// The key encoder for converting config keys to file names. - var keyEncoder: any ConfigKeyEncoder + var keyEncoder: any ConfigKeyEncoder = .directoryFiles } /// The underlying snapshot of the provider. @@ -178,22 +157,19 @@ public struct DirectoryFilesProvider: Sendable { /// - When `true`, if the directory is missing, treats it as empty. /// - secretsSpecifier: Specifies which values should be treated as secrets. /// - arraySeparator: The character used to separate elements in array values. - /// - keyEncoder: The encoder to use for converting configuration keys to file names. /// - Throws: If the directory cannot be found or read. public init( directoryPath: FilePath, allowMissing: Bool = false, secretsSpecifier: SecretsSpecifier = .all, - arraySeparator: Character = ",", - keyEncoder: some ConfigKeyEncoder = .directoryFiles + arraySeparator: Character = "," ) async throws { try await self.init( directoryPath: directoryPath, allowMissing: allowMissing, fileSystem: LocalCommonProviderFileSystem(), secretsSpecifier: secretsSpecifier, - arraySeparator: arraySeparator, - keyEncoder: keyEncoder + arraySeparator: arraySeparator ) } @@ -210,15 +186,13 @@ public struct DirectoryFilesProvider: Sendable { /// - fileSystem: The file system implementation to use. /// - secretsSpecifier: Specifies which values should be treated as secrets. Defaults to `.all`. /// - arraySeparator: The character used to separate elements in array values. Defaults to comma. - /// - keyEncoder: The encoder to use for converting configuration keys to file names. /// - Throws: If the directory cannot be found or read. internal init( directoryPath: FilePath, allowMissing: Bool, fileSystem: some CommonProviderFileSystem, secretsSpecifier: SecretsSpecifier = .all, - arraySeparator: Character = ",", - keyEncoder: some ConfigKeyEncoder = .directoryFiles + arraySeparator: Character = "," ) async throws { let fileValues = try await Self.loadDirectory( at: directoryPath, @@ -228,8 +202,7 @@ public struct DirectoryFilesProvider: Sendable { ) self._snapshot = .init( fileValues: fileValues, - arrayDecoder: DirectoryFilesValueArrayDecoder(separator: arraySeparator), - keyEncoder: keyEncoder + arrayDecoder: DirectoryFilesValueArrayDecoder(separator: arraySeparator) ) } @@ -478,7 +451,7 @@ extension DirectoryFilesProvider: ConfigProvider { /// - Components are joined with dashes /// - Non-alphanumeric characters (except dashes) are replaced with underscores @available(Configuration 1.0, *) -public struct DirectoryFileKeyEncoder { +internal struct DirectoryFileKeyEncoder { /// Creates a default directory key encoder that follows standard file naming conventions. public init() {} } @@ -486,7 +459,7 @@ public struct DirectoryFileKeyEncoder { @available(Configuration 1.0, *) extension DirectoryFileKeyEncoder: ConfigKeyEncoder { // swift-format-ignore: AllPublicDeclarationsHaveDocumentation - public func encode(_ key: AbsoluteConfigKey) -> String { + func encode(_ key: AbsoluteConfigKey) -> String { key.components .map { component in component @@ -513,7 +486,7 @@ extension ConfigKeyEncoder where Self == DirectoryFileKeyEncoder { /// - Non-alphanumeric characters (except dashes) are replaced with underscores /// /// - Returns: A new key encoder. - public static var directoryFiles: Self { + static var directoryFiles: Self { DirectoryFileKeyEncoder() } } diff --git a/Tests/ConfigurationTests/SeparatorKeyEncoderTests.swift b/Tests/ConfigurationTests/SeparatorKeyEncoderTests.swift index 95ad240..c8a1f91 100644 --- a/Tests/ConfigurationTests/SeparatorKeyEncoderTests.swift +++ b/Tests/ConfigurationTests/SeparatorKeyEncoderTests.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import Configuration +@testable import Configuration import Testing struct SeparatorKeyEncoderTests {