Skip to content

Commit

Permalink
Add support for regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Apr 26, 2019
1 parent 09c32b0 commit 4dfabdc
Show file tree
Hide file tree
Showing 3 changed files with 15 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 @@ -98,7 +98,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:
case .string, .url, .encrypted, .encryptionKey, .colour, .image, .regex:
copy[pair.key] = ConfigurationProperty<String>(key: pair.key, typeHint: typeHintValue, dict: dict)
case .double, .float:
copy[pair.key] = ConfigurationProperty<Double>(key: pair.key, typeHint: typeHintValue, dict: dict)
Expand Down
5 changes: 5 additions & 0 deletions Sources/Config/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum PropertyType: String {
case colour = "Colour"
case reference = "Reference"
case image = "Image"
case regex = "Regex"

var typeName: String {
switch self {
Expand All @@ -76,6 +77,8 @@ enum PropertyType: String {
return "UIColor"
case .image:
return "UIImage"
case .regex:
return "NSRegularExpression"
default:
return rawValue
}
Expand Down Expand Up @@ -109,6 +112,8 @@ enum PropertyType: String {
return #"UIImage(named: "\#(value)")!"#
case .bool:
return "\(value as! Bool)"
case .regex:
return "try! NSRegularExpression(pattern: \(stringValue()), options: [])"
default:
return "\(value)"
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/ConfigTests/ConfigurationPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,13 @@ class ConfigurationPropertyTests: XCTestCase {
let overriddenKeyValue = property?.keyValue(for: "hello")
expect(overriddenKeyValue).to(equal("hello value"))
}

func testItCanWriteARegexProperty() throws {
let imageProperty = ConfigurationProperty<String>(key: "test", typeHint: "Regex", dict: [
"defaultValue": "an\\sexpression",
])
let expectedValue = ##" static let test: NSRegularExpression = try! NSRegularExpression(pattern: #"an\sexpression"#, options: [])"##
let actualValue = try whenTheDeclarationIsWritten(for: imageProperty)
expect(actualValue).to(equal(expectedValue))
}
}

0 comments on commit 4dfabdc

Please sign in to comment.