Skip to content

Instantly share code, notes, and snippets.

@filipemp
Created October 20, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipemp/21dd761c9232460d32ef669e34269440 to your computer and use it in GitHub Desktop.
Save filipemp/21dd761c9232460d32ef669e34269440 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
// MARK: Protocols
protocol UserRequestProtocol {
var name: String { get set }
var age: Int { get set }
var password: String { get set }
var likesBacon: Bool { get set }
}
protocol UserViewModelProtocol {
var name: String? { get set }
var age: Int? { get set }
var password: String? { get set }
var likesBacon: Bool? { get set }
}
protocol MapperProtocol {
func mapUserViewModel(userViewModel: UserViewModelProtocol) throws -> UserRequestProtocol?
}
enum MapperError: ErrorType {
case InvalidParameter(String)
}
// MARK: Structs
struct UserRequest: UserRequestProtocol {
var name: String
var age: Int
var password: String
var likesBacon: Bool
}
struct UserViewModel: UserViewModelProtocol {
var name: String?
var age: Int?
var password: String?
var likesBacon: Bool?
}
// MARK: Mapper
class UserViewModelUserRequestMapper: MapperProtocol {
func mapUserViewModel(userViewModel: UserViewModelProtocol) throws -> UserRequestProtocol? {
guard let name = userViewModel.name where name.characters.count > 0 else {
throw MapperError.InvalidParameter("Invalid user name")
}
guard let age = userViewModel.age where age > 14 else {
throw MapperError.InvalidParameter("Invalid user age")
}
guard let password = userViewModel.password where password.characters.count > 4 && password.characters.count < 8 else {
throw MapperError.InvalidParameter("Invalid password")
}
guard let likesBacon = userViewModel.likesBacon where (userViewModel.likesBacon != nil) else {
throw MapperError.InvalidParameter("We don't know if he/she likes bacon!!!")
}
return UserRequest(name: name, age: age, password: password, likesBacon: likesBacon)
}
}
// MARK: Setup
let mapper = UserViewModelUserRequestMapper()
let nullViewModel = UserViewModel(name: nil, age: nil, password: nil, likesBacon: nil)
let knowMyName = UserViewModel(name: "Chris Lattner", age: nil, password: nil, likesBacon: nil)
let dontKnowIfILikeBacon = UserViewModel(name: "Steve Wozniak", age: 66, password: "b>#F{mt>A-%6FZ^w", likesBacon: nil)
let jobs = UserViewModel(name: "Steve Jobs", age: 999, password: "lisa2", likesBacon: true)
let invalidPassword = UserViewModel(name: "John Doe", age: 32, password: "", likesBacon: false)
let passwordTooLongUser = UserViewModel(name: "Andre Sales", age: 24, password: "jackdaniels", likesBacon: true)
var nullResult, knowMyNameResult, dontKnowIfILikeBaconResult, jobsResult, invalidPasswordResult, passwordTooLongUserResult: UserRequestProtocol?
do {
nullResult = try mapper.mapUserViewModel(nullViewModel)
} catch let error {
print("\(error)")
}
do {
knowMyNameResult = try mapper.mapUserViewModel(knowMyName)
} catch let error {
print("\(error)")
}
do {
dontKnowIfILikeBaconResult = try mapper.mapUserViewModel(dontKnowIfILikeBacon)
} catch let error {
print("\(error)")
}
do {
jobsResult = try mapper.mapUserViewModel(jobs)
} catch let error {
print("\(error)")
}
do {
invalidPasswordResult = try mapper.mapUserViewModel(invalidPassword)
} catch let error {
print("\(error)")
}
do {
passwordTooLongUserResult = try mapper.mapUserViewModel(passwordTooLongUser)
} catch let error {
print("\(error)")
}
// MARK: Testing
func check() -> Bool {
return (nullResult == nil) && (knowMyNameResult == nil) && (dontKnowIfILikeBaconResult == nil) && (jobsResult != nil) && (invalidPasswordResult == nil) && (passwordTooLongUserResult == nil)
}
if check() {
print("YOU DID IT, CONGRATZ CHAMP")
} else {
print("LET'S TRY THIS AGAIN, YOU CAN DO IT, BRO!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment