Skip to content

Commit

Permalink
Updated to Swift 3 -- one build error
Browse files Browse the repository at this point in the history
  • Loading branch information
zwaldowski committed Aug 24, 2016
1 parent 4ba8473 commit 924f29d
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 457 deletions.
4 changes: 4 additions & 0 deletions Freddy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@
baseConfigurationReference = DB6ADF811C23617500D77BF1 /* Framework.xcconfig */;
buildSettings = {
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -753,6 +754,7 @@
baseConfigurationReference = DB6ADF811C23617500D77BF1 /* Framework.xcconfig */;
buildSettings = {
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -761,6 +763,7 @@
baseConfigurationReference = DB6ADF831C23617500D77BF1 /* Tests.xcconfig */;
buildSettings = {
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -769,6 +772,7 @@
baseConfigurationReference = DB6ADF831C23617500D77BF1 /* Tests.xcconfig */;
buildSettings = {
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
16 changes: 8 additions & 8 deletions Sources/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ public enum JSON {
/// A case for denoting a boolean with an associated value of `Swift.Bool`.
case Bool(Swift.Bool)
/// A case for denoting null.
case Null
case null
}

// MARK: - Errors

extension JSON {

/// An enum to encapsulate errors that may arise in working with `JSON`.
public enum Error: ErrorType {
public enum Error: ErrorProtocol {
/// The `index` is out of bounds for a JSON array
case IndexOutOfBounds(index: Swift.Int)
case indexOutOfBounds(index: Swift.Int)

/// The `key` was not found in the JSON dictionary
case KeyNotFound(key: Swift.String)
case keyNotFound(key: Swift.String)

/// The JSON is not subscriptable with `type`
case UnexpectedSubscript(type: JSONPathType.Type)
case unexpectedSubscript(type: JSONPathType.Type)

/// Unexpected JSON `value` was found that is not convertible `to` type
case ValueNotConvertible(value: JSON, to: Any.Type)
case valueNotConvertible(value: JSON, to: Any.Type)
}

}
Expand All @@ -66,7 +66,7 @@ public func ==(lhs: JSON, rhs: JSON) -> Bool {
return Double(intL) == dubR
case (.Bool(let bL), .Bool(let bR)):
return bL == bR
case (.Null, .Null):
case (.null, .null):
return true
default:
return false
Expand All @@ -88,7 +88,7 @@ extension JSON: CustomStringConvertible {
case .Double(let double): return Swift.String(double)
case .Int(let int): return Swift.String(int)
case .Bool(let bool): return Swift.String(bool)
case .Null: return "null"
case .null: return "null"
}
}

Expand Down
25 changes: 12 additions & 13 deletions Sources/JSONDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Double: JSONDecodable {
case let .Int(int):
self = Swift.Double(int)
default:
throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Double)
throw JSON.Error.valueNotConvertible(value: json, to: Swift.Double)
}
}

Expand All @@ -53,7 +53,7 @@ extension Int: JSONDecodable {
case let .Int(int):
self = int
default:
throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Int)
throw JSON.Error.valueNotConvertible(value: json, to: Swift.Int)
}
}

Expand All @@ -67,7 +67,6 @@ extension String: JSONDecodable {
/// an instance of `String` cannot be created from the `JSON` value that was
/// passed to this initializer.
public init(json: JSON) throws {

switch json {
case let .String(string):
self = string
Expand All @@ -78,7 +77,7 @@ extension String: JSONDecodable {
case let .Double(double):
self = String(double)
default:
throw JSON.Error.ValueNotConvertible(value: json, to: Swift.String)
throw JSON.Error.valueNotConvertible(value: json, to: Swift.String)
}
}

Expand All @@ -93,7 +92,7 @@ extension Bool: JSONDecodable {
/// passed to this initializer.
public init(json: JSON) throws {
guard case let .Bool(bool) = json else {
throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Bool)
throw JSON.Error.valueNotConvertible(value: json, to: Swift.Bool)
}
self = bool
}
Expand All @@ -110,7 +109,7 @@ extension RawRepresentable where RawValue: JSONDecodable {
public init(json: JSON) throws {
let raw = try json.decode(type: RawValue.self)
guard let value = Self(rawValue: raw) else {
throw JSON.Error.ValueNotConvertible(value: json, to: Self.self)
throw JSON.Error.valueNotConvertible(value: json, to: Self.self)
}
self = value
}
Expand All @@ -123,10 +122,10 @@ internal extension JSON {
/// - returns: An `Array` of `JSON` elements
/// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`.
/// - seealso: `JSON.decode(_:type:)`
static func getArray(json: JSON) throws -> [JSON] {
static func getArray(_ json: JSON) throws -> [JSON] {
// Ideally should be expressed as a conditional protocol implementation on Swift.Array.
guard case let .Array(array) = json else {
throw Error.ValueNotConvertible(value: json, to: Swift.Array<JSON>)
throw Error.valueNotConvertible(value: json, to: Swift.Array<JSON>)
}
return array
}
Expand All @@ -136,10 +135,10 @@ internal extension JSON {
/// - returns: An `Dictionary` of `String` mapping to `JSON` elements
/// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`.
/// - seealso: `JSON.decode(_:type:)`
static func getDictionary(json: JSON) throws -> [Swift.String: JSON] {
static func getDictionary(_ json: JSON) throws -> [Swift.String: JSON] {
// Ideally should be expressed as a conditional protocol implementation on Swift.Dictionary.
guard case let .Dictionary(dictionary) = json else {
throw Error.ValueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, JSON>)
throw Error.valueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, JSON>)
}
return dictionary
}
Expand All @@ -151,7 +150,7 @@ internal extension JSON {
/// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`, as
/// well as any error that arises from decoding the contained values.
/// - seealso: `JSON.decode(_:type:)`
static func getArrayOf<Decoded: JSONDecodable>(json: JSON) throws -> [Decoded] {
static func getArrayOf<Decoded: JSONDecodable>(_ json: JSON) throws -> [Decoded] {
// Ideally should be expressed as a conditional protocol implementation on Swift.Dictionary.
// This implementation also doesn't do the `type = Type.self` trick.
return try getArray(json).map(Decoded.init)
Expand All @@ -163,9 +162,9 @@ internal extension JSON {
/// - throws: One of the `JSON.Error` cases thrown by `decode(_:type:)` or
/// any error that arises from decoding the contained values.
/// - seealso: `JSON.decode(_:type:)`
static func getDictionaryOf<Decoded: JSONDecodable>(json: JSON) throws -> [Swift.String: Decoded] {
static func getDictionaryOf<Decoded: JSONDecodable>(_ json: JSON) throws -> [Swift.String: Decoded] {
guard case let .Dictionary(dictionary) = json else {
throw Error.ValueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, Decoded>)
throw Error.valueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, Decoded>)
}
var decodedDictionary = Swift.Dictionary<Swift.String, Decoded>(minimumCapacity: dictionary.count)
for (key, value) in dictionary {
Expand Down
40 changes: 20 additions & 20 deletions Sources/JSONEncodingDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public struct JSONEncodingDetector {
//// The Unicode encodings looked for during detection
public enum Encoding {
//// UTF-8
case UTF8
case utf8
//// UTF-16 Little Endian
case UTF16LE
case utf16LE
//// UTF-16 Big Endian
case UTF16BE
case utf16BE
//// UTF-32 Little Endian
case UTF32LE
case utf32LE
//// UTF-32 Big Endian
case UTF32BE
case utf32BE
}

//// The Unicode encodings supported by JSONParser.swift
public static let supportedEncodings: [Encoding] = [.UTF8]
public static let supportedEncodings: [Encoding] = [.utf8]

typealias ByteStreamPrefixInformation = (encoding: Encoding, byteOrderMarkLength: Int)

Expand Down Expand Up @@ -56,33 +56,33 @@ public struct JSONEncodingDetector {
////
//// - parameter header: The front Slice of data being read and evaluated.
//// - returns: A tuple containing the detected Unicode encoding and the lenght of the byte order mark.
static func detectEncoding(header: Slice<UnsafeBufferPointer<UInt8>>) -> ByteStreamPrefixInformation {
static func detectEncoding(_ header: RandomAccessSlice<UnsafeBufferPointer<UInt8>>) -> ByteStreamPrefixInformation {

guard let prefix = prefixFromHeader(header) else {
return (.UTF8, 0)
return (.utf8, 0)
}

if let prefixInfo = JSONEncodingDetector.encodingFromBOM(prefix) {
return prefixInfo
} else {
switch prefix {
case(0, 0, 0?, _):
return (.UTF32BE, 0)
return (.utf32BE, 0)
case(_, 0, 0?, 0?):
return (.UTF32LE, 0)
return (.utf32LE, 0)
case (0, _, 0?, _), (0, _, _, _):
return (.UTF16BE, 0)
return (.utf16BE, 0)
case (_, 0, _, 0?), (_, 0, _, _):
return (.UTF16LE, 0)
return (.utf16LE, 0)
default:
return (.UTF8, 0)
return (.utf8, 0)
}
}
}

private typealias EncodingBytePrefix = (UInt8, UInt8, UInt8?, UInt8?)

private static func prefixFromHeader(header: Slice<UnsafeBufferPointer<UInt8>>) -> EncodingBytePrefix? {
private static func prefixFromHeader(_ header: RandomAccessSlice<UnsafeBufferPointer<UInt8>>) -> EncodingBytePrefix? {
if header.count >= 4 {
return(header[0], header[1], header[2], header[3])
} else if header.count >= 2 {
Expand All @@ -91,18 +91,18 @@ public struct JSONEncodingDetector {
return nil
}

private static func encodingFromBOM(prefix: EncodingBytePrefix) -> ByteStreamPrefixInformation? {
private static func encodingFromBOM(_ prefix: EncodingBytePrefix) -> ByteStreamPrefixInformation? {
switch prefix {
case(0xFE, 0xFF, _, _):
return (.UTF16BE, 2)
return (.utf16BE, 2)
case(0x00, 0x00, 0xFE?, 0xFF?):
return (.UTF32BE, 4)
return (.utf32BE, 4)
case(0xEF, 0xBB, 0xBF?, _):
return (.UTF8, 3)
return (.utf8, 3)
case(0xFF, 0xFE, 0?, 0?):
return (.UTF32LE, 4)
return (.utf32LE, 4)
case(0xFF, 0xFE, _, _):
return (.UTF16LE, 2)
return (.utf16LE, 2)
default:
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/JSONLiteralConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension JSON: ArrayLiteralConvertible {

/// Create an instance by copying each element of the `collection` into a
/// new `Array`.
public init<Collection: CollectionType where Collection.Generator.Element == JSON>(_ collection: Collection) {
public init<Collection: Swift.Collection where Collection.Iterator.Element == JSON>(_ collection: Collection) {
self = .Array(Swift.Array(collection))
}

Expand All @@ -29,8 +29,8 @@ extension JSON: DictionaryLiteralConvertible {

/// Create an instance by copying each key/value pair of the `pairs` into
/// a new `Dictionary`.
public init<Dictionary: SequenceType where Dictionary.Generator.Element == (Swift.String, JSON)>(_ pairs: Dictionary) {
var dictionary = Swift.Dictionary<Swift.String, JSON>(minimumCapacity: pairs.underestimateCount())
public init<Dictionary: Sequence where Dictionary.Iterator.Element == (Swift.String, JSON)>(_ pairs: Dictionary) {
var dictionary = Swift.Dictionary<Swift.String, JSON>(minimumCapacity: pairs.underestimatedCount)
for (key, value) in pairs {
dictionary[key] = value
}
Expand Down Expand Up @@ -124,7 +124,7 @@ extension JSON: NilLiteralConvertible {

/// Create an instance initialized with `nil`.
public init(nilLiteral: ()) {
self = .Null
self = .null
}

}
Loading

0 comments on commit 924f29d

Please sign in to comment.