Skip to content

Commit

Permalink
coding works on linux now
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed May 24, 2018
1 parent 94e881b commit 344c27d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 186 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM liveui/boost-base:1.0

WORKDIR /boost

ADD scripts ./scripts
ADD Sources ./Sources
ADD Tests ./Tests
ADD Package.swift ./

RUN swift build --configuration debug

183 changes: 22 additions & 161 deletions Sources/XMLCoding/Decoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,18 @@ extension _XMLDecoder : SingleValueDecodingContainer {
// MARK: - Concrete Value Representations

extension _XMLDecoder {

/// Returns the given value unboxed from a container.
internal func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
guard !(value is NSNull) else { return nil }

guard let value = value as? String else { return nil }
guard let value = value as? String else {
return nil
}

if value == "true" || value == "1" {
if value.lowercased() == "true" || value == "1" {
return true
} else if value == "false" || value == "0" {
} else if value.lowercased() == "false" || value == "0" {
return false
}

Expand All @@ -348,18 +351,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let int = number.intValue
guard NSNumber(value: int) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return int
return Int(exactly: value)
}

internal func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? {
Expand All @@ -371,18 +363,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let int8 = number.int8Value
guard NSNumber(value: int8) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return int8
return Int8(exactly: value)
}

internal func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? {
Expand All @@ -394,18 +375,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let int16 = number.int16Value
guard NSNumber(value: int16) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return int16
return Int16(exactly: value)
}

internal func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? {
Expand All @@ -417,18 +387,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let int32 = number.int32Value
guard NSNumber(value: int32) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return int32
return Int32(exactly: value)
}

internal func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? {
Expand All @@ -440,18 +399,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let int64 = number.int64Value
guard NSNumber(value: int64) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return int64
return Int64(exactly: value)
}

internal func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? {
Expand All @@ -463,18 +411,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let uint = number.uintValue
guard NSNumber(value: uint) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return uint
return UInt(exactly: value)
}

internal func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? {
Expand All @@ -486,18 +423,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let uint8 = number.uint8Value
guard NSNumber(value: uint8) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return uint8
return UInt8(exactly: value)
}

internal func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? {
Expand All @@ -509,18 +435,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let uint16 = number.uint16Value
guard NSNumber(value: uint16) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return uint16
return UInt16(exactly: value)
}

internal func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? {
Expand All @@ -532,18 +447,7 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let uint32 = number.uint32Value
guard NSNumber(value: uint32) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return uint32
return UInt32(exactly: value)
}

internal func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? {
Expand All @@ -555,74 +459,31 @@ extension _XMLDecoder {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let uint64 = number.uint64Value
guard NSNumber(value: uint64) == number else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type)."))
}

return uint64
return UInt64(exactly: value)
}

internal func unbox(_ value: Any, as type: Float.Type) throws -> Float? {
guard !(value is NSNull) else { return nil }

guard let string = value as? String else { return nil }

if let value = Double(string) {
let number = NSNumber(value: value)

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

let double = number.doubleValue
guard abs(double) <= Double(Float.greatestFiniteMagnitude) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number \(number) does not fit in \(type)."))
}

return Float(double)
} else if case let .convertFromString(posInfString, negInfString, nanString) = self.options.nonConformingFloatDecodingStrategy {
if string == posInfString {
return Float.infinity
} else if string == negInfString {
return -Float.infinity
} else if string == nanString {
return Float.nan
}
guard let value = Float(string) else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
return value
}

internal func unbox(_ value: Any, as type: Double.Type) throws -> Double? {
guard !(value is NSNull) else { return nil }

guard let string = value as? String else { return nil }

if let number = Decimal(string: string) as NSDecimalNumber? {

guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
}

return number.doubleValue
} else if case let .convertFromString(posInfString, negInfString, nanString) = self.options.nonConformingFloatDecodingStrategy {
if string == posInfString {
return Double.infinity
} else if string == negInfString {
return -Double.infinity
} else if string == nanString {
return Double.nan
}
guard let value = Double(string) else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string)
}

throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
return value
}

internal func unbox(_ value: Any, as type: String.Type) throws -> String? {
Expand Down
30 changes: 5 additions & 25 deletions Sources/XMLCoding/Encoder/XMLEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -852,35 +852,15 @@ extension _XMLEncoder {

internal func box(_ value: Float) throws -> NSObject {
if value.isInfinite || value.isNaN {
guard case let .convertToString(positiveInfinity: posInfString, negativeInfinity: negInfString, nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
throw EncodingError._invalidFloatingPointValue(value, at: codingPath)
}

if value == Float.infinity {
return posInfString as NSObject
} else if value == -Float.infinity {
return negInfString as NSObject
} else {
return nanString as NSObject
}
throw EncodingError._invalidFloatingPointValue(value, at: codingPath)
} else {
return NSNumber(value: value)
}
}

internal func box(_ value: Double) throws -> NSObject {
if value.isInfinite || value.isNaN {
guard case let .convertToString(positiveInfinity: posInfString, negativeInfinity: negInfString, nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
throw EncodingError._invalidFloatingPointValue(value, at: codingPath)
}

if value == Double.infinity {
return posInfString as NSObject
} else if value == -Double.infinity {
return negInfString as NSObject
} else {
return nanString as NSObject
}
throw EncodingError._invalidFloatingPointValue(value, at: codingPath)
} else {
return NSNumber(value: value)
}
Expand All @@ -897,12 +877,12 @@ extension _XMLEncoder {
return NSNumber(value: value.timeIntervalSince1970 * 1000.0)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
return _iso8601Formatter.string(from: value) as NSObject
return NSString(string: _iso8601Formatter.string(from: value))
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .formatted(let formatter):
return formatter.string(from: value) as NSObject
return NSString(string: formatter.string(from: value))
case .custom(let closure):
let depth = self.storage.count
try closure(value, self)
Expand All @@ -919,7 +899,7 @@ extension _XMLEncoder {
try value.encode(to: self)
return self.storage.popContainer()
case .base64:
return value.base64EncodedString() as NSObject
return NSString(string: value.base64EncodedString())
case .custom(let closure):
let depth = self.storage.count
try closure(value, self)
Expand Down

0 comments on commit 344c27d

Please sign in to comment.