diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a82a153 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: swift +osx_image: xcode9.2 +xcode_project: XMLDecoder.xcodeproj +xcode_scheme: XMLDecoder +xcode_sdk: iphonesimulator diff --git a/README.md b/README.md index 986f6c8..8879ae7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,40 @@ # XMLDecoder -XMLDecoder for Swift + +[![Build Status](https://travis-ci.org/uny/XMLDecoder.svg?branch=feature%2Ffirst)](https://travis-ci.org/uny/XMLDecoder) +[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) + +XMLDecoder for Swift. + +## Requirements + +- iOS 8.0+ +- Xcode 9.0+ +- Swift 4.0+ + +## Installation + +### Carthage + +To integrate XMLDecoder into your Xcode project using Carthage, specify it in your `Cartfile`: +``` +github "uny/XMLDecoder" +``` + +## Usage +You can use XMLDecoder like JSONDecoder: +```swift +struct Object { + let string: String + let int: Int +} + +let data = "string100".data(using: .utf8)! +let decoder = XMLDecoder() +do { + let object = try decoder.decode(Object.self, from: data) + print(object) +} catch let error { + // ... +} +``` +XMLDecoder currently does not support attributes. diff --git a/Sources/Info.plist b/Sources/Info.plist new file mode 100644 index 0000000..1007fd9 --- /dev/null +++ b/Sources/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + + diff --git a/Sources/XMLDecoder.h b/Sources/XMLDecoder.h new file mode 100644 index 0000000..736034e --- /dev/null +++ b/Sources/XMLDecoder.h @@ -0,0 +1,18 @@ +// +// XMLDecoder.h +// XMLDecoder +// +// Created by Yuki Nagai on 2017/12/17. +// + +#import + +//! Project version number for XMLDecoder. +FOUNDATION_EXPORT double XMLDecoderVersionNumber; + +//! Project version string for XMLDecoder. +FOUNDATION_EXPORT const unsigned char XMLDecoderVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/Sources/XMLDecoder.swift b/Sources/XMLDecoder.swift new file mode 100644 index 0000000..87935c4 --- /dev/null +++ b/Sources/XMLDecoder.swift @@ -0,0 +1,1479 @@ +// +// XMLDecoder.swift +// XMLDecoder +// +// Created by Yuki Nagai on 2017/12/17. +// + +import Foundation + +/// `XMLDecoder` facilitates the decoding of XML into semantic `Decodable` types. +public final class XMLDecoder { + // MARK: Options + + /// The strategy to use for decoding `Date` values. + public enum DateDecodingStrategy { + /// Defer to `Date` for decoding. This is the default strategy. + case deferredToDate + + /// Decode the `Date` as a UNIX timestamp from a XML number. + case secondsSince1970 + + /// Decode the `Date` as UNIX millisecond timestamp from a XML number. + case millisecondsSince1970 + + /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). + @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) + case iso8601 + + /// Decode the `Date` as a string parsed by the given formatter. + case formatted(DateFormatter) + + /// Decode the `Date` as a custom value decoded by the given closure. + case custom((_ decoder: Decoder) throws -> Date) + } + + /// The strategy to use for decoding `Data` values. + public enum DataDecodingStrategy { + /// Defer to `Data` for decoding. + case deferredToData + + /// Decode the `Data` from a Base64-encoded string. This is the default strategy. + case base64 + + /// Decode the `Data` as a custom value decoded by the given closure. + case custom((_ decoder: Decoder) throws -> Data) + } + + /// The strategy to use for non-XML-conforming floating-point values (IEEE 754 infinity and NaN). + public enum NonConformingFloatDecodingStrategy { + /// Throw upon encountering non-conforming values. This is the default strategy. + case `throw` + + /// Decode the values from the given representation strings. + case convertFromString(positiveInfinity: String, negativeInfinity: String, nan: String) + } + + /// The strategy to use for automatically changing the value of keys before decoding. + public enum KeyDecodingStrategy { + /// Use the keys specified by each type. This is the default strategy. + case useDefaultKeys + + /// Convert from "PascalCaseKeys" to "camelCaseKeys" before attempting to match a key with the one specified by each type. + case convertFromPascalCase + + /// Provide a custom conversion from the key in the encoded XML to the keys specified by the decoded types. + /// The full path to the current decoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before decoding. + /// If the result of the conversion is a duplicate key, then only one value will be present in the container for the type to decode from. + case custom((_ codingPath: [CodingKey]) -> CodingKey) + + fileprivate static func _convertFromPascalCase(_ stringKey: String) -> String { + // Find first lowercase character + if let lowerBound = stringKey.rangeOfCharacter(from: .lowercaseLetters)?.lowerBound { + if lowerBound == stringKey.startIndex { + return stringKey + } else if lowerBound == stringKey.index(stringKey.startIndex, offsetBy: 1) { + let head = String(stringKey.prefix(1)).lowercased() + let tail = String(stringKey.dropFirst()) + return head + tail + } else { + let index = stringKey.index(lowerBound, offsetBy: -1) + let head = String(stringKey[stringKey.startIndex..(_ type: T.Type, from data: Data) throws -> T { + let topLevel: Any + do { + topLevel = try _XMLParser().parse(data) + } catch { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid XML.", underlyingError: error)) + } + + let decoder = _XMLDecoder(referencing: topLevel, options: self.options) + guard let value = try decoder.unbox(topLevel, as: type) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value.")) + } + + return value + } +} + +private final class _XMLDecoder: Decoder { + // MARK: Properties + + /// The decoder's storage. + private(set) var storage: _XMLDecodingStorage + + /// Options set on the top-level decoder. + let options: XMLDecoder._Options + + /// The path to the current point in encoding. + var codingPath: [CodingKey] + + /// Contextual user-provided information for use during encoding. + var userInfo: [CodingUserInfoKey : Any] { + return self.options.userInfo + } + + // MARK: - Initialization + + /// Initializes `self` with the given top-level container and options. + fileprivate init(referencing container: Any, at codingPath: [CodingKey] = [], options: XMLDecoder._Options) { + self.storage = _XMLDecodingStorage() + self.storage.push(container: container) + self.codingPath = codingPath + self.options = options + } + + // MARK: - Decoder Methods + + func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { + guard !(self.storage.topContainer is NSNull) else { + throw DecodingError.valueNotFound(KeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get keyed decoding container -- found null value instead.")) + } + + guard let topContainer = self.storage.topContainer as? _XMLElement else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: _XMLElement.self, reality: self.storage.topContainer) + } + + let children: [(String, _XMLElement)] = topContainer.children + .flatMap { child in child as? _XMLElement } + .map { child in (child.name, child) } + + let container = _XMLKeyedDecodingContainer( + referencing: self, + wrapping: Dictionary(children, uniquingKeysWith: { (first, _) in first })) + return KeyedDecodingContainer(container) + } + + func unkeyedContainer() throws -> UnkeyedDecodingContainer { + guard !(self.storage.topContainer is NSNull) else { + throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get unkeyed decoding container -- found null value instead.")) + } + + guard let topContainer = self.storage.topContainer as? _XMLElement else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: _XMLElement.self, reality: self.storage.topContainer) + } + + let container = topContainer.children.flatMap { child in child as? _XMLElement } + + return _XMLUnkeyedDecodingContainer(referencing: self, wrapping: container) + } + + func singleValueContainer() throws -> SingleValueDecodingContainer { + return self + } +} + +// MARK: - Decoding Storage + +private struct _XMLDecodingStorage { + // MARK: Properties + + /// The container stack. + /// Elements may be any one of the XML types (NSNull, NSNumber, String, Array, [String : Any]). + private(set) var containers: [Any] = [] + + // MARK: - Initialization + + /// Initializes `self` with no containers. + init() {} + + // MARK: - Modifying the Stack + + var count: Int { + return self.containers.count + } + + var topContainer: Any { + precondition(self.containers.count > 0, "Empty container stack.") + return self.containers.last! + } + + mutating func push(container: Any) { + self.containers.append(container) + } + + mutating func popContainer() { + precondition(self.containers.count > 0, "Empty container stack.") + self.containers.removeLast() + } +} + +// MARK: Decoding Containers + +private struct _XMLKeyedDecodingContainer : KeyedDecodingContainerProtocol { + typealias Key = K + + // MARK: Properties + + /// A reference to the decoder we're reading from. + private let decoder: _XMLDecoder + + /// A reference to the container we're reading from. + private let container: [String : Any] + + /// The path of coding keys taken to get to this point in decoding. + private(set) var codingPath: [CodingKey] + + // MARK: - Initialization + + /// Initializes `self` by referencing the given decoder and container. + init(referencing decoder: _XMLDecoder, wrapping container: [String : Any]) { + self.decoder = decoder + switch decoder.options.keyDecodingStrategy { + case .useDefaultKeys: + self.container = container + case .convertFromPascalCase: + // Convert the pascal case keys in the container to camel case. + // If we hit a duplicate key after conversion, then we'll use the first one we saw. Effectively an undefined behavior with XML dictionaries. + self.container = Dictionary(container.map { + key, value in (XMLDecoder.KeyDecodingStrategy._convertFromPascalCase(key), value) + }, uniquingKeysWith: { (first, _) in first }) + case .custom(let converter): + self.container = Dictionary(container.map { + key, value in (converter(decoder.codingPath + [_XMLKey(stringValue: key, intValue: nil)]).stringValue, value) + }, uniquingKeysWith: { (first, _) in first }) + } + self.codingPath = decoder.codingPath + } + + // MARK: - KeyedDecodingContainerProtocol Methods + + var allKeys: [Key] { + return self.container.keys.flatMap { Key(stringValue: $0) } + } + + func contains(_ key: Key) -> Bool { + return self.container[key.stringValue] != nil + } + + private func _errorDescription(of key: CodingKey) -> String { + return "\(key) (\"\(key.stringValue)\")" + } + + func decodeNil(forKey key: Key) throws -> Bool { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + return entry is NSNull + } + + func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Bool.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Int.Type, forKey key: Key) throws -> Int { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Int.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Int8.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Int16.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Int32.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Int64.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: UInt.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: UInt8.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: UInt16.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: UInt32.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: UInt64.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Float.Type, forKey key: Key) throws -> Float { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Float.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: Double.Type, forKey key: Key) throws -> Double { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: Double.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: String.Type, forKey key: Key) throws -> String { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: String.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func decode(_ type: T.Type, forKey key: Key) throws -> T { + guard let entry = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(self._errorDescription(of: key)).")) + } + + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = try self.decoder.unbox(entry, as: type) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) + } + + return value + } + + func nestedContainer(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer { + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get \(KeyedDecodingContainer.self) -- no value found for key \(self._errorDescription(of: key))")) + } + + guard let dictionary = value as? [String : Any] else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value) + } + + let container = _XMLKeyedDecodingContainer(referencing: self.decoder, wrapping: dictionary) + return KeyedDecodingContainer(container) + } + + func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer { + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + guard let value = self.container[key.stringValue] else { + throw DecodingError.keyNotFound(key, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get UnkeyedDecodingContainer -- no value found for key \(self._errorDescription(of: key))")) + } + + guard let array = value as? [Any] else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value) + } + + return _XMLUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) + } + + private func _superDecoder(forKey key: CodingKey) throws -> Decoder { + self.decoder.codingPath.append(key) + defer { self.decoder.codingPath.removeLast() } + + let value: Any = self.container[key.stringValue] ?? NSNull() + return _XMLDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options) + } + + func superDecoder() throws -> Decoder { + return try self._superDecoder(forKey: _XMLKey.super) + } + + func superDecoder(forKey key: Key) throws -> Decoder { + return try self._superDecoder(forKey: key) + } +} + +private struct _XMLUnkeyedDecodingContainer : UnkeyedDecodingContainer { + // MARK: Properties + + /// A reference to the decoder we're reading from. + private let decoder: _XMLDecoder + + /// A reference to the container we're reading from. + private let container: [Any] + + /// The path of coding keys taken to get to this point in decoding. + private(set) var codingPath: [CodingKey] + + /// The index of the element we're about to decode. + private(set) var currentIndex: Int + + // MARK: - Initialization + + /// Initializes `self` by referencing the given decoder and container. + fileprivate init(referencing decoder: _XMLDecoder, wrapping container: [Any]) { + self.decoder = decoder + self.container = container + self.codingPath = decoder.codingPath + self.currentIndex = 0 + } + + // MARK: - UnkeyedDecodingContainer Methods + + var count: Int? { + return self.container.count + } + + var isAtEnd: Bool { + return self.currentIndex >= self.count! + } + + mutating func decodeNil() throws -> Bool { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + if self.container[self.currentIndex] is NSNull { + self.currentIndex += 1 + return true + } else { + return false + } + } + + mutating func decode(_ type: Bool.Type) throws -> Bool { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Bool.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Int.Type) throws -> Int { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Int8.Type) throws -> Int8 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int8.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Int16.Type) throws -> Int16 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int16.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Int32.Type) throws -> Int32 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int32.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Int64.Type) throws -> Int64 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int64.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: UInt.Type) throws -> UInt { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: UInt8.Type) throws -> UInt8 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt8.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: UInt16.Type) throws -> UInt16 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt16.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: UInt32.Type) throws -> UInt32 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt32.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: UInt64.Type) throws -> UInt64 { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt64.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Float.Type) throws -> Float { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Float.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: Double.Type) throws -> Double { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Double.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: String.Type) throws -> String { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: String.self) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func decode(_ type: T.Type) throws -> T { + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) + } + + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: type) else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_XMLKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) + } + + self.currentIndex += 1 + return decoded + } + + mutating func nestedContainer(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer { + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(KeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get nested keyed container -- unkeyed container is at end.")) + } + + let value = self.container[self.currentIndex] + guard !(value is NSNull) else { + throw DecodingError.valueNotFound(KeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get keyed decoding container -- found null value instead.")) + } + + guard let dictionary = value as? [String : Any] else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value) + } + + self.currentIndex += 1 + let container = _XMLKeyedDecodingContainer(referencing: self.decoder, wrapping: dictionary) + return KeyedDecodingContainer(container) + } + + mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer { + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get nested keyed container -- unkeyed container is at end.")) + } + + let value = self.container[self.currentIndex] + guard !(value is NSNull) else { + throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get keyed decoding container -- found null value instead.")) + } + + guard let array = value as? [Any] else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value) + } + + self.currentIndex += 1 + return _XMLUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) + } + + mutating func superDecoder() throws -> Decoder { + self.decoder.codingPath.append(_XMLKey(index: self.currentIndex)) + defer { self.decoder.codingPath.removeLast() } + + guard !self.isAtEnd else { + throw DecodingError.valueNotFound(Decoder.self, + DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Cannot get superDecoder() -- unkeyed container is at end.")) + } + + let value = self.container[self.currentIndex] + self.currentIndex += 1 + return _XMLDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options) + } +} + +extension _XMLDecoder : SingleValueDecodingContainer { + // MARK: SingleValueDecodingContainer Methods + + private func expectNonNull(_ type: T.Type) throws { + guard !self.decodeNil() else { + throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected \(type) but found null value instead.")) + } + } + + func decodeNil() -> Bool { + return self.storage.topContainer is NSNull + } + + func decode(_ type: Bool.Type) throws -> Bool { + try expectNonNull(Bool.self) + return try self.unbox(self.storage.topContainer, as: Bool.self)! + } + + func decode(_ type: Int.Type) throws -> Int { + try expectNonNull(Int.self) + return try self.unbox(self.storage.topContainer, as: Int.self)! + } + + func decode(_ type: Int8.Type) throws -> Int8 { + try expectNonNull(Int8.self) + return try self.unbox(self.storage.topContainer, as: Int8.self)! + } + + func decode(_ type: Int16.Type) throws -> Int16 { + try expectNonNull(Int16.self) + return try self.unbox(self.storage.topContainer, as: Int16.self)! + } + + func decode(_ type: Int32.Type) throws -> Int32 { + try expectNonNull(Int32.self) + return try self.unbox(self.storage.topContainer, as: Int32.self)! + } + + func decode(_ type: Int64.Type) throws -> Int64 { + try expectNonNull(Int64.self) + return try self.unbox(self.storage.topContainer, as: Int64.self)! + } + + func decode(_ type: UInt.Type) throws -> UInt { + try expectNonNull(UInt.self) + return try self.unbox(self.storage.topContainer, as: UInt.self)! + } + + func decode(_ type: UInt8.Type) throws -> UInt8 { + try expectNonNull(UInt8.self) + return try self.unbox(self.storage.topContainer, as: UInt8.self)! + } + + func decode(_ type: UInt16.Type) throws -> UInt16 { + try expectNonNull(UInt16.self) + return try self.unbox(self.storage.topContainer, as: UInt16.self)! + } + + func decode(_ type: UInt32.Type) throws -> UInt32 { + try expectNonNull(UInt32.self) + return try self.unbox(self.storage.topContainer, as: UInt32.self)! + } + + func decode(_ type: UInt64.Type) throws -> UInt64 { + try expectNonNull(UInt64.self) + return try self.unbox(self.storage.topContainer, as: UInt64.self)! + } + + func decode(_ type: Float.Type) throws -> Float { + try expectNonNull(Float.self) + return try self.unbox(self.storage.topContainer, as: Float.self)! + } + + func decode(_ type: Double.Type) throws -> Double { + try expectNonNull(Double.self) + return try self.unbox(self.storage.topContainer, as: Double.self)! + } + + func decode(_ type: String.Type) throws -> String { + try expectNonNull(String.self) + return try self.unbox(self.storage.topContainer, as: String.self)! + } + + func decode(_ type: T.Type) throws -> T { + try expectNonNull(type) + return try self.unbox(self.storage.topContainer, as: type)! + } +} + +// MARK: - Concrete Value Representations + +extension _XMLDecoder { + /// Returns the given value unboxed from a container. + func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? { + if let bool = value as? Bool { + return bool + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + switch string { + case "0", "false": + return false + default: + return true + } + } + + func unbox(_ value: Any, as type: Int.Type) throws -> Int? { + if let int = value as? Int { + return int + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Int(string) + } + + func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? { + if let int8 = value as? Int8 { + return int8 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Int8(string) + } + + func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? { + if let int16 = value as? Int16 { + return int16 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Int16(string) + } + + func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? { + if let int32 = value as? Int32 { + return int32 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Int32(string) + } + + func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? { + if let int64 = value as? Int64 { + return int64 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Int64(string) + } + + func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? { + if let uint = value as? UInt { + return uint + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return UInt(string) + } + + func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? { + if let uint8 = value as? UInt8 { + return uint8 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return UInt8(string) + } + + func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? { + if let uint16 = value as? UInt16 { + return uint16 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return UInt16(string) + } + + func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? { + if let uint32 = value as? UInt32 { + return uint32 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return UInt32(string) + } + + func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? { + if let uint64 = value as? UInt64 { + return uint64 + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return UInt64(string) + } + + func unbox(_ value: Any, as type: Float.Type) throws -> Float? { + if let float = value as? Float { + return float + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Float(string) + } + + func unbox(_ value: Any, as type: Double.Type) throws -> Double? { + if let double = value as? Double { + return double + } + guard let string = try self.unbox(value, as: String.self) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return Double(string) + } + + func unbox(_ value: Any, as type: String.Type) throws -> String? { + let text: String + switch value { + case let element as _XMLElement: + text = element.text + case let string as String: + text = string + case let convertible as CustomStringConvertible: + text = convertible.description + default: + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + return text + } + + func unbox(_ value: Any, as type: Date.Type) throws -> Date? { + guard !(value is NSNull) else { return nil } + + switch self.options.dateDecodingStrategy { + case .deferredToDate: + self.storage.push(container: value) + let date = try Date(from: self) + self.storage.popContainer() + return date + + case .secondsSince1970: + let double = try self.unbox(value, as: Double.self)! + return Date(timeIntervalSince1970: double) + + case .millisecondsSince1970: + let double = try self.unbox(value, as: Double.self)! + return Date(timeIntervalSince1970: double / 1000.0) + + case .iso8601: + if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) { + let string = try self.unbox(value, as: String.self)! + guard let date = _iso8601Formatter.date(from: string) else { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted.")) + } + + return date + } else { + fatalError("ISO8601DateFormatter is unavailable on this platform.") + } + + case .formatted(let formatter): + let string = try self.unbox(value, as: String.self)! + guard let date = formatter.date(from: string) else { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter.")) + } + + return date + + case .custom(let closure): + self.storage.push(container: value) + let date = try closure(self) + self.storage.popContainer() + return date + } + } + + func unbox(_ value: Any, as type: Data.Type) throws -> Data? { + guard !(value is NSNull) else { return nil } + + switch self.options.dataDecodingStrategy { + case .deferredToData: + self.storage.push(container: value) + let data = try Data(from: self) + self.storage.popContainer() + return data + + case .base64: + guard let string = value as? String else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + } + + guard let data = Data(base64Encoded: string) else { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64.")) + } + + return data + + case .custom(let closure): + self.storage.push(container: value) + let data = try closure(self) + self.storage.popContainer() + return data + } + } + + func unbox(_ value: Any, as type: Decimal.Type) throws -> Decimal? { + guard !(value is NSNull) else { return nil } + + // Attempt to bridge from NSDecimalNumber. + if let decimal = value as? Decimal { + return decimal + } else { + let doubleValue = try self.unbox(value, as: Double.self)! + return Decimal(doubleValue) + } + } + + func unbox(_ value: Any, as type: T.Type) throws -> T? { + let decoded: T + if type == Date.self || type == NSDate.self { + guard let date = try self.unbox(value, as: Date.self) else { return nil } + decoded = date as! T + } else if type == Data.self || type == NSData.self { + guard let data = try self.unbox(value, as: Data.self) else { return nil } + decoded = data as! T + } else if type == URL.self || type == NSURL.self { + guard let urlString = try self.unbox(value, as: String.self) else { + return nil + } + + guard let url = URL(string: urlString) else { + throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, + debugDescription: "Invalid URL string.")) + } + + decoded = (url as! T) + } else if type == Decimal.self || type == NSDecimalNumber.self { + guard let decimal = try self.unbox(value, as: Decimal.self) else { return nil } + decoded = decimal as! T + } else { + self.storage.push(container: value) + decoded = try type.init(from: self) + self.storage.popContainer() + } + + return decoded + } +} + +// MARK: - XML Parser +private final class _XMLParser: NSObject, XMLParserDelegate { + private var stack = _ElementStack() + + func parse(_ data: Data) throws -> _XMLElement { + self.stack.removeAll() + let reference = _XMLElement(name: "XMLDecoder.Reference") + self.stack.push(reference) + let parser = XMLParser(data: data) + parser.delegate = self + _ = parser.parse() + if let error = parser.parserError { + throw error + } + let root = reference.children.flatMap { child in child as? _XMLElement } + if root.count == 1 { + return root[0] + } else { + return reference + } + } + + func parser( + _ parser: XMLParser, + didStartElement elementName: String, + namespaceURI: String?, + qualifiedName qName: String?, + attributes attributeDict: [String : String] = [:]) { + let current = self.stack.top().addElement(name: elementName) + self.stack.push(current) + } + + func parser(_ parser: XMLParser, foundCharacters string: String) { + self.stack.top().addText(string) + } + + func parser( + _ parser: XMLParser, + didEndElement elementName: String, + namespaceURI: String?, + qualifiedName qName: String?) { + self.stack.drop() + } + + private struct _ElementStack { + var items: [_XMLElement] = [] + + mutating func push(_ item: _XMLElement) { + self.items.append(item) + } + + mutating func pop() -> _XMLElement { + return self.items.removeLast() + } + + mutating func drop() { + _ = self.pop() + } + + mutating func removeAll() { + self.items.removeAll(keepingCapacity: false) + } + + func top() -> _XMLElement { + return self.items[self.items.count - 1] + } + } +} + +private final class _XMLElement { + let name: String + private(set) var children: [Any] = [] + + var text: String { + return self.children + .flatMap { child in child as? String } + .reduce("") { accumulator, child in accumulator + child } + } + + init(name: String) { + self.name = name + } + + func addElement(name: String) -> _XMLElement { + let element = _XMLElement(name: name) + self.children.append(element) + return element + } + + func addText(_ text: String) { + self.children.append(text) + } +} + +// MARK: - Shared Key Types +private struct _XMLKey : CodingKey { + var stringValue: String + var intValue: Int? + + init?(stringValue: String) { + self.stringValue = stringValue + self.intValue = nil + } + + init?(intValue: Int) { + self.stringValue = "\(intValue)" + self.intValue = intValue + } + + init(stringValue: String, intValue: Int?) { + self.stringValue = stringValue + self.intValue = intValue + } + + init(index: Int) { + self.stringValue = "Index \(index)" + self.intValue = index + } + + static let `super` = _XMLKey(stringValue: "super")! +} + +// MARK: - Shared ISO8601 Date Formatter +// NOTE: This value is implicitly lazy and _must_ be lazy. We're compiled against the latest SDK (w/ ISO8601DateFormatter), but linked against whichever Foundation the user has. ISO8601DateFormatter might not exist, so we better not hit this code path on an older OS. +@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) +private var _iso8601Formatter: ISO8601DateFormatter = { + let formatter = ISO8601DateFormatter() + formatter.formatOptions = .withInternetDateTime + return formatter +}() + +// MARK: - Error Utilities +extension DecodingError { + /// Returns a `.typeMismatch` error describing the expected type. + /// + /// - parameter path: The path of `CodingKey`s taken to decode a value of this type. + /// - parameter expectation: The type expected to be encountered. + /// - parameter reality: The value that was encountered instead of the expected type. + /// - returns: A `DecodingError` with the appropriate path and debug description. + fileprivate static func _typeMismatch(at path: [CodingKey], expectation: Any.Type, reality: Any) -> DecodingError { + let description = "Expected to decode \(expectation) but found \(_typeDescription(of: reality)) instead." + return .typeMismatch(expectation, Context(codingPath: path, debugDescription: description)) + } + + /// Returns a description of the type of `value` appropriate for an error message. + /// + /// - parameter value: The value whose type to describe. + /// - returns: A string describing `value`. + /// - precondition: `value` is one of the types below. + fileprivate static func _typeDescription(of value: Any) -> String { + if value is NSNull { + return "a null value" + } else if value is NSNumber /* FIXME: If swift-corelibs-foundation isn't updated to use NSNumber, this check will be necessary: || value is Int || value is Double */ { + return "a number" + } else if value is String { + return "a string/data" + } else if value is [Any] { + return "an array" + } else if value is [String : Any] { + return "a dictionary" + } else { + return "\(type(of: value))" + } + } +} diff --git a/Tests/Info.plist b/Tests/Info.plist new file mode 100644 index 0000000..6c40a6c --- /dev/null +++ b/Tests/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Tests/XMLDecoderTests.swift b/Tests/XMLDecoderTests.swift new file mode 100644 index 0000000..e486752 --- /dev/null +++ b/Tests/XMLDecoderTests.swift @@ -0,0 +1,105 @@ +// +// XMLDecoderTests.swift +// XMLDecoderTests +// +// Created by Yuki Nagai on 2017/12/17. +// + +import XCTest +@testable import XMLDecoder + +final class XMLDecoderTests: XCTestCase { + func testArray() { + let string = "" + + "Belgian Waffles5.95" + + "Strawberry Belgian Waffles7.95" + let data = string.data(using: .utf8)! + let decoder = XMLDecoder() + do { + let menu = try decoder.decode(Menu.self, from: data) + XCTAssertEqual(menu.foods.count, 2) + XCTAssertEqual(menu.foods.first?.name, "Belgian Waffles") + XCTAssertEqual(menu.foods.first?.price, 5.95) + XCTAssertEqual(menu.foods.last?.name, "Strawberry Belgian Waffles") + XCTAssertEqual(menu.foods.last?.price, 7.95) + } catch let error { + XCTFail(error.localizedDescription) + } + } + + func testPascalCaseKeys() { + let string = "" + + "https://github.comhttps://github.com" + + "https://github.comhttps://github.com" + let data = string.data(using: .utf8)! + let decoder = XMLDecoder() + decoder.keyDecodingStrategy = .convertFromPascalCase + do { + let root = try decoder.decode(PascalCaseKeys.self, from: data) + XCTAssertEqual(root.url, URL(string: "https://github.com")) + XCTAssertEqual(root.urlString, "https://github.com") + XCTAssertEqual(root.capitalized, "https://github.com") + XCTAssertEqual(root.lowercased, "https://github.com") + } catch let error { + XCTFail(error.localizedDescription) + } + } + + func testNumberValues() { + let string = "" + + "-128-8-16-32-64" + + "1288163264" + + "3.143.14" + let data = string.data(using: .utf8)! + let decoder = XMLDecoder() + do { + let root = try decoder.decode(NumberValues.self, from: data) + XCTAssertEqual(root.int, -128) + XCTAssertEqual(root.int8, -8) + XCTAssertEqual(root.int16, -16) + XCTAssertEqual(root.int32, -32) + XCTAssertEqual(root.int64, -64) + XCTAssertEqual(root.uint, 128) + XCTAssertEqual(root.uint8, 8) + XCTAssertEqual(root.uint16, 16) + XCTAssertEqual(root.uint32, 32) + XCTAssertEqual(root.uint64, 64) + XCTAssertEqual(root.float, 3.14) + XCTAssertEqual(root.double, 3.14) + } catch let error { + XCTFail(error.localizedDescription) + } + } + + // MARK: - Nested Objects + private struct Menu: Decodable { + let foods: [Food] + + struct Food: Decodable { + let name: String + let price: Double + } + } + + private struct PascalCaseKeys: Decodable { + let url: URL + let urlString: String + let capitalized: String + let lowercased: String + } + + private struct NumberValues: Decodable { + let int: Int + let int8: Int8 + let int16: Int16 + let int32: Int32 + let int64: Int64 + let uint: UInt + let uint8: UInt8 + let uint16: UInt16 + let uint32: UInt32 + let uint64: UInt64 + let float: Float + let double: Double + } +} diff --git a/XMLDecoder.xcodeproj/project.pbxproj b/XMLDecoder.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0546669 --- /dev/null +++ b/XMLDecoder.xcodeproj/project.pbxproj @@ -0,0 +1,441 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + AAFC7BC81FE6458C004823ED /* XMLDecoder.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AAFC7BBE1FE6458C004823ED /* XMLDecoder.framework */; }; + AAFC7BCD1FE6458C004823ED /* XMLDecoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAFC7BCC1FE6458C004823ED /* XMLDecoderTests.swift */; }; + AAFC7BCF1FE6458C004823ED /* XMLDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = AAFC7BC11FE6458C004823ED /* XMLDecoder.h */; settings = {ATTRIBUTES = (Public, ); }; }; + AAFC7BD91FE64655004823ED /* XMLDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAFC7BD81FE64655004823ED /* XMLDecoder.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + AAFC7BC91FE6458C004823ED /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = AAFC7BB51FE6458C004823ED /* Project object */; + proxyType = 1; + remoteGlobalIDString = AAFC7BBD1FE6458C004823ED; + remoteInfo = XMLDecoder; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + AAFC7BBE1FE6458C004823ED /* XMLDecoder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = XMLDecoder.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + AAFC7BC11FE6458C004823ED /* XMLDecoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XMLDecoder.h; sourceTree = ""; }; + AAFC7BC21FE6458C004823ED /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + AAFC7BC71FE6458C004823ED /* XMLDecoderTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = XMLDecoderTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + AAFC7BCC1FE6458C004823ED /* XMLDecoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLDecoderTests.swift; sourceTree = ""; }; + AAFC7BCE1FE6458C004823ED /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + AAFC7BD81FE64655004823ED /* XMLDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLDecoder.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + AAFC7BBA1FE6458C004823ED /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + AAFC7BC41FE6458C004823ED /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + AAFC7BC81FE6458C004823ED /* XMLDecoder.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + AAFC7BB41FE6458C004823ED = { + isa = PBXGroup; + children = ( + AAFC7BC01FE6458C004823ED /* Sources */, + AAFC7BCB1FE6458C004823ED /* Tests */, + AAFC7BBF1FE6458C004823ED /* Products */, + ); + sourceTree = ""; + }; + AAFC7BBF1FE6458C004823ED /* Products */ = { + isa = PBXGroup; + children = ( + AAFC7BBE1FE6458C004823ED /* XMLDecoder.framework */, + AAFC7BC71FE6458C004823ED /* XMLDecoderTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + AAFC7BC01FE6458C004823ED /* Sources */ = { + isa = PBXGroup; + children = ( + AAFC7BC11FE6458C004823ED /* XMLDecoder.h */, + AAFC7BC21FE6458C004823ED /* Info.plist */, + AAFC7BD81FE64655004823ED /* XMLDecoder.swift */, + ); + path = Sources; + sourceTree = ""; + }; + AAFC7BCB1FE6458C004823ED /* Tests */ = { + isa = PBXGroup; + children = ( + AAFC7BCC1FE6458C004823ED /* XMLDecoderTests.swift */, + AAFC7BCE1FE6458C004823ED /* Info.plist */, + ); + path = Tests; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + AAFC7BBB1FE6458C004823ED /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + AAFC7BCF1FE6458C004823ED /* XMLDecoder.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + AAFC7BBD1FE6458C004823ED /* XMLDecoder */ = { + isa = PBXNativeTarget; + buildConfigurationList = AAFC7BD21FE6458C004823ED /* Build configuration list for PBXNativeTarget "XMLDecoder" */; + buildPhases = ( + AAFC7BB91FE6458C004823ED /* Sources */, + AAFC7BBA1FE6458C004823ED /* Frameworks */, + AAFC7BBB1FE6458C004823ED /* Headers */, + AAFC7BBC1FE6458C004823ED /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = XMLDecoder; + productName = XMLDecoder; + productReference = AAFC7BBE1FE6458C004823ED /* XMLDecoder.framework */; + productType = "com.apple.product-type.framework"; + }; + AAFC7BC61FE6458C004823ED /* XMLDecoderTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = AAFC7BD51FE6458C004823ED /* Build configuration list for PBXNativeTarget "XMLDecoderTests" */; + buildPhases = ( + AAFC7BC31FE6458C004823ED /* Sources */, + AAFC7BC41FE6458C004823ED /* Frameworks */, + AAFC7BC51FE6458C004823ED /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + AAFC7BCA1FE6458C004823ED /* PBXTargetDependency */, + ); + name = XMLDecoderTests; + productName = XMLDecoderTests; + productReference = AAFC7BC71FE6458C004823ED /* XMLDecoderTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + AAFC7BB51FE6458C004823ED /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0920; + LastUpgradeCheck = 0920; + ORGANIZATIONNAME = ""; + TargetAttributes = { + AAFC7BBD1FE6458C004823ED = { + CreatedOnToolsVersion = 9.2; + LastSwiftMigration = 0920; + ProvisioningStyle = Automatic; + }; + AAFC7BC61FE6458C004823ED = { + CreatedOnToolsVersion = 9.2; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = AAFC7BB81FE6458C004823ED /* Build configuration list for PBXProject "XMLDecoder" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = AAFC7BB41FE6458C004823ED; + productRefGroup = AAFC7BBF1FE6458C004823ED /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + AAFC7BBD1FE6458C004823ED /* XMLDecoder */, + AAFC7BC61FE6458C004823ED /* XMLDecoderTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + AAFC7BBC1FE6458C004823ED /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + AAFC7BC51FE6458C004823ED /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + AAFC7BB91FE6458C004823ED /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + AAFC7BD91FE64655004823ED /* XMLDecoder.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + AAFC7BC31FE6458C004823ED /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + AAFC7BCD1FE6458C004823ED /* XMLDecoderTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + AAFC7BCA1FE6458C004823ED /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = AAFC7BBD1FE6458C004823ED /* XMLDecoder */; + targetProxy = AAFC7BC91FE6458C004823ED /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + AAFC7BD01FE6458C004823ED /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + AAFC7BD11FE6458C004823ED /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 4.0; + VALIDATE_PRODUCT = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; + AAFC7BD31FE6458C004823ED /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist"; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "jp.co.recruit-lifestyle.XMLDecoder"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + AAFC7BD41FE6458C004823ED /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = ""; + CODE_SIGN_STYLE = Automatic; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist"; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "jp.co.recruit-lifestyle.XMLDecoder"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; + AAFC7BD61FE6458C004823ED /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + INFOPLIST_FILE = Tests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "jp.co.recruit-lifestyle.XMLDecoderTests"; + PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + AAFC7BD71FE6458C004823ED /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + INFOPLIST_FILE = Tests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "jp.co.recruit-lifestyle.XMLDecoderTests"; + PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + AAFC7BB81FE6458C004823ED /* Build configuration list for PBXProject "XMLDecoder" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + AAFC7BD01FE6458C004823ED /* Debug */, + AAFC7BD11FE6458C004823ED /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + AAFC7BD21FE6458C004823ED /* Build configuration list for PBXNativeTarget "XMLDecoder" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + AAFC7BD31FE6458C004823ED /* Debug */, + AAFC7BD41FE6458C004823ED /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + AAFC7BD51FE6458C004823ED /* Build configuration list for PBXNativeTarget "XMLDecoderTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + AAFC7BD61FE6458C004823ED /* Debug */, + AAFC7BD71FE6458C004823ED /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = AAFC7BB51FE6458C004823ED /* Project object */; +} diff --git a/XMLDecoder.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/XMLDecoder.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..4add6eb --- /dev/null +++ b/XMLDecoder.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/XMLDecoder.xcodeproj/xcshareddata/xcschemes/XMLDecoder.xcscheme b/XMLDecoder.xcodeproj/xcshareddata/xcschemes/XMLDecoder.xcscheme new file mode 100644 index 0000000..99d00da --- /dev/null +++ b/XMLDecoder.xcodeproj/xcshareddata/xcschemes/XMLDecoder.xcscheme @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +