Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public struct EnvironmentVariablesProvider: Sendable {

/// A decoder of arrays from a string.
var arrayDecoder: EnvironmentValueArrayDecoder

/// A decoder of bool values from a string.
static func decodeBool(from string: String) -> Bool? {
let stringLowercased = string.lowercased()
return switch stringLowercased {
case "yes", "1": true
case "no", "0": false
default: Bool(stringLowercased)
}
}
}

/// The underlying snapshot of the provider.
Expand Down Expand Up @@ -393,7 +403,7 @@ extension EnvironmentVariablesProvider.Snapshot {
}
content = .double(doubleValue)
case .bool:
guard let boolValue = Bool(stringValue) else {
guard let boolValue = Self.decodeBool(from: stringValue) else {
try throwMismatch()
}
content = .bool(boolValue)
Expand Down Expand Up @@ -426,7 +436,7 @@ extension EnvironmentVariablesProvider.Snapshot {
case .boolArray:
let arrayValue = arrayDecoder.decode(stringValue)
let boolArray = try arrayValue.map { stringValue in
guard let boolValue = Bool(stringValue) else {
guard let boolValue = Self.decodeBool(from: stringValue) else {
try throwMismatch()
}
return boolValue
Expand Down
58 changes: 58 additions & 0 deletions Tests/ConfigurationTests/EnvironmentVariablesProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,64 @@ struct EnvironmentVariablesProviderTests {
#expect(provider.debugDescription == expectedDebugDescription)
}

@available(Configuration 1.0, *)
@Test func decodeBoolFromString() throws {
let sut = EnvironmentVariablesProvider.Snapshot.decodeBool
let cases: [(expected: Bool?, input: [String])] = [
(true, ["1"]),
(false, ["0"]),
(true, ["Yes", "yes", "YES", "yES"]),
(false, ["No", "no", "NO", "nO"]),
(true, ["true", "TRUE", "trUe"]),
(false, ["false", "FALSE", "faLse"]),
(nil, ["", " ", "_true_", "_false_", "_yes_", "_no_", "_1_", "_0_", "11", "00"]),
]
for (expected, inputs) in cases {
for input in inputs {
#expect(sut(input) == expected, "input: \(input)")
}
}
}

@available(Configuration 1.0, *)
@Test func valueForKeyOfBoolAndBoolArrayTypes() throws {
let sut = EnvironmentVariablesProvider(
environmentVariables: [
"BOOL_TRUE": "true",
"BOOL_FALSE": "false",
"BOOL_1": "1",
"BOOL_0": "0",
"BOOL_YES": "YES",
"BOOL_NO": "NO",
"BOOL_THROWS_ERROR_EMPTY": "",
"BOOL_THROWS_ERROR_NOT_BOOL_STRING": "2",
"BOOLY_ARRAY_TRUE": "true,1,,YES",
"BOOLY_ARRAY_FALSE": "false,0,NO",
"BOOLY_ARRAY_THROWS_1": "true,1,YESS",
"BOOLY_ARRAY_THROWS_2": "false,00,no",
"BOOLY_ARRAY_THROWS_3": "false, ,no",
])
#expect(try sut.value(forKey: "BOOL_TRUE", type: .bool).value == true)
#expect(try sut.value(forKey: "BOOL_FALSE", type: .bool).value == false)
#expect(try sut.value(forKey: "BOOL_1", type: .bool).value == true)
#expect(try sut.value(forKey: "BOOL_0", type: .bool).value == false)
#expect(try sut.value(forKey: "BOOL_YES", type: .bool).value == true)
#expect(try sut.value(forKey: "BOOL_NO", type: .bool).value == false)
#expect(throws: ConfigError.self) { try sut.value(forKey: "BOOL_THROWS_ERROR_EMPTY", type: .bool) }
#expect(throws: ConfigError.self) { try sut.value(forKey: "BOOL_THROWS_ERROR_NOT_BOOL_STRING", type: .bool) }
#expect(
try sut.value(forKey: "BOOLY_ARRAY_TRUE", type: .boolArray).value
== .init([true, true, true], isSecret: false)
)
#expect(
try sut.value(forKey: "BOOLY_ARRAY_FALSE", type: .boolArray).value
== .init([false, false, false], isSecret: false)
)
#expect(throws: ConfigError.self) { try sut.value(forKey: "BOOLY_ARRAY_THROWS_1", type: .boolArray) }
#expect(throws: ConfigError.self) { try sut.value(forKey: "BOOLY_ARRAY_THROWS_2", type: .boolArray) }
#expect(throws: ConfigError.self) { try sut.value(forKey: "BOOLY_ARRAY_THROWS_3", type: .boolArray) }
}

@available(Configuration 1.0, *)
@Test func compat() async throws {
try await ProviderCompatTest(provider: provider).runTest()
Expand Down