Skip to content

Commit

Permalink
Unfold guard … else blocks to allow settingbreakpoints (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
regexident authored and MaxDesiatov committed Dec 23, 2018
1 parent fb07143 commit 4cad2df
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 25 deletions.
4 changes: 3 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public struct XMLHeader {
}

func toXML() -> String? {
guard !isEmpty() else { return nil }
guard !isEmpty() else {
return nil
}

var string = "<?xml "

Expand Down
84 changes: 63 additions & 21 deletions Sources/XMLCoder/Decoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,19 @@ open class XMLDecoder {
case custom((_ codingPath: [CodingKey]) -> CodingKey)

static func _convertFromCapitalized(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
guard !stringKey.isEmpty else {
return stringKey
}
var result = stringKey
let range = result.startIndex...result.index(after: result.startIndex)
result.replaceSubrange(range, with: result[range].lowercased())
return result
}

static func _convertFromSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
guard !stringKey.isEmpty else {
return stringKey
}

// Find the first non-underscore character
guard let firstNonUnderscore = stringKey.index(where: { $0 != "_" }) else {
Expand Down Expand Up @@ -399,9 +403,13 @@ extension _XMLDecoder {
/// Returns the given box unboxed from a container.

func unbox(_ box: Box) throws -> Bool? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let boolBox = BoolBox(xmlString: string) else {
throw DecodingError._typeMismatch(at: codingPath, expectation: Bool.self, reality: box)
Expand All @@ -411,9 +419,13 @@ extension _XMLDecoder {
}

func unbox(_ box: Box) throws -> Decimal? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let decimalBox = DecimalBox(xmlString: string) else {
throw DecodingError._typeMismatch(at: codingPath, expectation: Decimal.self, reality: box)
Expand All @@ -423,9 +435,13 @@ extension _XMLDecoder {
}

func unbox<T: BinaryInteger & SignedInteger & Decodable>(_ box: Box) throws -> T? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let intBox = IntBox(xmlString: string) else {
throw DecodingError._typeMismatch(at: codingPath, expectation: T.self, reality: box)
Expand All @@ -442,9 +458,13 @@ extension _XMLDecoder {
}

func unbox<T: BinaryInteger & UnsignedInteger & Decodable>(_ box: Box) throws -> T? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let uintBox = UIntBox(xmlString: string) else {
throw DecodingError._typeMismatch(at: codingPath, expectation: T.self, reality: box)
Expand All @@ -461,9 +481,13 @@ extension _XMLDecoder {
}

func unbox<T: BinaryFloatingPoint & Decodable>(_ box: Box) throws -> T? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let floatBox = FloatBox(xmlString: string) else {
throw DecodingError._typeMismatch(at: codingPath, expectation: T.self, reality: box)
Expand All @@ -480,7 +504,9 @@ extension _XMLDecoder {
}

func unbox(_ box: Box) throws -> String? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else {
throw DecodingError._typeMismatch(at: codingPath, expectation: String.self, reality: box)
Expand All @@ -490,7 +516,9 @@ extension _XMLDecoder {
}

func unbox(_ box: Box) throws -> Date? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

switch options.dateDecodingStrategy {
case .deferredToDate:
Expand Down Expand Up @@ -550,7 +578,9 @@ extension _XMLDecoder {
}

func unbox(_ box: Box) throws -> Data? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

switch options.dataDecodingStrategy {
case .deferredToData:
Expand All @@ -577,9 +607,13 @@ extension _XMLDecoder {
}

func unbox(_ box: Box) throws -> URL? {
guard !box.isNull else { return nil }
guard !box.isNull else {
return nil
}

guard let string = (box as? StringBox)?.unbox() else { return nil }
guard let string = (box as? StringBox)?.unbox() else {
return nil
}

guard let urlBox = URLBox(xmlString: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(
Expand All @@ -595,16 +629,24 @@ extension _XMLDecoder {
let decoded: T
let type = T.self
if type == Date.self || type == NSDate.self {
guard let date: Date = try unbox(box) else { return nil }
guard let date: Date = try unbox(box) else {
return nil
}
decoded = date as! T
} else if type == Data.self || type == NSData.self {
guard let data: Data = try unbox(box) else { return nil }
guard let data: Data = try unbox(box) else {
return nil
}
decoded = data as! T
} else if type == URL.self || type == NSURL.self {
guard let data: URL = try unbox(box) else { return nil }
guard let data: URL = try unbox(box) else {
return nil
}
decoded = data as! T
} else if type == Decimal.self || type == NSDecimalNumber.self {
guard let decimal: Decimal = try unbox(box) else { return nil }
guard let decimal: Decimal = try unbox(box) else {
return nil
}
decoded = decimal as! T
} else {
storage.push(container: box)
Expand Down
12 changes: 9 additions & 3 deletions Sources/XMLCoder/Encoder/XMLEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ open class XMLEncoder {
case custom((_ codingPath: [CodingKey]) -> CodingKey)

static func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
guard !stringKey.isEmpty else {
return stringKey
}

var words: [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
Expand Down Expand Up @@ -524,7 +526,9 @@ extension _XMLEncoder {
let depth = storage.count
try closure(value, self)

guard storage.count > depth else { return KeyedBox() }
guard storage.count > depth else {
return KeyedBox()
}

return storage.popContainer()
}
Expand All @@ -541,7 +545,9 @@ extension _XMLEncoder {
let depth = storage.count
try closure(value, self)

guard storage.count > depth else { return KeyedBox() }
guard storage.count > depth else {
return KeyedBox()
}

return storage.popContainer()
}
Expand Down

0 comments on commit 4cad2df

Please sign in to comment.