Skip to content

Commit

Permalink
Merge pull request #64 from zenangst/feature/boolean-mapping
Browse files Browse the repository at this point in the history
Add support for mapping booleans
  • Loading branch information
zenangst authored May 11, 2017
2 parents 0bd529e + d028146 commit af2c52b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ let dictionary = [
let model = Person(dictionary)
```

## Resolving value types.

Tailor supports mapping values from dictionaries using type specific functions.

```swift
dictionary.boolean("key")
dictionary.double("key")
dictionary.float("key")
dictionary.int("key")
dictionary.string("key")
```

You can also use `value(forKey:ofType:)`, it works like this.

```swift
dictionary.value(forKey: "key", ofType: Bool.self)
dictionary.value(forKey: "key", ofType: Double.self)
dictionary.value(forKey: "key", ofType: Float.self)
dictionary.value(forKey: "key", ofType: Int.self)
dictionary.value(forKey: "key", ofType: String.self)
```

All of these methods returns an optional value.

## Installation

**Tailor** is available through [CocoaPods](http://cocoapods.org). To install
Expand Down
29 changes: 29 additions & 0 deletions Sources/Shared/Extensions/Dictionary+Tailor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ public extension Dictionary {
return property(name)
}

/// Map value using key to String
///
/// - Parameter name: The name of the key.
/// - Returns: An optional String
func boolean(_ name: String) -> Bool? {
guard let key = name as? Key else {
return nil
}

if let string = self[key] as? String {

if ["true", "1"].contains(string.lowercased()) {
return true
} else if ["false", "0"].contains(string.lowercased()) {
return false
}

return nil
} else if let number = self[key] as? NSNumber {
return number.boolValue
} else if let boolean = self[key] as? Bool {
return boolean
} else {
return nil
}
}

/// A generic method that maps a value from a key to a specific type.
///
/// - Parameters:
Expand All @@ -110,6 +137,8 @@ public extension Dictionary {
return value
} else {
switch ofType {
case is Bool.Type:
return boolean(forKey) as? T
case is Double.Type:
return double(forKey) as? T
case is Float.Type:
Expand Down
9 changes: 9 additions & 0 deletions TailorTests/Shared/DefaultTypesMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class TestMappingDefaultTypes: QuickSpec {
"name" : "Swedish Chef",
"int": 1,
"float": 2.5,
"bool" : true,
"string_bool" : "true",
"string_number_bool" : "1",
"string": "1.25",
"array" : [
"foo", "bar", "baz"
Expand Down Expand Up @@ -40,6 +43,10 @@ class TestMappingDefaultTypes: QuickSpec {
expect(dictionary.value(forKey: "string", ofType: Int.self)).to(equal(1))
expect(dictionary.value(forKey: "string", ofType: String.self)).to(equal("1.25"))

expect(dictionary.value(forKey: "bool", ofType: Bool.self)).to(equal(true))
expect(dictionary.value(forKey: "string_bool", ofType: Bool.self)).to(equal(true))
expect(dictionary.value(forKey: "string_number_bool", ofType: Bool.self)).to(equal(true))

expect(dictionary.string("string")).to(equal("1.25"))

expect(dictionary.float("name")).to(beNil())
Expand All @@ -62,6 +69,8 @@ class TestMappingDefaultTypes: QuickSpec {
expect(dictionary.int("string")).to(equal(1))
expect(dictionary.string("string")).to(equal("1.25"))

expect(dictionary.boolean("string_number_bool")).to(equal(true))

}
}
}
Expand Down

0 comments on commit af2c52b

Please sign in to comment.