Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mapping booleans #64

Merged
merged 4 commits into from
May 11, 2017
Merged
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
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There 's another case of "1", "0"

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we could add that :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blame the backend 😄

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertFailure("Contact your backend devs because something is clearly not correct here.")


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onmyway133 what do you think about this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is good 👍


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