From 770a01a2194ae69a37b5feb69b1a0dc1a8d98abf Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Sun, 2 Oct 2016 13:22:28 +0200 Subject: [PATCH 01/14] Updating to Swift 3 --- Carthage/Checkouts/SwiftCheck | 2 +- Carthage/Checkouts/Swiftz | 2 +- Tyro.xcodeproj/project.pbxproj | 6 ++ Tyro/Date.swift | 38 +++++------ Tyro/DecimalNumber.swift | 16 ++--- Tyro/Decoder.swift | 19 +++--- Tyro/EitherExt.swift | 6 +- Tyro/EncodedTypes.swift | 20 +++--- Tyro/Encoder.swift | 82 +++++++++++++++--------- Tyro/Enum.swift | 10 +-- Tyro/Error.swift | 12 ++-- Tyro/Instances.swift | 112 ++++++++++++++++----------------- Tyro/JSON.swift | 4 +- Tyro/JSONKeypath.swift | 8 +-- Tyro/JSONOperators.swift | 26 ++++---- Tyro/JSONValue.swift | 52 ++++++++------- Tyro/NSDataExt.swift | 4 +- Tyro/URL.swift | 19 +++--- TyroTests/DateSpec.swift | 18 +++--- TyroTests/EncoderSpec.swift | 14 ++--- TyroTests/EnumSpec.swift | 6 +- TyroTests/JSONDataSpec.swift | 2 +- TyroTests/JSONValueSpec.swift | 106 ++++++++++++++++--------------- TyroTests/KeypathSpec.swift | 2 +- TyroTests/OperatorsSpec.swift | 2 +- TyroTests/TypesSpec.swift | 2 +- TyroTests/URLSpec.swift | 4 +- TyroTests/UserExample.swift | 6 +- 28 files changed, 322 insertions(+), 278 deletions(-) diff --git a/Carthage/Checkouts/SwiftCheck b/Carthage/Checkouts/SwiftCheck index f214146..983b595 160000 --- a/Carthage/Checkouts/SwiftCheck +++ b/Carthage/Checkouts/SwiftCheck @@ -1 +1 @@ -Subproject commit f2141461f015315cc583222f0bb30ae07da8aad8 +Subproject commit 983b5954f221d36efe3469e8a5155d953034994a diff --git a/Carthage/Checkouts/Swiftz b/Carthage/Checkouts/Swiftz index 78665a0..afe8639 160000 --- a/Carthage/Checkouts/Swiftz +++ b/Carthage/Checkouts/Swiftz @@ -1 +1 @@ -Subproject commit 78665a0921857ce6b1408bd91a5069931cab0191 +Subproject commit afe8639115abd249c85a477b9dea1f0f8c299815 diff --git a/Tyro.xcodeproj/project.pbxproj b/Tyro.xcodeproj/project.pbxproj index 5021c9f..83b808e 100644 --- a/Tyro.xcodeproj/project.pbxproj +++ b/Tyro.xcodeproj/project.pbxproj @@ -786,9 +786,11 @@ }; DE702D0E1BFB28E20017CEE8 = { CreatedOnToolsVersion = 7.1.1; + LastSwiftMigration = 0810; }; DE702D171BFB28E30017CEE8 = { CreatedOnToolsVersion = 7.1.1; + LastSwiftMigration = 0810; }; }; }; @@ -1540,6 +1542,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1561,6 +1564,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -1576,6 +1580,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.typelift.TyroTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1591,6 +1596,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.typelift.TyroTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/Tyro/Date.swift b/Tyro/Date.swift index d3d76bc..4aee188 100644 --- a/Tyro/Date.swift +++ b/Tyro/Date.swift @@ -10,29 +10,29 @@ import Foundation import Swiftz public struct DateTimestampJSONConverter : FromJSON, ToJSON { - public typealias T = NSDate + public typealias T = Date - private init() {} + fileprivate init() {} - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Number(let value): - let date = NSDate(timeIntervalSince1970 : value.doubleValue / 1000.0) + let date = Date(timeIntervalSince1970 : value.doubleValue / 1000.0) return .Right(date) default: - return .Left(.TypeMismatch("NSDate timestamp", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("Date timestamp", "\(type(of: value).self)")) } } - public static func toJSON(date : NSDate) -> Either { - return .Right(.Number(NSNumber(unsignedLongLong : UInt64(date.timeIntervalSince1970 * 1000.0)))) + public static func toJSON(_ date : Date) -> Either { + return .Right(.Number(NSNumber(value : UInt64(date.timeIntervalSince1970 * 1000.0)))) } } public struct DateTimestampJSONFormatter : JSONFormatterType { public typealias T = DateTimestampJSONConverter.T - public private(set) var jsonValue : JSONValue? + public fileprivate(set) var jsonValue : JSONValue? public init(_ jsonValue : JSONValue?) { self.jsonValue = jsonValue @@ -42,35 +42,35 @@ public struct DateTimestampJSONFormatter : JSONFormatterType { jsonValue = nil } - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { return DateTimestampJSONConverter.fromJSON(value) } - public func encodeEither(value : T) -> Either { + public func encodeEither(_ value : T) -> Either { return DateTimestampJSONConverter.toJSON(value) } } public struct DateFormatJSONFormatter : JSONFormatterType { - public typealias T = NSDate + public typealias T = Date public let dateFormat : String public static let DefaultDateFormat = "yyyy'-'MM'-'dd HH':'mm':'ss ZZZ" - public private(set) var jsonValue : JSONValue? + public fileprivate(set) var jsonValue : JSONValue? public init(_ jsonValue : JSONValue?, _ dateFormat : String = DateFormatJSONFormatter.DefaultDateFormat) { self.dateFormat = dateFormat self.jsonValue = jsonValue } - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { switch value { case .String(let value): - let formatter = NSDateFormatter() + let formatter = DateFormatter() formatter.dateFormat = dateFormat - let date = formatter.dateFromString(value) + let date = formatter.date(from: value) if let date = date { return .Right(date) } @@ -78,14 +78,14 @@ public struct DateFormatJSONFormatter : JSONFormatterType { return .Left(.Custom("Could not format value (\(value)) to format (\(formatter.dateFormat))")) } default: - return .Left(.TypeMismatch("NSDate format", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("Date format", "\(type(of: value).self)")) } } - public func encodeEither(value : T) -> Either { - let formatter = NSDateFormatter() + public func encodeEither(_ value : T) -> Either { + let formatter = DateFormatter() formatter.dateFormat = dateFormat - let string = formatter.stringFromDate(value) + let string = formatter.string(from: value) return .Right(.String(string)) } } diff --git a/Tyro/DecimalNumber.swift b/Tyro/DecimalNumber.swift index f292290..d9d5bef 100644 --- a/Tyro/DecimalNumber.swift +++ b/Tyro/DecimalNumber.swift @@ -12,36 +12,38 @@ import Swiftz public struct DecimalNumberJSONConverter : FromJSON, ToJSON { public typealias T = NSDecimalNumber - private init() {} + fileprivate init() {} - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Number(let n): return .Right(NSDecimalNumber(decimal : n.decimalValue)) default: - return .Left(.TypeMismatch("NSDecimalNumber JSON", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("DecimalNumber JSON", "\(type(of: value).self)")) } } - public static func toJSON(dn : NSDecimalNumber) -> Either { + public static func toJSON(_ dn : NSDecimalNumber) -> Either { return .Right(.Number(dn)) } } public struct DecimalNumberJSONFormatter : JSONFormatterType { + public typealias T = DecimalNumberJSONConverter.T - public private(set) var jsonValue : JSONValue? + public fileprivate(set) var jsonValue : JSONValue? public init(_ jsonValue : JSONValue?) { self.jsonValue = jsonValue } - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { return DecimalNumberJSONConverter.fromJSON(value) } - public func encodeEither(value : T) -> Either { + public func encodeEither(_ value : T) -> Either { return DecimalNumberJSONConverter.toJSON(value) } + } diff --git a/Tyro/Decoder.swift b/Tyro/Decoder.swift index 0a40422..5861922 100644 --- a/Tyro/Decoder.swift +++ b/Tyro/Decoder.swift @@ -11,24 +11,25 @@ import Swiftz public protocol JSONDecoderType { associatedtype DecodedType = AnyObject - func decodeEither(value : JSONValue) -> Either - func decode(value : JSONValue) -> DecodedType? + func decodeEither(_ value : JSONValue) -> Either + func decode(_ value : JSONValue) -> DecodedType? } public class JSONDecoder : JSONDecoderType { public static let decoder = JSONDecoder() - private init() {} + fileprivate init() {} - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { switch value { case .Array(let values): - return .Right(values.flatMap { $0.anyObject }) + + return Either.Right( values.flatMap{ $0.anyObject } as AnyObject ) case .Object(let value): - return .Right(value.mapMaybe { $0.anyObject }) + return .Right(value.mapMaybe{ $0.anyObject } as AnyObject) case .Number(let n): return .Right(n) case .String(let s): - return .Right(s) + return .Right(s as AnyObject) case .Null: return .Right(NSNull()) } @@ -36,13 +37,13 @@ public class JSONDecoder : JSONDecoderType { } extension JSONDecoderType { - public func decode(value : JSONValue) -> DecodedType? { + public func decode(_ value : JSONValue) -> DecodedType? { return decodeEither(value).right } } extension JSONValue : JSONDecoderType { - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { return jsonValue.toEither(.Custom("Could not decode JSONValue from JSONValue. There must be a problem.")) } } diff --git a/Tyro/EitherExt.swift b/Tyro/EitherExt.swift index 877ba69..f3c92ea 100644 --- a/Tyro/EitherExt.swift +++ b/Tyro/EitherExt.swift @@ -11,7 +11,7 @@ import Swiftz /// Left-to-right coalescing operator for Either where if the either is left then /// the operator maps to the value provided by `f` otherwise it returns the either right. -public func | (either : Either?, @autoclosure(escaping) f : () -> R?) -> R? { +public func | (either : Either?, f : @autoclosure @escaping () -> R?) -> R? { return either?.either(onLeft: { _ in f() }, onRight: { $0 }) } @@ -40,7 +40,7 @@ extension Array where Element : EitherType { } } - public func eitherMap(f : Element -> Either) -> ([Either], [Either]) { + public func eitherMap(_ f : (Element) -> Either) -> ([Either], [Either]) { let (lefties, righties) = splitFor { $0.left != nil } return (lefties.map(Either.Left), righties.map(Either.Right)) } @@ -48,7 +48,7 @@ extension Array where Element : EitherType { extension Array { /// Splits the array into a tuple with the first array being which elements hold for f and the second array for those elements that do not hold for f. - public func splitFor(f : Element -> Bool) -> ([Element], [Element]) { + public func splitFor(_ f : (Element) -> Bool) -> ([Element], [Element]) { return (takeWhile(f), dropWhile(f)) } } diff --git a/Tyro/EncodedTypes.swift b/Tyro/EncodedTypes.swift index 5199f93..67fa9f4 100644 --- a/Tyro/EncodedTypes.swift +++ b/Tyro/EncodedTypes.swift @@ -9,42 +9,42 @@ import Foundation import Swiftz -public struct FromJSONArray: FromJSON { +public struct FromJSONArray: FromJSON where B.T == A { public typealias T = [A] - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Array(let values): return values.flatMap(B.fromJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: Either.Right) default: - return .Left(.TypeMismatch("\(JSONValue.Array.self)", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("\(JSONValue.Array.self)", "\(type(of: value))")) } } } -public struct ToJSONArray: ToJSON { +public struct ToJSONArray: ToJSON where B.T == A { public typealias T = [A] - public static func toJSON(value : T) -> Either { + public static func toJSON(_ value : T) -> Either { return value.flatMap(B.toJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Array($0)) }) } } -public struct FromJSONDictionary: FromJSON { +public struct FromJSONDictionary: FromJSON where B.T == A { public typealias T = [Swift.String : A] - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Object(let value): return value.mapMaybe(B.fromJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: Either.Right) default: - return .Left(.TypeMismatch("\(JSONValue.Object.self)", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("\(JSONValue.Object.self)", "\(type(of: value).self)")) } } } -public struct ToJSONDictionary: ToJSON { +public struct ToJSONDictionary: ToJSON where B.T == A { public typealias T = [String : A] - public static func toJSON(value : T) -> Either { + public static func toJSON(_ value : T) -> Either { return value.mapMaybe(B.toJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Object($0)) }) } } diff --git a/Tyro/Encoder.swift b/Tyro/Encoder.swift index 5bfb4b0..98d2308 100644 --- a/Tyro/Encoder.swift +++ b/Tyro/Encoder.swift @@ -11,34 +11,39 @@ import Swiftz public protocol JSONEncoderType { associatedtype EncodedType = AnyObject - func encodeEither(value : EncodedType) -> Either - func encode(value : EncodedType) -> JSONValue? + func encodeEither(_ value : EncodedType) -> Either + func encode(_ value : EncodedType) -> JSONValue? - func encodeEither(value : [EncodedType]) -> Either - func encode(value : [EncodedType]) -> JSONValue? + func encodeEither(_ value : [EncodedType]) -> Either + func encode(_ value : [EncodedType]) -> JSONValue? - func encodeEither(value : [String : EncodedType]) -> Either - func encode(value : [String : EncodedType]) -> JSONValue? + func encodeEither(_ value : [String : EncodedType]) -> Either + func encode(_ value : [String : EncodedType]) -> JSONValue? } extension JSONEncoderType { - public func encodeEither(value : [EncodedType]) -> Either { + + public func encodeEither(_ value : [EncodedType]) -> Either { + return value.flatMap(self.encodeEither).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Array($0)) }) } - public func encodeEither(value : [String : EncodedType]) -> Either { + public func encodeEither(_ value : [String : EncodedType]) -> Either { + return value.mapMaybe(self.encodeEither).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Object($0)) }) } - public func encode(value : EncodedType) -> JSONValue? { + public func encode(_ value : EncodedType) -> JSONValue? { return encodeEither(value).right } - public func encode(value : [EncodedType]) -> JSONValue? { + public func encode(_ value : [EncodedType]) -> JSONValue? { + return encodeEither(value).right } - public func encode(value : [String : EncodedType]) -> JSONValue? { + public func encode(_ value : [String : EncodedType]) -> JSONValue? { + return encodeEither(value).right } } @@ -47,7 +52,7 @@ public class JSONEncoder : JSONEncoderType { public static let encoder = JSONEncoder() private init() {} - public func encodeEither(value : AnyObject) -> Either { + public func encodeEither(_ value : AnyObject) -> Either { switch value { case let values as [AnyObject]: return encodeEither(values) @@ -65,70 +70,87 @@ public class JSONEncoder : JSONEncoderType { } } - public func encode(value : AnyObject) -> JSONValue? { + public func encode(_ value : AnyObject) -> JSONValue? { + return encodeEither(value).right } } /// Extra decoders for native types that are not of type AnyObject extension JSONEncoderType { - public static func encodeEither(value : A) -> Either { + public static func encodeEither(_ value : A) -> Either where A.T == A { + return A.toJSON(value) } - public static func encodeEither(value : [A]) -> Either { + public static func encodeEither(_ value : [A]) -> Either where A.T == A { + return value.flatMap(A.toJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Array($0)) }) } - public static func encodeEither(value : [Swift.String : A]) -> Either { + public static func encodeEither(_ value : [Swift.String : A]) -> Either where A.T == A { + return value.mapMaybe(A.toJSON).lift().either(onLeft: { .Left(.Array($0)) }, onRight: { .Right(.Object($0)) }) } - public static func encode(value : A) -> JSONValue? { + public static func encode(_ value : A) -> JSONValue? where A.T == A { + return encodeEither(value).right } - public static func encode(value : [A]) -> JSONValue? { - return encodeEither(value).right + public static func encode(_ value : [A]) -> JSONValue? where A.T == A { + + let eitherJsonValue = encodeEither(value) + return eitherJsonValue.right } - public static func encode(value : [Swift.String : A]) -> JSONValue? { - return encodeEither(value).right + public static func encode(_ value : [Swift.String : A]) -> JSONValue? where A.T == A { + + let eitherJsonValue = encodeEither(value) + return eitherJsonValue.right } } extension JSONValue : JSONEncoderType { public typealias EncodedType = AnyObject - public func encodeEither(value : EncodedType) -> Either { + public func encodeEither(_ value : EncodedType) -> Either { + return JSONEncoder.encoder.encodeEither(value) } - public func encode(value : AnyObject) -> JSONValue? { + public func encode(_ value : AnyObject) -> JSONValue? { + return JSONEncoder.encoder.encode(value) } - public static func encodeEither(value : A) -> Either { - return JSONEncoder.encodeEither(value) + public static func encodeEither(_ value : A) -> Either where A.T == A { + + return A.toJSON(value) } - public static func encodeEither(value : [A]) -> Either { + public static func encodeEither(_ value : [A]) -> Either where A.T == A { + return JSONEncoder.encodeEither(value) } - public static func encodeEither(value : [Swift.String : A]) -> Either { + public static func encodeEither(value : [Swift.String : A]) -> Either where A.T == A { + return JSONEncoder.encodeEither(value) } - public static func encode(value : A) -> JSONValue? { + public static func encode(value : A) -> JSONValue? where A.T == A { + return JSONEncoder.encode(value) } - public static func encode(value : [A]) -> JSONValue? { + public static func encode(value : [A]) -> JSONValue? where A.T == A { + return JSONEncoder.encode(value) } - public static func encode(value : [Swift.String : A]) -> JSONValue? { + public static func encode(value : [Swift.String : A]) -> JSONValue? where A.T == A { + return JSONEncoder.encode(value) } } diff --git a/Tyro/Enum.swift b/Tyro/Enum.swift index c296788..88d9b88 100644 --- a/Tyro/Enum.swift +++ b/Tyro/Enum.swift @@ -11,7 +11,7 @@ import Swiftz /// FromJSON conformance for Int enums extension FromJSON where Self : RawRepresentable, Self.RawValue == Int { - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Number(let n as Int): let x : T? = self.init(rawValue : n) as? T @@ -24,14 +24,14 @@ extension FromJSON where Self : RawRepresentable, Self.RawValue == Int { /// ToJSON conformance for Int enums extension ToJSON where Self : RawRepresentable, Self.RawValue == Int, T == Self { - public static func toJSON(value : T) -> Either { - return .Right(.Number(value.rawValue)) + public static func toJSON(_ value : T) -> Either { + return .Right(.Number(NSNumber(value:value.rawValue))) } } /// FromJSON conformance for String enums extension FromJSON where Self : RawRepresentable, Self.RawValue == String { - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .String(let s): let x : T? = self.init(rawValue : s) as? T @@ -44,7 +44,7 @@ extension FromJSON where Self : RawRepresentable, Self.RawValue == String { /// ToJSON conformance for String enums extension ToJSON where Self : RawRepresentable, Self.RawValue == String, T == Self { - public static func toJSON(value : T) -> Either { + public static func toJSON(_ value : T) -> Either { return .Right(.String(value.rawValue)) } } diff --git a/Tyro/Error.swift b/Tyro/Error.swift index 0455f6b..fb8a2ae 100644 --- a/Tyro/Error.swift +++ b/Tyro/Error.swift @@ -8,12 +8,12 @@ import Foundation -public protocol JSONErrorType : ErrorType {} +public protocol JSONErrorType : Error {} public enum JSONError : JSONErrorType { case Array([JSONError]) // array of errors case TypeMismatch(String, String) // expected / actual - case Error(ErrorType, String) // error / message + case Error(Error, String) // error / message case Custom(String) // message } @@ -21,13 +21,13 @@ extension JSONError : CustomStringConvertible { public var description : String { switch self { case .Array(let errors): - return "JSONError(Array(\"\(errors)\"))" + return "JSONError(array(\"\(errors)\"))" case .TypeMismatch(let expected, let actual): - return "JSONError(TypeMismatch(\"\(expected) is not \(actual)\"))" + return "JSONError(typeMismatch(\"\(expected) is not \(actual)\"))" case .Error(let error, let message): - return "JSONError(Error(\"\(message) error : \(error)\"))" + return "JSONError(error(\"\(message) error : \(error)\"))" case .Custom(let message): - return "JSONError(Custom(\"\(message)\"))" + return "JSONError(custom(\"\(message)\"))" } } } diff --git a/Tyro/Instances.swift b/Tyro/Instances.swift index 1799684..e169ee9 100644 --- a/Tyro/Instances.swift +++ b/Tyro/Instances.swift @@ -11,196 +11,196 @@ import Swiftz /// String extension String : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { 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)")) + return .Left(.TypeMismatch("\(String.self)", "\(type(of: value).self)")) } } } extension String : ToJSON { - public static func toJSON(value : String) -> Either { + public static func toJSON(_ value : String) -> Either { return .Right(.String(value)) } } /// Bool extension Bool : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .Number(0), .Number(false): return .Right(false) case .Number(1), .Number(true): return .Right(true) default: - return .Left(.TypeMismatch("\(Bool.self)", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("\(Bool.self)", "\(type(of: value).self)")) } } } extension Bool : ToJSON { - public static func toJSON(value : Bool) -> Either { - return .Right(.Number(value)) + public static func toJSON(_ value : Bool) -> Either { + return .Right(.Number(NSNumber(value: value))) } } /// Int extension Int : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.integerValue }.toEither(.TypeMismatch("\(Int.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.intValue }.toEither(.TypeMismatch("\(Int.self)", "\(type(of: value).self)")) } } extension Int : ToJSON { - public static func toJSON(value : Int) -> Either { - return .Right(.Number(value)) + public static func toJSON(_ value : Int) -> Either { + return .Right(.Number(NSNumber(value: value))) } } /// Int8 extension Int8 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.charValue }.toEither(.TypeMismatch("\(Int8.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.int8Value }.toEither(.TypeMismatch("\(Int8.self)", "\(type(of: value).self)")) } } extension Int8 : ToJSON { - public static func toJSON(value : Int8) -> Either { - return .Right(.Number(NSNumber(char : value))) + public static func toJSON(_ value : Int8) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// Int16 extension Int16 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.shortValue }.toEither(.TypeMismatch("\(Int16.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.int16Value }.toEither(.TypeMismatch("\(Int16.self)", "\(type(of: value).self)")) } } extension Int16 : ToJSON { - public static func toJSON(value : Int16) -> Either { - return .Right(.Number(NSNumber(short : value))) + public static func toJSON(_ value : Int16) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// Int32 extension Int32 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.intValue }.toEither(.TypeMismatch("\(Int32.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map{ $0.int32Value }.toEither(.TypeMismatch("\(Int32.self)", "\(type(of: value).self)")) } } extension Int32 : ToJSON { - public static func toJSON(value : Int32) -> Either { - return .Right(.Number(NSNumber(int : value))) + public static func toJSON(_ value : Int32) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// Int64 extension Int64 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.longLongValue }.toEither(.TypeMismatch("\(Int64.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.int64Value }.toEither(.TypeMismatch("\(Int64.self)", "\(type(of: value).self)")) } } extension Int64 : ToJSON { - public static func toJSON(value : Int64) -> Either { - return .Right(.Number(NSNumber(longLong : value))) + public static func toJSON(_ value : Int64) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// UInt extension UInt : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.unsignedLongValue }.toEither(.TypeMismatch("\(UInt.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.uintValue }.toEither(.TypeMismatch("\(UInt.self)", "\(type(of: value).self)")) } } extension UInt : ToJSON { - public static func toJSON(value : UInt) -> Either { - return .Right(.Number(NSNumber(unsignedLong : value))) + public static func toJSON(_ value : UInt) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// UInt8 extension UInt8 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.unsignedCharValue }.toEither(.TypeMismatch("\(UInt8.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.uint8Value }.toEither(.TypeMismatch("\(UInt8.self)", "\(type(of: value).self)")) } } extension UInt8 : ToJSON { - public static func toJSON(value : UInt8) -> Either { - return .Right(.Number(NSNumber(unsignedChar : value))) + public static func toJSON(_ value : UInt8) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// UInt16 extension UInt16 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.unsignedShortValue }.toEither(.TypeMismatch("\(UInt16.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.uint16Value }.toEither(.TypeMismatch("\(UInt16.self)", "\(type(of: value).self)")) } } extension UInt16 : ToJSON { - public static func toJSON(value : UInt16) -> Either { - return .Right(.Number(NSNumber(unsignedShort : value))) + public static func toJSON(_ value : UInt16) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// UInt32 extension UInt32 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.unsignedIntValue }.toEither(.TypeMismatch("\(UInt32.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.uint32Value }.toEither(.TypeMismatch("\(UInt32.self)", "\(type(of: value).self)")) } } extension UInt32 : ToJSON { - public static func toJSON(value : UInt32) -> Either { - return .Right(.Number(NSNumber(unsignedInt : value))) + public static func toJSON(_ value : UInt32) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// UInt64 extension UInt64 : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.unsignedLongLongValue }.toEither(.TypeMismatch("\(UInt64.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.uint64Value }.toEither(.TypeMismatch("\(UInt64.self)", "\(type(of: value).self)")) } } extension UInt64 : ToJSON { - public static func toJSON(value : UInt64) -> Either { - return .Right(.Number(NSNumber(unsignedLongLong : value))) + public static func toJSON(_ value : UInt64) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// Float extension Float : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.floatValue }.toEither(.TypeMismatch("\(Float.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.floatValue }.toEither(.TypeMismatch("\(Float.self)", "\(type(of: value).self)")) } } extension Float : ToJSON { - public static func toJSON(value : Float) -> Either { - return .Right(.Number(NSNumber(float : value))) + public static func toJSON(_ value : Float) -> Either { + return .Right(.Number(NSNumber(value : value))) } } /// Double extension Double : FromJSON { - public static func fromJSON(value : JSONValue) -> Either { - return value.number.map { $0.doubleValue }.toEither(.TypeMismatch("\(Double.self)", "\(value.dynamicType.self)")) + public static func fromJSON(_ value : JSONValue) -> Either { + return value.number.map { $0.doubleValue }.toEither(.TypeMismatch("\(Double.self)", "\(type(of: value).self)")) } } extension Double : ToJSON { - public static func toJSON(value : Double) -> Either { - return .Right(.Number(NSNumber(double : value))) + public static func toJSON(_ value : Double) -> Either { + return .Right(.Number(NSNumber(value : value))) } -} \ No newline at end of file +} diff --git a/Tyro/JSON.swift b/Tyro/JSON.swift index f3f7d50..0ad708a 100644 --- a/Tyro/JSON.swift +++ b/Tyro/JSON.swift @@ -11,10 +11,10 @@ import Swiftz public protocol FromJSON { associatedtype T = Self - static func fromJSON(value : JSONValue) -> Either + static func fromJSON(_ value : JSONValue) -> Either } public protocol ToJSON { associatedtype T = Self - static func toJSON(value : T) -> Either + static func toJSON(_ value : T) -> Either } diff --git a/Tyro/JSONKeypath.swift b/Tyro/JSONKeypath.swift index d8029f1..4a91150 100644 --- a/Tyro/JSONKeypath.swift +++ b/Tyro/JSONKeypath.swift @@ -12,7 +12,7 @@ import Swiftz /// Represents a subscript into a nested set of dictionaries. When used in conjunction with the /// JSON decoding machinery, this class can be used to combine strings into keypaths to target /// values inside nested JSON objects. -public struct JSONKeypath : StringLiteralConvertible { +public struct JSONKeypath : ExpressibleByStringLiteral { public typealias StringLiteralType = String public let path : [String] @@ -39,19 +39,19 @@ extension JSONKeypath : Monoid { return JSONKeypath([]) } - public func op(other : JSONKeypath) -> JSONKeypath { + public func op(_ other : JSONKeypath) -> JSONKeypath { return JSONKeypath(self.path + other.path) } } extension JSONKeypath : CustomStringConvertible { public var description : String { - return self.path.intersperse(".").reduce("", combine : +) + return self.path.intersperse(".").reduce("", +) } } extension JSONKeypath { - public func resolve(dictionary : Dictionary) -> JSONValue? { + public func resolve(_ dictionary : Dictionary) -> JSONValue? { if path.isEmpty { return nil } diff --git a/Tyro/JSONOperators.swift b/Tyro/JSONOperators.swift index 3dc11bb..802f9cb 100644 --- a/Tyro/JSONOperators.swift +++ b/Tyro/JSONOperators.swift @@ -11,6 +11,8 @@ import Swiftz // JSONFormatterType decoding operators +infix operator JSONValue? { return lhs?[rhs] } @@ -28,44 +30,44 @@ 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 (lhs : JSONValue?, rhs : JSONKeypath) -> A? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> A? where A.T == A { return lhs?[rhs]?.value() } -public func (lhs : JSONValue?, rhs : JSONKeypath) -> [A]? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> [A]? where A.T == A { return lhs?[rhs]?.value() } -public func (lhs : JSONValue?, rhs : JSONKeypath) -> [String : A]? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> [String : A]? where A.T == A { return lhs?[rhs]?.value() } -public func (lhs : JSONValue?, rhs : JSONKeypath) -> A?? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> A?? where A.T == A { return lhs (lhs : JSONValue?, rhs : JSONKeypath) -> [A]?? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> [A]?? where A.T == A { return lhs (lhs : JSONValue?, rhs : JSONKeypath) -> [String : A]?? { +public func (lhs : JSONValue?, rhs : JSONKeypath) -> [String : A]?? where A.T == A { return lhs (lhs : JSONValue?, rhs : JSONKeypath) throws -> A { +public func (lhs : JSONValue?, rhs : JSONKeypath) throws -> A where A.T == A { if let result : A = (lhs (lhs : JSONValue?, rhs : JSONKeypath } } -public func (lhs : JSONValue?, rhs : JSONKeypath) throws -> [A] { +public func (lhs : JSONValue?, rhs : JSONKeypath) throws -> [A] where A.T == A { if let result : [A] = (lhs (lhs : JSONValue?, rhs : JSONKeypath } } -public func (lhs : JSONValue?, rhs : JSONKeypath) throws -> [String : A] { +public func (lhs : JSONValue?, rhs : JSONKeypath) throws -> [String : A] where A.T == A { if let result : [String : A] = (lhs Bool { case (.String(let lhsValue), .String(let rhsValue)): return lhsValue == rhsValue case (.Number(let lhsValue), .Number(let rhsValue)): - return lhsValue.isEqualToNumber(rhsValue) + return lhsValue.isEqual(to: rhsValue) case (.Null, .Null): return true default: @@ -54,7 +54,7 @@ public func == (lhs : JSONValue, rhs : JSONValue) -> Bool { } public func == (lhs : JSONValue?, rhs : JSONValue?) -> Bool { - if let lhs = lhs, rhs = rhs { + if let lhs = lhs, let rhs = rhs { return lhs == rhs } @@ -109,9 +109,9 @@ extension JSONValue : JSONValueable { public var anyObject : AnyObject? { switch self { - case .Array(let values): return values.mapMaybe { $0.anyObject } - case .Object(let value): return value.mapMaybe { $0.anyObject } - case .String(let s): return s + case .Array(let values): return values.mapMaybe { $0.anyObject } as AnyObject? + case .Object(let value): return value.mapMaybe { $0.anyObject } as AnyObject? + case .String(let s): return s as AnyObject? case .Number(let n): return n case .Null: return NSNull() } @@ -120,31 +120,31 @@ extension JSONValue : JSONValueable { extension JSONValue { /// Values for FromJSON - public func value() -> A? { + public func value() -> A? where A.T == A { return valueEither().right } - public func valueEither() -> Either { + public func valueEither() -> Either where A.T == A { return A.fromJSON(self) } - public func value() -> [A]? { + public func value() -> [A]? where A.T == A { return valueEither().right } - public func valueEither() -> Either { + public func valueEither() -> Either where A.T == A { return FromJSONArray.fromJSON(self) } - public func value() -> [Swift.String : A]? { + public func value() -> [Swift.String : A]? where A.T == A { return valueEither().right } - public func valueEither() -> Either { + public func valueEither() -> Either where A.T == A { return FromJSONDictionary.fromJSON(self) } - public func error(type : A.Type) -> JSONError? { + public func error(_ type : A.Type) -> JSONError? { return type.fromJSON(self).left } } @@ -161,28 +161,32 @@ extension JSONValue { } extension JSONValue { - public static func decodeEither(data : NSData) -> Either { + public static func decodeEither(_ data : Data) -> Either { + do { - let object = try NSJSONSerialization.JSONObjectWithData(data, options : NSJSONReadingOptions(rawValue : 0)) - return JSONEncoder.encoder.encodeEither(object) + let object = try JSONSerialization.jsonObject(with: data, options : JSONSerialization.ReadingOptions(rawValue : 0)) + return JSONEncoder.encoder.encodeEither(object as AnyObject) } catch let error { return .Left(.Error(error, "Error while decoding data with decodeEither")) } } - public static func decodeEither(json : Swift.String) -> Either { - return (decodeEither <^> json.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion : false)) ?? .Left(.Custom("JSON string (\(json)) could not be converted to NSData using UTF-8 string encoding.")) + public static func decodeEither(_ json : Swift.String) -> Either { + + let t = (decodeEither <^> json.data(using: Swift.String.Encoding.utf8, allowLossyConversion : false)) + + return t ?? .Left(.Custom("JSON string (\(json)) could not be converted to Data using UTF-8 string encoding.")) } - public static func decode(data : NSData) -> JSONValue? { + public static func decode(_ data : Data) -> JSONValue? { return decodeEither(data).right } - public static func encodeEither(value : JSONValue) -> Either { - return JSONDecoder.decoder.decodeEither(value).flatMap { (object) -> Either in + public static func encodeEither(_ value : JSONValue) -> Either { + return JSONDecoder.decoder.decodeEither(value).flatMap { (object) -> Either in do { - let data : NSData = try NSJSONSerialization.dataWithJSONObject(object, options : NSJSONWritingOptions(rawValue : 0)) + let data : Data = try JSONSerialization.data(withJSONObject: object, options : JSONSerialization.WritingOptions(rawValue : 0)) return .Right(data) } catch let error { @@ -191,15 +195,15 @@ extension JSONValue { } } - public static func toJSONData(value : JSONValue) -> NSData? { + public static func toJSONData(_ value : JSONValue) -> Data? { return encodeEither(value).right } - public static func toJSONString(value : JSONValue) -> Swift.String? { + public static func toJSONString(_ value : JSONValue) -> Swift.String? { return toJSONData(value)?.toUTF8String() } - public func toJSONData() -> NSData? { + public func toJSONData() -> Data? { return JSONValue.toJSONData(self) } diff --git a/Tyro/NSDataExt.swift b/Tyro/NSDataExt.swift index 01e0826..2687915 100644 --- a/Tyro/NSDataExt.swift +++ b/Tyro/NSDataExt.swift @@ -9,9 +9,9 @@ import Foundation import Swiftz -extension NSData { +extension Data { internal func toUTF8String() -> String? { - return String(data : self, encoding : NSUTF8StringEncoding) + return String(data : self, encoding : String.Encoding.utf8) } } diff --git a/Tyro/URL.swift b/Tyro/URL.swift index 42638f3..dffca02 100644 --- a/Tyro/URL.swift +++ b/Tyro/URL.swift @@ -10,38 +10,39 @@ import Foundation import Swiftz public struct URLJSONConverter : FromJSON, ToJSON { - public typealias T = NSURL + public typealias T = URL - private init() {} + fileprivate init() {} - public static func fromJSON(value : JSONValue) -> Either { + public static func fromJSON(_ value : JSONValue) -> Either { switch value { case .String(let value): - return NSURL(string : value).toEither(.Custom("Could not convert value (\(value)) to NSURL")) + return URL(string : value).toEither(.Custom("Could not convert value (\(value)) to URL")) default: - return .Left(.TypeMismatch("URL JSON", "\(value.dynamicType.self)")) + return .Left(.TypeMismatch("URL JSON", "\(type(of: value).self)")) } } - public static func toJSON(url : NSURL) -> Either { + public static func toJSON(_ url : URL) -> Either { return .Right(.String(url.absoluteString)) } } public struct URLJSONFormatter : JSONFormatterType { + public typealias T = URLJSONConverter.T - public private(set) var jsonValue : JSONValue? + public fileprivate(set) var jsonValue : JSONValue? public init(_ jsonValue : JSONValue?) { self.jsonValue = jsonValue } - public func decodeEither(value : JSONValue) -> Either { + public func decodeEither(_ value : JSONValue) -> Either { return URLJSONConverter.fromJSON(value) } - public func encodeEither(value : T) -> Either { + public func encodeEither(_ value : T) -> Either { return URLJSONConverter.toJSON(value) } } diff --git a/TyroTests/DateSpec.swift b/TyroTests/DateSpec.swift index b46ee3b..e30f89b 100644 --- a/TyroTests/DateSpec.swift +++ b/TyroTests/DateSpec.swift @@ -13,25 +13,25 @@ import Swiftz class DateSpec : XCTestCase { func testDate() { let timestampInMilliseconds: Double = 1443769200000 - let expectedDate = NSDate(timeIntervalSince1970: 1443769200000.0 / 1000.0) + let expectedDate = Date(timeIntervalSince1970: 1443769200000.0 / 1000.0) let datesJson = "{\"lastUpdated\":\(timestampInMilliseconds),\"lastUpdatedCustomFormat\":\"2015-10-02\",\"lastUpdatedThisShouldBeMillisecondsNumber\":\"2015-11-19\",\"lastUpdatedPretty\":\"2015-10-02 07:00:00 +0000\",\"lastUpdatedPrettyWrongFormat\":\"2015-10-02 07:00:00\",\"dates\":[\"2015-10-02 07:00:00 +0000\",\"2015-10-02 08:00:00 +0000\",\"2015-10-02 09:00:00 +0000\"],\"object\":{\"date\":\(timestampInMilliseconds)}}" let result = datesJson.toJSON XCTAssertNotNil(result) - let date1: NSDate? = DateTimestampJSONFormatter(result) { - return UInt.arbitrary >>- { n in - switch n % 100 { - case 0: - return UInt.arbitrary >>- { b in - if b % 2 == 0 { - return Gen.pure(JSONValue.Array([])) - } - return [JSONValue].arbitrary.map(JSONValue.Array) - } - case 1: - return UInt.arbitrary >>- { b in - if b % 2 == 0 { - return Gen.pure(JSONValue.Object([:])) - } - return DictionaryOf.arbitrary.map { JSONValue.Object($0.getDictionary) } - } - case 2: - return Swift.String.arbitrary.map(JSONValue.String) - case 3: - return Bool.arbitrary.map(JSONValue.Number • NSNumber.init) - case 4: - return Int.arbitrary.map { JSONValue.Number($0 as NSNumber) } - case 5: - return Int8.arbitrary.map(JSONValue.Number • NSNumber.init) - case 6: - return Int16.arbitrary.map(JSONValue.Number • NSNumber.init) - case 7: - return Int32.arbitrary.map(JSONValue.Number • NSNumber.init) - case 8: - return Int64.arbitrary.map(JSONValue.Number • NSNumber.init) - case 9: - return UInt.arbitrary.map { JSONValue.Number($0 as NSNumber) } - case 10: - return UInt8.arbitrary.map(JSONValue.Number • NSNumber.init) - case 11: - return UInt16.arbitrary.map(JSONValue.Number • NSNumber.init) - case 12: - return UInt32.arbitrary.map(JSONValue.Number • NSNumber.init) - case 13: - return UInt64.arbitrary.map(JSONValue.Number • NSNumber.init) - case 14: - return Float.arbitrary.map(JSONValue.Number • NSNumber.init) - case 15: - return Double.arbitrary.map(JSONValue.Number • NSNumber.init) - default: - return Gen.pure(JSONValue.Null) - } - } + let t: Gen = UInt.arbitrary + + return t.flatMap { n in + switch n % 100 { + case 0: + return UInt.arbitrary.flatMap { b in + if b % 2 == 0 { + return Gen.pure(JSONValue.Array([])) + } + return [JSONValue].arbitrary.map(JSONValue.Array) + } + case 1: + return UInt.arbitrary.flatMap { b in + if b % 2 == 0 { + return Gen.pure(JSONValue.Object([:])) + } + return DictionaryOf.arbitrary.map { JSONValue.Object($0.getDictionary) } + } + case 2: + return Swift.String.arbitrary.map(JSONValue.String) + case 3: + return Bool.arbitrary.map { JSONValue.Number(NSNumber(value: $0)) } + case 4: + return Int.arbitrary.map { JSONValue.Number($0 as NSNumber) } + case 5: + return Int8.arbitrary.map(JSONValue.Number • NSNumber.init) + case 6: + return Int16.arbitrary.map(JSONValue.Number • NSNumber.init) + case 7: + return Int32.arbitrary.map(JSONValue.Number • NSNumber.init) + case 8: + return Int64.arbitrary.map(JSONValue.Number • NSNumber.init) + case 9: + return UInt.arbitrary.map { JSONValue.Number($0 as NSNumber) } + case 10: + return UInt8.arbitrary.map(JSONValue.Number • NSNumber.init) + case 11: + return UInt16.arbitrary.map(JSONValue.Number • NSNumber.init) + case 12: + return UInt32.arbitrary.map(JSONValue.Number • NSNumber.init) + case 13: + return UInt64.arbitrary.map(JSONValue.Number • NSNumber.init) + case 14: + return Float.arbitrary.map(JSONValue.Number • NSNumber.init) + case 15: + return Double.arbitrary.map { JSONValue.Number(NSNumber(value: $0)) } + default: + return Gen.pure(JSONValue.Null) + } + } + + fatalError() } } -func roundTrip>(_ : T.Type, _ x : JSONValue) -> Testable { - if let xs = T.fromJSON(x).right.map(T.toJSON) { +func roundTrip(_ : T.Type, _ x : JSONValue) -> Testable { + + if let xs = T.fromJSON(x).right.map(T.toJSON) { return xs == Either.Right(x) } + return Discard() } diff --git a/TyroTests/KeypathSpec.swift b/TyroTests/KeypathSpec.swift index ee98e9b..7b24002 100644 --- a/TyroTests/KeypathSpec.swift +++ b/TyroTests/KeypathSpec.swift @@ -17,7 +17,7 @@ class KeypathSpec : XCTestCase { let result = dictionaryJson.toJSONEither?.right XCTAssertNotNil(result) - let bool2: Bool? = result "bool" + let bool2: Bool? = result "bool") XCTAssertNotNil(bool2) XCTAssert(bool2 == true) } diff --git a/TyroTests/OperatorsSpec.swift b/TyroTests/OperatorsSpec.swift index cb4ce49..945b9f4 100644 --- a/TyroTests/OperatorsSpec.swift +++ b/TyroTests/OperatorsSpec.swift @@ -34,7 +34,7 @@ class OperatorsSpec : XCTestCase { } func testRetrieveNested() { - let answer: Int? = json.toJSON "answer" + let answer: Int? = json.toJSON "answer") XCTAssert(answer == 42) } diff --git a/TyroTests/TypesSpec.swift b/TyroTests/TypesSpec.swift index f4567f6..3fa718e 100644 --- a/TyroTests/TypesSpec.swift +++ b/TyroTests/TypesSpec.swift @@ -53,7 +53,7 @@ class TypesFromJSONSpec : XCTestCase { let jsonIntInObject = "{\"int1\":1,\"nestedInt\":{\"answer\":42}}" let int1: Int? = jsonIntInObject.toJSON "answer" + let answer: Int? = jsonIntInObject.toJSON "answer") XCTAssert(jsonIntInObject.toJSON?.object?.keys.count == 2) XCTAssert(int1 == 1) XCTAssert(answer == 42) diff --git a/TyroTests/URLSpec.swift b/TyroTests/URLSpec.swift index 4fe3c08..20cfa30 100644 --- a/TyroTests/URLSpec.swift +++ b/TyroTests/URLSpec.swift @@ -13,8 +13,8 @@ import Swiftz class URLSpec : XCTestCase { func testURL() { let json = "{\"url\":\"https://github.com/mpurland\"}" - let url: NSURL? = URLJSONFormatter(json.toJSON) Either { + static func fromJSON(_ j: JSONValue) -> Either { let id: UInt64? = j "profile" // A nested keypath + let profile: String? = j "profile") // A nested keypath let balance: Double? = j Bool { } func == (lhs: User?, rhs: User?) -> Bool { - if let lhs = lhs, rhs = rhs { + if let lhs = lhs, let rhs = rhs { return lhs == rhs } else { From 52afe24fcd70507d5a1ad4a0d0f202415a99e28d Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Sun, 2 Oct 2016 13:30:21 +0200 Subject: [PATCH 02/14] Updating Travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 441333f..1252ded 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode7.3 +osx_image: xcode8.0 git: submodules: false before_install: From 8598b2e54217cc6d99b773a49f0293f1f5dfcad4 Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Tue, 20 Dec 2016 12:17:05 +0100 Subject: [PATCH 03/14] Specifying swift version for Tyro-iOS --- Tyro.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tyro.xcodeproj/project.pbxproj b/Tyro.xcodeproj/project.pbxproj index 83b808e..f47d23d 100644 --- a/Tyro.xcodeproj/project.pbxproj +++ b/Tyro.xcodeproj/project.pbxproj @@ -1460,6 +1460,7 @@ PRODUCT_NAME = Tyro; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1482,6 +1483,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.typelift.Tyro; PRODUCT_NAME = Tyro; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; From 84b9c1b7639c8ee8c480726bf8f41bf7b9e6e741 Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Tue, 21 Mar 2017 14:20:08 +0100 Subject: [PATCH 04/14] Updating dependencies --- Cartfile | 2 +- Carthage/Checkouts/SwiftCheck | 2 +- Carthage/Checkouts/Swiftz | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cartfile b/Cartfile index 324b768..d7d4207 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "typelift/Swiftz" +github "typelift/Swiftz" ~> 0.6.2 diff --git a/Carthage/Checkouts/SwiftCheck b/Carthage/Checkouts/SwiftCheck index 983b595..e07a71a 160000 --- a/Carthage/Checkouts/SwiftCheck +++ b/Carthage/Checkouts/SwiftCheck @@ -1 +1 @@ -Subproject commit 983b5954f221d36efe3469e8a5155d953034994a +Subproject commit e07a71a07d2b252ea1b47d9a091b5bc6dec3fb07 diff --git a/Carthage/Checkouts/Swiftz b/Carthage/Checkouts/Swiftz index afe8639..82bf915 160000 --- a/Carthage/Checkouts/Swiftz +++ b/Carthage/Checkouts/Swiftz @@ -1 +1 @@ -Subproject commit afe8639115abd249c85a477b9dea1f0f8c299815 +Subproject commit 82bf915555ef1dbffe658c01ae3997d4f40db6ef From c23973b1bac8019d60b7d990bb4172eebe34f1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Tue, 25 Apr 2017 19:04:10 +0800 Subject: [PATCH 05/14] Use correct version of SwiftCheck --- Carthage/Checkouts/SwiftCheck | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Carthage/Checkouts/SwiftCheck b/Carthage/Checkouts/SwiftCheck index e07a71a..0e2d31c 160000 --- a/Carthage/Checkouts/SwiftCheck +++ b/Carthage/Checkouts/SwiftCheck @@ -1 +1 @@ -Subproject commit e07a71a07d2b252ea1b47d9a091b5bc6dec3fb07 +Subproject commit 0e2d31cbf718cc31bb2f80a65760439ecc3ee7fc From 2f81a9cc32a5866079bc0c91490aa75130f61a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Tue, 25 Apr 2017 19:04:28 +0800 Subject: [PATCH 06/14] Specify swift version 3.0 --- Tyro.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tyro.xcodeproj/project.pbxproj b/Tyro.xcodeproj/project.pbxproj index f47d23d..1042fb5 100644 --- a/Tyro.xcodeproj/project.pbxproj +++ b/Tyro.xcodeproj/project.pbxproj @@ -1392,6 +1392,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -1433,6 +1434,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; From 5449d3592cafa80e4b27b597fe48d86a5241c3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Tue, 25 Apr 2017 19:12:43 +0800 Subject: [PATCH 07/14] Changed travis CI image --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1252ded..7d1a781 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode8.0 +osx_image: xcode8.2 git: submodules: false before_install: From 38405825d260e94ba6724a5fe133fa22eca6ba54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Tue, 25 Apr 2017 19:12:54 +0800 Subject: [PATCH 08/14] Update quickly readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bb10765..eede078 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ Tyro ====== -Tyro used to be a Swift library for Functional JSON parsing and encoding. It is -now **deprecated**. Please use your favorite extension of `Dictionary` instead. +Tyro is a Swift library for Functional JSON parsing and encoding. It is now supported on this repo for swift 3 but used +to be maintained by [typelift](https://github.com/typelift/tyro). From aa7576128fb7500ec27ccd481174243f5859a12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Tue, 25 Apr 2017 19:58:08 +0800 Subject: [PATCH 09/14] Remove notifications when travis-ci runs --- .travis.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d1a781..3c9d565 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,3 @@ script: - xcodebuild test -scheme Tyro-iOS -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6s" | xcpretty -c - xcodebuild test -scheme Tyro-tvOS -destination 'platform=tvOS Simulator,name=Apple TV 1080p' | xcpretty -c - xcodebuild build -scheme Tyro-watchOS -destination 'platform=watchOS Simulator,name=Apple Watch - 42mm' | xcpretty -c -notifications: - webhooks: - urls: - - https://webhooks.gitter.im/e/1d781e1bcbabade5de35 - on_success: always - on_failure: always - on_start: always From 5e2d6a952970129476a1e412577a6f9e2e779ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Wed, 26 Apr 2017 17:23:36 +0800 Subject: [PATCH 10/14] Automate release of Cocoapods spec on tag https://fuller.li/posts/automated-cocoapods-releases-with-ci/ --- .travis.yml | 5 +++++ scripts/push.sh | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 scripts/push.sh diff --git a/.travis.yml b/.travis.yml index 3c9d565..6f39019 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,3 +12,8 @@ script: - xcodebuild test -scheme Tyro-iOS -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6s" | xcpretty -c - xcodebuild test -scheme Tyro-tvOS -destination 'platform=tvOS Simulator,name=Apple TV 1080p' | xcpretty -c - xcodebuild build -scheme Tyro-watchOS -destination 'platform=watchOS Simulator,name=Apple Watch - 42mm' | xcpretty -c +deploy: + provider: script + script: ./scripts/push.sh + on: + tags: true diff --git a/scripts/push.sh b/scripts/push.sh new file mode 100644 index 0000000..90ef3b3 --- /dev/null +++ b/scripts/push.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +source ~/.rvm/scripts/rvm +rvm use default +pod trunk push \ No newline at end of file From 4141922f53f8176317d718c33e17197c45ce5045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Thu, 27 Apr 2017 14:20:38 +0800 Subject: [PATCH 11/14] Remove warning --- Tyro.xcodeproj/project.pbxproj | 10 ---------- TyroTests/JSONValueSpec.swift | 2 -- 2 files changed, 12 deletions(-) diff --git a/Tyro.xcodeproj/project.pbxproj b/Tyro.xcodeproj/project.pbxproj index 1042fb5..f59721e 100644 --- a/Tyro.xcodeproj/project.pbxproj +++ b/Tyro.xcodeproj/project.pbxproj @@ -1277,10 +1277,6 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Checkouts/Swiftz/Carthage/Checkouts/SwiftCheck/build/Debug-appletvos", - ); INFOPLIST_FILE = TyroTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.typelift.Tyro-tvOSTests"; @@ -1294,10 +1290,6 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Checkouts/Swiftz/Carthage/Checkouts/SwiftCheck/build/Debug-appletvos", - ); INFOPLIST_FILE = TyroTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.typelift.Tyro-tvOSTests"; @@ -1498,7 +1490,6 @@ FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", - "$(PROJECT_DIR)/Carthage/Checkouts/Swiftz/Carthage/Checkouts/SwiftCheck/build/Debug-iphoneos", ); INFOPLIST_FILE = TyroTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; @@ -1518,7 +1509,6 @@ FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", - "$(PROJECT_DIR)/Carthage/Checkouts/Swiftz/Carthage/Checkouts/SwiftCheck/build/Debug-iphoneos", ); INFOPLIST_FILE = TyroTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; diff --git a/TyroTests/JSONValueSpec.swift b/TyroTests/JSONValueSpec.swift index 478a9c2..a063de0 100644 --- a/TyroTests/JSONValueSpec.swift +++ b/TyroTests/JSONValueSpec.swift @@ -63,8 +63,6 @@ extension JSONValue : Arbitrary { return Gen.pure(JSONValue.Null) } } - - fatalError() } } From 1be394ecf93872ce5a18336b242bfec1760ebc2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Thu, 27 Apr 2017 14:22:02 +0800 Subject: [PATCH 12/14] Update Podspec Note will failed until Swiftz publish its spec. See https://github.com/typelift/Swiftz/issues/327 --- Tyro.podspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tyro.podspec b/Tyro.podspec index 4dd1f75..97a2241 100644 --- a/Tyro.podspec +++ b/Tyro.podspec @@ -1,8 +1,8 @@ Pod::Spec.new do |s| s.name = "Tyro" - s.version = "0.0.8" + s.version = "0.1.0" s.summary = "Functional JSON parsing and encoding." - s.homepage = "https://github.com/typelift/Tyro" + s.homepage = "https://github.com/OpenShelter/Tyro" s.license = { :type => "BSD" } s.authors = { "CodaFi" => "devteam.codafi@gmail.com", "pthariensflame" => "alexanderaltman@me.com", "mpurland" => "m.purland@gmail.com" } @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.ios.deployment_target = "8.0" s.tvos.deployment_target = "9.1" s.watchos.deployment_target = "2.1" - s.source = { :git => "https://github.com/typelift/Tyro.git", :tag => "v#{s.version}", :submodules => true } + s.source = { :git => "https://github.com/OpenShelter/Tyro.git", :tag => "v#{s.version}", :submodules => true } s.source_files = "Tyro/*.swift" s.dependency "Swiftz" end From 4c4c6cd96e5c0ae1b2356e0a67fff4fbfdf24cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Thu, 27 Apr 2017 16:46:03 +0800 Subject: [PATCH 13/14] Disable cocoapods for now --- .travis.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6f39019..1f423f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,14 +6,15 @@ before_install: - git submodule update --init --recursive install: true script: - - pod lib lint + # TODO: reenable when https://github.com/OpenShelter/Tyro/issues/2 solved + # - pod lib lint - set -o pipefail - xcodebuild test -scheme Tyro -configuration Debug | xcpretty -c - xcodebuild test -scheme Tyro-iOS -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6s" | xcpretty -c - xcodebuild test -scheme Tyro-tvOS -destination 'platform=tvOS Simulator,name=Apple TV 1080p' | xcpretty -c - xcodebuild build -scheme Tyro-watchOS -destination 'platform=watchOS Simulator,name=Apple Watch - 42mm' | xcpretty -c -deploy: - provider: script - script: ./scripts/push.sh - on: - tags: true +# deploy: +# provider: script +# script: ./scripts/push.sh +# on: +# tags: true From 6dea737770f0c4b3c7b1286e16ef24ac917cf1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Benaiteau?= Date: Thu, 27 Apr 2017 17:47:04 +0800 Subject: [PATCH 14/14] Update Version --- Tyro/Info.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tyro/Info.plist b/Tyro/Info.plist index eabb8de..abe61f5 100644 --- a/Tyro/Info.plist +++ b/Tyro/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.0.9 + 0.1.0 CFBundleSignature ???? CFBundleVersion