Skip to content

Commit d45e629

Browse files
committed
Move decodeBool to Snapshot of EnvironmentVariablesProvider
Apply review comments
1 parent 0f3bde2 commit d45e629

File tree

5 files changed

+38
-103
lines changed

5 files changed

+38
-103
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
### Creating an environment variable provider
66

7-
- ``init(secretsSpecifier:bytesDecoder:boolDecoder:arraySeparator:)``
8-
- ``init(environmentVariables:secretsSpecifier:bytesDecoder:boolDecoder:arraySeparator:)``
9-
- ``init(environmentFilePath:allowMissing:secretsSpecifier:bytesDecoder:boolDecoder:arraySeparator:)``
7+
- ``init(secretsSpecifier:bytesDecoder:arraySeparator:)``
8+
- ``init(environmentVariables:secretsSpecifier:bytesDecoder:arraySeparator:)``
9+
- ``init(environmentFilePath:allowMissing:secretsSpecifier:bytesDecoder:arraySeparator:)``
1010

1111
### Inspecting an environment variable provider
1212

Sources/Configuration/Providers/EnvironmentVariables/EnvironmentVariablesProvider.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,18 @@ public struct EnvironmentVariablesProvider: Sendable {
144144
var arrayDecoder: EnvironmentValueArrayDecoder
145145

146146
/// A decoder of bool values from a string
147-
let boolDecoder: BoolDecoder
147+
static func decodeBool(from string: String) -> Bool? {
148+
let stringLowercased = string.lowercased()
149+
return if ["true", "false"].contains(stringLowercased) {
150+
stringLowercased == "true"
151+
} else if ["yes", "no"].contains(stringLowercased) {
152+
stringLowercased == "yes"
153+
} else if ["1", "0"].contains(stringLowercased) {
154+
stringLowercased == "1"
155+
} else {
156+
nil
157+
}
158+
}
148159
}
149160

150161
/// The underlying snapshot of the provider.
@@ -173,14 +184,12 @@ public struct EnvironmentVariablesProvider: Sendable {
173184
public init(
174185
secretsSpecifier: SecretsSpecifier<String, String> = .none,
175186
bytesDecoder: some ConfigBytesFromStringDecoder = .base64,
176-
boolDecoder: BoolDecoder = .init(),
177187
arraySeparator: Character = ","
178188
) {
179189
self.init(
180190
environmentVariables: ProcessInfo.processInfo.environment,
181191
secretsSpecifier: secretsSpecifier,
182192
bytesDecoder: bytesDecoder,
183-
boolDecoder: boolDecoder,
184193
arraySeparator: arraySeparator
185194
)
186195
}
@@ -212,7 +221,6 @@ public struct EnvironmentVariablesProvider: Sendable {
212221
environmentVariables: [String: String],
213222
secretsSpecifier: SecretsSpecifier<String, String> = .none,
214223
bytesDecoder: some ConfigBytesFromStringDecoder = .base64,
215-
boolDecoder: BoolDecoder = .init(),
216224
arraySeparator: Character = ","
217225
) {
218226
let tuples: [(String, EnvironmentValue)] = environmentVariables.map { key, value in
@@ -227,8 +235,7 @@ public struct EnvironmentVariablesProvider: Sendable {
227235
self._snapshot = .init(
228236
environmentVariables: Dictionary(uniqueKeysWithValues: tuples),
229237
bytesDecoder: bytesDecoder,
230-
arrayDecoder: EnvironmentValueArrayDecoder(separator: arraySeparator),
231-
boolDecoder: boolDecoder
238+
arrayDecoder: EnvironmentValueArrayDecoder(separator: arraySeparator)
232239
)
233240
}
234241

@@ -262,7 +269,6 @@ public struct EnvironmentVariablesProvider: Sendable {
262269
allowMissing: Bool = false,
263270
secretsSpecifier: SecretsSpecifier<String, String> = .none,
264271
bytesDecoder: some ConfigBytesFromStringDecoder = .base64,
265-
boolDecoder: BoolDecoder = .init(),
266272
arraySeparator: Character = ","
267273
) async throws {
268274
try await self.init(
@@ -271,7 +277,6 @@ public struct EnvironmentVariablesProvider: Sendable {
271277
fileSystem: LocalCommonProviderFileSystem(),
272278
secretsSpecifier: secretsSpecifier,
273279
bytesDecoder: bytesDecoder,
274-
boolDecoder: boolDecoder,
275280
arraySeparator: arraySeparator
276281
)
277282
}
@@ -294,7 +299,6 @@ public struct EnvironmentVariablesProvider: Sendable {
294299
fileSystem: some CommonProviderFileSystem,
295300
secretsSpecifier: SecretsSpecifier<String, String> = .none,
296301
bytesDecoder: some ConfigBytesFromStringDecoder = .base64,
297-
boolDecoder: BoolDecoder = .init(),
298302
arraySeparator: Character = ","
299303
) async throws {
300304
let loadedData = try await fileSystem.fileContents(atPath: environmentFilePath)
@@ -311,7 +315,6 @@ public struct EnvironmentVariablesProvider: Sendable {
311315
environmentVariables: EnvironmentFileParser.parsed(contents),
312316
secretsSpecifier: secretsSpecifier,
313317
bytesDecoder: bytesDecoder,
314-
boolDecoder: boolDecoder,
315318
arraySeparator: arraySeparator
316319
)
317320
}
@@ -408,7 +411,7 @@ extension EnvironmentVariablesProvider.Snapshot {
408411
}
409412
content = .double(doubleValue)
410413
case .bool:
411-
guard let boolValue = boolDecoder.decodeBool(from: stringValue) else {
414+
guard let boolValue = Self.decodeBool(from: stringValue) else {
412415
try throwMismatch()
413416
}
414417
content = .bool(boolValue)
@@ -441,7 +444,7 @@ extension EnvironmentVariablesProvider.Snapshot {
441444
case .boolArray:
442445
let arrayValue = arrayDecoder.decode(stringValue)
443446
let boolArray = try arrayValue.map { stringValue in
444-
guard let boolValue = boolDecoder.decodeBool(from: stringValue) else {
447+
guard let boolValue = Self.decodeBool(from: stringValue) else {
445448
try throwMismatch()
446449
}
447450
return boolValue

Sources/Configuration/ValueCoders/ConfigBoolsFromStringDecoder.swift

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

Tests/ConfigurationTests/ConfigBoolsFromStringDecoderTests.swift

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

Tests/ConfigurationTests/EnvironmentVariablesProviderTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ struct EnvironmentVariablesProviderTests {
6868
#expect(provider.debugDescription == expectedDebugDescription)
6969
}
7070

71+
@Test()
72+
@available(Configuration 1.0, *)
73+
func decodeBoolFromString() throws {
74+
let cases: [(expected: Bool?, input: [String])] = [
75+
(true, ["1"]),
76+
(false, ["0"]),
77+
(true, ["Yes", "yes", "YES", "yES"]),
78+
(false, ["No", "no", "NO", "nO"]),
79+
(true, ["true", "TRUE", "trUe"]),
80+
(false, ["false", "FALSE", "faLse"]),
81+
(nil, ["", "_true_", "_false_", "_yes_", "_no_", "_1_", "_0_", "11", "00"])
82+
]
83+
84+
for (expected, inputs) in cases {
85+
for input in inputs {
86+
#expect(EnvironmentVariablesProvider.Snapshot.decodeBool(from: input) == expected, "input: \(input)")
87+
}
88+
}
89+
}
90+
7191
@available(Configuration 1.0, *)
7292
@Test func valueForKeyOfBoolAndBoolArrayTypes() throws {
7393
let ep = EnvironmentVariablesProvider(

0 commit comments

Comments
 (0)