Skip to content

Commit

Permalink
Allow optional ints
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed May 1, 2019
1 parent 301123d commit ec12044
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Config/ConfigurationFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func parseNextProperty(properties: [String: Property], pair: (key: String, value
copy[pair.key] = ConfigurationProperty<Double>(key: pair.key, typeHint: typeHintValue, dict: dict)
case .int:
copy[pair.key] = ConfigurationProperty<Int>(key: pair.key, typeHint: typeHintValue, dict: dict)
case .optionalInt:
copy[pair.key] = ConfigurationProperty<Int?>(key: pair.key, typeHint: typeHintValue, dict: dict)
case .dictionary:
copy[pair.key] = ConfigurationProperty<[String: Any]>(key: pair.key, typeHint: typeHintValue, dict: dict)
case .bool:
Expand Down
7 changes: 7 additions & 0 deletions Sources/Config/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum PropertyType: String {
case encrypted = "Encrypted"
case encryptionKey = "EncryptionKey"
case int = "Int"
case optionalInt = "Int?"
case double = "Double"
case float = "Float"
case dictionary = "Dictionary"
Expand Down Expand Up @@ -119,6 +120,12 @@ enum PropertyType: String {
return "\(value as! Bool)"
case .regex:
return "try! NSRegularExpression(pattern: \(stringValueAllowingOptional(false)), options: [])"
case .optionalInt:
if let int = value as? Int {
return "\(int)"
} else {
fallthrough
}
default:
return "\(value)"
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/ConfigTests/ConfigurationFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ class ConfigurationFileTests: XCTestCase {
public static let float: Float = 0.0
public static let optionalInt: Int? = nil
public static let optionalString: String? = nil
public static let schemeName: String = #"any"#
public static let stringArray: [String] = []
Expand Down Expand Up @@ -422,6 +426,14 @@ private let configurationWithDifferentTypes: [String: Any] = [
"stringArray": [
"type": "[String]",
"defaultValue": []
],
"optionalString": [
"type": "String?",
"defaultValue": nil
],
"optionalInt": [
"type": "Int?",
"defaultValue": nil
]
]

Expand Down
30 changes: 30 additions & 0 deletions Tests/ConfigTests/ConfigurationPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,34 @@ class ConfigurationPropertyTests: XCTestCase {
let actualValue = try whenTheDeclarationIsWritten(for: property)
expect(actualValue).to(equal(expectedValue))
}

func testItCanWriteAnOptionalIntProperty() throws {
let property = ConfigurationProperty<Int?>(key: "test", typeHint: "Int?", dict: [
"defaultValue": NSNull.self
])
let expectedValue = " static let test: Int? = nil"
let actualValue = try whenTheDeclarationIsWritten(for: property)
expect(actualValue).to(equal(expectedValue))
}

func testItCanWriteAnOptionalOverrideIntProperty() throws {
let property = ConfigurationProperty<Int?>(key: "test", typeHint: "Int?", dict: [
"defaultValue": 3,
"overrides": [
"bla": NSNull.self
]
])
let expectedValue = " static let test: Int? = nil"
let actualValue = try whenTheDeclarationIsWritten(for: property, scheme: "bla")
expect(actualValue).to(equal(expectedValue))
}

func testItCanWriteAnOptionalIntPropertyWithAValue() throws {
let property = ConfigurationProperty<Int?>(key: "test", typeHint: "Int?", dict: [
"defaultValue": 7
])
let expectedValue = " static let test: Int? = 7"
let actualValue = try whenTheDeclarationIsWritten(for: property)
expect(actualValue).to(equal(expectedValue))
}
}

0 comments on commit ec12044

Please sign in to comment.