Skip to content

Commit

Permalink
Add EnvironmentVariable property type
Browse files Browse the repository at this point in the history
  • Loading branch information
drhaynes authored and dhardiman committed Jan 22, 2024
1 parent e9f9164 commit 028f52a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Config/ConfigurationFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func parseNextProperty(properties: [String: Property], pair: (key: String, value
var copy = properties
if let typeHint = PropertyType(rawValue: typeHintValue) {
switch typeHint {
case .string, .url, .encrypted, .encryptionKey, .colour, .image, .regex:
case .string, .url, .encrypted, .encryptionKey, .colour, .image, .regex, .environmentVariable:
copy[pair.key] = ConfigurationProperty<String>(key: pair.key, typeHint: typeHintValue, dict: dict, patterns: patterns)
case .optionalString:
copy[pair.key] = ConfigurationProperty<String?>(key: pair.key, typeHint: typeHintValue, dict: dict, patterns: patterns)
Expand Down
10 changes: 10 additions & 0 deletions Sources/Config/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum PropertyType: String {
case regex = "Regex"
case dynamicColour = "DynamicColour"
case dynamicColourReference = "DynamicColourReference"
case environmentVariable = "EnvironmentVariable"

var typeName: String {
switch self {
Expand All @@ -83,6 +84,8 @@ enum PropertyType: String {
return "UIImage"
case .regex:
return "NSRegularExpression"
case .environmentVariable:
return "String"
default:
return rawValue
}
Expand Down Expand Up @@ -150,6 +153,13 @@ enum PropertyType: String {
return dynamicColourValue(for: value as? [String: String])
case .dynamicColourReference:
return dynamicColourReferenceValue(for: value as? [String: String])
case .environmentVariable:
guard let environmentVariable = value as? String,
let rawValue = getenv(environmentVariable),
let stringValue = String(utf8String: rawValue) else {
return "\"\""
}
return "#\"\(stringValue)\"#"
default:
return "\(value)"
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/ConfigTests/ConfigurationPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ class ConfigurationPropertyTests: XCTestCase {
expect(actualValue).to(equal(expectedValue))
}

func testItCanWriteAnEnvironmentVariableProperty() throws {
// Set an environment variable using the libc API
setenv("SOMETHING", "testValue", 1)
let property = ConfigurationProperty<String>(key: "test", typeHint: "EnvironmentVariable", dict: ["defaultValue": "SOMETHING"])
let expectedValue = ##" static let test: String = #"testValue"#"##
let actualValue = try whenTheDeclarationIsWritten(for: property)
expect(actualValue).to(equal(expectedValue))
}

func testItWritesAnEmptyStringForAnEnvironmentVariableThatIsNotSet() throws {
let property = ConfigurationProperty<String>(key: "test", typeHint: "EnvironmentVariable", dict: ["defaultValue": "DOESNT_EXIST"])
let expectedValue = ##" static let test: String = """##
let actualValue = try whenTheDeclarationIsWritten(for: property)
expect(actualValue).to(equal(expectedValue))
}

func testItCanUseCommonPatternsForOverrides() throws {
let dict: [String: Any] = [
"defaultValue": "test value",
Expand Down

0 comments on commit 028f52a

Please sign in to comment.