Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply SwiftFormat on CI runs #32

Merged
merged 2 commits into from Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -6,8 +6,10 @@ env:
before_install:
- gem install cocoapods --pre # Since Travis is not always on latest version
- brew update
- brew install swiftformat
- brew outdated carthage || brew upgrade carthage
script:
- swiftformat --lint --verbose .
- pod lib lint
- xcodebuild test -enableCodeCoverage YES -scheme XMLCoder
before_deploy:
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMLCoder/Auxiliaries/String+Extensions.swift
Expand Up @@ -10,11 +10,11 @@ import Foundation
extension String {
func escape(_ characterSet: [(character: String, escapedCharacter: String)]) -> String {
var string = self

for set in characterSet {
string = string.replacingOccurrences(of: set.character, with: set.escapedCharacter, options: .literal)
}

return string
}
}
72 changes: 36 additions & 36 deletions Sources/XMLCoder/Auxiliaries/XMLElement.swift
Expand Up @@ -10,31 +10,31 @@ import Foundation
struct _XMLElement {
static let attributesKey = "___ATTRIBUTES"
static let escapedCharacterSet = [("&", "&amp"), ("<", "&lt;"), (">", "&gt;"), ("'", "&apos;"), ("\"", "&quot;")]

var key: String
var value: String?
var attributes: [String: String] = [:]
var elements: [String: [_XMLElement]] = [:]

init(key: String, value: String? = nil, attributes: [String: String] = [:], elements: [String: [_XMLElement]] = [:]) {
self.key = key
self.value = value
self.attributes = attributes
self.elements = elements
}

init(key: String, box: UnkeyedBox) {
self.init(key: key)
self.elements[key] = box.map { box in

elements[key] = box.map { box in
_XMLElement(key: key, box: box)
}
}

init(key: String, box: KeyedBox) {
self.init(key: key)
self.attributes = Dictionary(uniqueKeysWithValues: box.attributes.compactMap { key, box in

attributes = Dictionary(uniqueKeysWithValues: box.attributes.compactMap { key, box in
guard let value = box.xmlString() else {
return nil
}
Expand All @@ -57,17 +57,17 @@ struct _XMLElement {
preconditionFailure("Unclassified box: \(type(of: box))")
}
}
self.elements = Dictionary(elementsByKey) { existingElements, newElements in

elements = Dictionary(elementsByKey) { existingElements, newElements in
existingElements + newElements
}
}

init(key: String, box: SimpleBox) {
self.init(key: key)
self.value = box.xmlString()
value = box.xmlString()
}

init(key: String, box: Box) {
switch box {
case let unkeyedBox as UnkeyedBox:
Expand All @@ -80,20 +80,20 @@ struct _XMLElement {
preconditionFailure("Unclassified box: \(type(of: box))")
}
}

mutating func append(value string: String) {
var value = self.value ?? ""
value += string.trimmingCharacters(in: .whitespacesAndNewlines)
self.value = value
}

mutating func append(element: _XMLElement, forKey key: String) {
self.elements[key, default: []].append(element)
elements[key, default: []].append(element)
}

func flatten() -> KeyedBox {
let attributes = self.attributes.mapValues { StringBox($0) }

var elements: [String: Box] = [:]
for (key, value) in self.elements {
for child in value {
Expand Down Expand Up @@ -127,18 +127,18 @@ struct _XMLElement {

return KeyedBox(elements: elements, attributes: attributes)
}

func toXMLString(with header: XMLHeader? = nil, withCDATA cdata: Bool, formatting: XMLEncoder.OutputFormatting, ignoreEscaping _: Bool = false) -> String {
if let header = header, let headerXML = header.toXML() {
return headerXML + _toXMLString(withCDATA: cdata, formatting: formatting)
}
return _toXMLString(withCDATA: cdata, formatting: formatting)
}

fileprivate func formatUnsortedXMLElements(_ string: inout String, _ level: Int, _ cdata: Bool, _ formatting: XMLEncoder.OutputFormatting, _ prettyPrinted: Bool) {
formatXMLElements(from: elements.map { (key: $0, value: $1) }, into: &string, at: level, cdata: cdata, formatting: formatting, prettyPrinted: prettyPrinted)
}

fileprivate func elementString(for element: (key: String, value: [_XMLElement]), at level: Int, cdata: Bool, formatting: XMLEncoder.OutputFormatting, prettyPrinted: Bool) -> String {
var string = ""
for child in element.value {
Expand All @@ -147,35 +147,35 @@ struct _XMLElement {
}
return string
}

fileprivate func formatSortedXMLElements(_ string: inout String, _ level: Int, _ cdata: Bool, _ formatting: XMLEncoder.OutputFormatting, _ prettyPrinted: Bool) {
formatXMLElements(from: elements.sorted { $0.key < $1.key }, into: &string, at: level, cdata: cdata, formatting: formatting, prettyPrinted: prettyPrinted)
}

fileprivate func attributeString(key: String, value: String) -> String {
return " \(key)=\"\(value.escape(_XMLElement.escapedCharacterSet))\""
}

fileprivate func formatXMLAttributes(from keyValuePairs: [(key: String, value: String)], into string: inout String) {
for (key, value) in keyValuePairs {
string += attributeString(key: key, value: value)
}
}

fileprivate func formatXMLElements(from elements: [(key: String, value: [_XMLElement])], into string: inout String, at level: Int, cdata: Bool, formatting: XMLEncoder.OutputFormatting, prettyPrinted: Bool) {
for element in elements {
string += elementString(for: element, at: level, cdata: cdata, formatting: formatting, prettyPrinted: prettyPrinted)
}
}

fileprivate func formatSortedXMLAttributes(_ string: inout String) {
formatXMLAttributes(from: attributes.sorted(by: { $0.key < $1.key }), into: &string)
}

fileprivate func formatUnsortedXMLAttributes(_ string: inout String) {
formatXMLAttributes(from: attributes.map { (key: $0, value: $1) }, into: &string)
}

fileprivate func formatXMLAttributes(_ formatting: XMLEncoder.OutputFormatting, _ string: inout String) {
if #available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
if formatting.contains(.sortedKeys) {
Expand All @@ -187,7 +187,7 @@ struct _XMLElement {
}
formatUnsortedXMLAttributes(&string)
}

fileprivate func formatXMLElements(_ formatting: XMLEncoder.OutputFormatting, _ string: inout String, _ level: Int, _ cdata: Bool, _ prettyPrinted: Bool) {
if #available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
if formatting.contains(.sortedKeys) {
Expand All @@ -199,15 +199,15 @@ struct _XMLElement {
}
formatUnsortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
}

fileprivate func _toXMLString(indented level: Int = 0, withCDATA cdata: Bool, formatting: XMLEncoder.OutputFormatting, ignoreEscaping: Bool = false) -> String {
let prettyPrinted = formatting.contains(.prettyPrinted)
let indentation = String(repeating: " ", count: (prettyPrinted ? level : 0) * 4)
var string = indentation
string += "<\(key)"

formatXMLAttributes(formatting, &string)

if let value = value {
string += ">"
if !ignoreEscaping {
Expand All @@ -219,13 +219,13 @@ struct _XMLElement {
} else if !elements.isEmpty {
string += prettyPrinted ? ">\n" : ">"
formatXMLElements(formatting, &string, level, cdata, prettyPrinted)

string += indentation
string += "</\(key)>"
} else {
string += " />"
}

return string
}
}
Expand All @@ -237,8 +237,8 @@ extension _XMLElement: Equatable {
lhs.value == rhs.value,
lhs.attributes == rhs.attributes,
lhs.elements == rhs.elements
else {
return false
else {
return false
}
return true
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/XMLCoder/Auxiliaries/XMLHeader.swift
Expand Up @@ -14,34 +14,34 @@ public struct XMLHeader {
public let encoding: String?
/// indicates whether a document relies on information from an external source.
public let standalone: String?

public init(version: Double? = nil, encoding: String? = nil, standalone: String? = nil) {
self.version = version
self.encoding = encoding
self.standalone = standalone
}

func isEmpty() -> Bool {
return version == nil && encoding == nil && standalone == nil
}

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

var string = "<?xml "

if let version = version {
string += "version=\"\(version)\" "
}

if let encoding = encoding {
string += "encoding=\"\(encoding)\" "
}

if let standalone = standalone {
string += "standalone=\"\(standalone)\""
}

return string.trimmingCharacters(in: .whitespaces) + "?>\n"
}
}
20 changes: 9 additions & 11 deletions Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Expand Up @@ -8,12 +8,10 @@

import Foundation

struct XMLElementContext {

}
struct XMLElementContext {}

class _XMLStackParser: NSObject {
var root: _XMLElement? = nil
var root: _XMLElement?
private var stack: [_XMLElement] = []

static func parse(with data: Data) throws -> KeyedBox {
Expand All @@ -25,25 +23,25 @@ class _XMLStackParser: NSObject {
debugDescription: "The given data could not be parsed into XML."
))
}

return node.flatten()
}

func parse(with data: Data) throws -> _XMLElement? {
let xmlParser = XMLParser(data: data)
xmlParser.delegate = self

guard xmlParser.parse() else {
if let error = xmlParser.parserError {
throw error
}
return nil
}

return root
}
func withCurrentElement(_ body: (inout _XMLElement) throws -> ()) rethrows {

func withCurrentElement(_ body: (inout _XMLElement) throws -> Void) rethrows {
guard !stack.isEmpty else {
return
}
Expand All @@ -66,11 +64,11 @@ extension _XMLStackParser: XMLParserDelegate {
guard var element = stack.popLast() else {
return
}

if let value = element.value {
element.value = value.isEmpty ? nil : value
}

withCurrentElement { currentElement in
currentElement.append(element: element, forKey: element.key)
}
Expand Down
20 changes: 9 additions & 11 deletions Sources/XMLCoder/Box/BoolBox.swift
Expand Up @@ -9,31 +9,31 @@ import Foundation

struct BoolBox: Equatable {
typealias Unboxed = Bool

let unboxed: Unboxed

init(_ unboxed: Unboxed) {
self.unboxed = unboxed
}

init?(xmlString: String) {
switch xmlString {
case "false", "0": self.init(false)
case "true", "1": self.init(true)
case _: return nil
}
}

func unbox() -> Unboxed {
return self.unboxed
return unboxed
}
}

extension BoolBox: Box {
var isNull: Bool {
return false
}

/// # Lexical representation
/// Boolean has a lexical representation consisting of the following
/// legal literals {`true`, `false`, `1`, `0`}.
Expand All @@ -45,16 +45,14 @@ extension BoolBox: Box {
///
/// [Schema definition](https://www.w3.org/TR/xmlschema-2/#boolean)
func xmlString() -> String? {
return (self.unboxed) ? "true" : "false"
return (unboxed) ? "true" : "false"
}
}

extension BoolBox: SimpleBox {

}
extension BoolBox: SimpleBox {}

extension BoolBox: CustomStringConvertible {
var description: String {
return self.unboxed.description
return unboxed.description
}
}