diff --git a/README.md b/README.md index 94a56bd..8642be0 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,7 @@ struct User { let latitude: Double let longitude: Double let admin: Bool - - static func create(id: UInt64)(name: String)(age: Int)(tweets: [String])(profile: String)(balance: Double)(latitude: Double)(longitude: Double)(admin: Bool) -> User { - return User(id: id, name: name, age: age, tweets: tweets, profile: profile, balance: balance, latitude: latitude, longitude: longitude, admin: admin) - } + let optionalNickname: String? } extension User: FromJSON { @@ -49,8 +46,9 @@ extension User: FromJSON { let latitude: Double? = j id <*> name <*> age @@ -59,12 +57,28 @@ extension User: FromJSON { <*> balance <*> latitude <*> longitude - <*> admin).toEither(.Custom("Could not create user")) + <*> admin + <*> optionalNickname).toEither(.Custom("Could not create user")) } } extension User: Equatable {} +func == (lhs: User, rhs: User) -> Bool { + return lhs.id == rhs.id + && lhs.name == rhs.name + && lhs.age == rhs.age + && lhs.tweets == rhs.tweets + && lhs.profile == rhs.profile + && lhs.balance == rhs.balance + && lhs.latitude == rhs.latitude + && lhs.longitude == rhs.longitude + && lhs.admin == rhs.admin + && lhs.optionalNickname == rhs.optionalNickname +} + +extension User: Equatable {} + func == (lhs: User, rhs: User) -> Bool { return lhs.id == rhs.id && lhs.name == rhs.name diff --git a/Tyro/Instances.swift b/Tyro/Instances.swift index 69654a1..1799684 100644 --- a/Tyro/Instances.swift +++ b/Tyro/Instances.swift @@ -15,6 +15,8 @@ extension String : FromJSON { switch value { case .String(let value): return .Right(value) + case .Number(let value): + return .Right(value.stringValue) default: return .Left(.TypeMismatch("\(String.self)", "\(value.dynamicType.self)")) } @@ -201,4 +203,4 @@ extension Double : ToJSON { public static func toJSON(value : Double) -> Either { return .Right(.Number(NSNumber(double : value))) } -} +} \ No newline at end of file diff --git a/Tyro/JSONOperators.swift b/Tyro/JSONOperators.swift index 5ec1ebe..d6f7757 100644 --- a/Tyro/JSONOperators.swift +++ b/Tyro/JSONOperators.swift @@ -24,15 +24,15 @@ public func (lhs : B?, rhs : JSONKeypath) -> [String } public func (lhs : B?, rhs : JSONKeypath) -> B.DecodedType?? { - return lhs (lhs : B?, rhs : JSONKeypath) -> [B.DecodedType]?? { - return lhs (lhs : B?, rhs : JSONKeypath) -> [String : B.DecodedType]?? { - return lhs >(_ : T.Type, _ x : JSONValue) -> T class JSONSpec : XCTestCase { let frequentFliers : [Property] = [ - forAll { (x : JSONValue) in roundTrip(Swift.String.self, x) }, +// forAll { (x : JSONValue) in roundTrip(Swift.String.self, x) }, forAll { (x : JSONValue) in roundTrip(Bool.self, x) }, + // forAll { (x : JSONValue) in roundTrip(Int.self, x) }, // forAll { (x : JSONValue) in roundTrip(Int8.self, x) }, // forAll { (x : JSONValue) in roundTrip(Int16.self, x) }, diff --git a/TyroTests/TypesSpec.swift b/TyroTests/TypesSpec.swift index 1224be8..83f944a 100644 --- a/TyroTests/TypesSpec.swift +++ b/TyroTests/TypesSpec.swift @@ -23,12 +23,6 @@ class TypesFromJSONSpec : XCTestCase { XCTAssert(stringFromObject == "This is a string") } - func testStringInvalid() { - let jsonNotAString = "[1]" - let notAStringArray: [String]? = jsonNotAString.toJSON?.value() - XCTAssertNil(notAStringArray) - } - func testBool() { let jsonBoolInArray = "[true,false]" let boolInArray: [Bool]? = jsonBoolInArray.toJSON?.value() @@ -223,6 +217,21 @@ class TypesFromJSONSpec : XCTestCase { XCTAssertNotNil(pi) XCTAssertEqualWithAccuracy(pi!, doublePI, accuracy: 1.0 / 1_000_000_000_000.0) } + + func testStringFromNumber() { + let json = "{\"id\":122585221112454722}" + let id: UInt64? = json.toJSON User { - return User(id: id, name: name, age: age, tweets: tweets, profile: profile, balance: balance, latitude: latitude, longitude: longitude, admin: admin) - } + let optionalNickname: String? } extension User: FromJSON { @@ -39,8 +36,9 @@ extension User: FromJSON { let latitude: Double? = j id <*> name <*> age @@ -49,7 +47,8 @@ extension User: FromJSON { <*> balance <*> latitude <*> longitude - <*> admin).toEither(.Custom("Could not create user")) + <*> admin + <*> optionalNickname).toEither(.Custom("Could not create user")) } } @@ -65,6 +64,7 @@ func == (lhs: User, rhs: User) -> Bool { && lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude && lhs.admin == rhs.admin + && lhs.optionalNickname == rhs.optionalNickname } func == (lhs: User?, rhs: User?) -> Bool { diff --git a/TyroTests/UserSpec.swift b/TyroTests/UserSpec.swift index 37845cc..267dd5c 100644 --- a/TyroTests/UserSpec.swift +++ b/TyroTests/UserSpec.swift @@ -12,6 +12,7 @@ import Swiftz class UserSpec : XCTestCase { let userJson = "{\"id\": 103622342330925644, \"name\": \"Matthew Purland\", \"age\": 30, \"tweets\": [\"Hello from Tyro\"], \"attributes\": {\"profile\": \"Test Profile\"}, \"balance\": 102.30, \"admin\": true, \"latitude\": 31.75, \"longitude\": 31.75}" + let userJsonWithNickname = "{\"id\": 103622342330925644, \"name\": \"Matthew Purland\", \"age\": 30, \"tweets\": [\"Hello from Tyro\"], \"attributes\": {\"profile\": \"Test Profile\"}, \"balance\": 102.30, \"admin\": true, \"latitude\": 31.75, \"longitude\": 31.75, \"optionalNickname\":\"mpurland\"}" let userJsonWithoutLongitude = "{\"id\": 103622342330925644, \"name\": \"Matthew\", \"age\": 30, \"tweets\": [\"Hello from Tyro\"], \"attributes\": {\"profile\": \"Test Profile\"}, \"balance\": 102.30, \"admin\": true, \"latitude\": 31.75}" func testDecodeUserEither() { @@ -32,6 +33,17 @@ class UserSpec : XCTestCase { XCTAssert(user?.admin == true) XCTAssert(user?.latitude == 31.75) XCTAssert(user?.longitude == 31.75) + XCTAssert(user?.optionalNickname == nil) + + let userWithNickname: User? = userJsonWithNickname.toJSON?.value() + XCTAssertNotNil(userWithNickname) + XCTAssert(userWithNickname?.optionalNickname == "mpurland") + + let nickname1: String?? = userJson.toJSON