Skip to content

Commit

Permalink
Add SwiftLint and fix linter errors (#35)
Browse files Browse the repository at this point in the history
Cleanup type names, fix line length, add `.swiftlint.yml` and `swiftlint` run to `.travis.yml`.

Resolves #24

* Fix linter warnings and errors
* Apply SwiftFormat changes
* Add swiftlint to .travis.yml
* Fix swiftlint Travis homebrew errors
* Fix formatting
  • Loading branch information
MaxDesiatov committed Jan 18, 2019
1 parent 2f7b0d6 commit 401de6a
Show file tree
Hide file tree
Showing 35 changed files with 619 additions and 358 deletions.
17 changes: 17 additions & 0 deletions .swiftlint.yml
@@ -0,0 +1,17 @@
disabled_rules:
- trailing_comma
- identifier_name
- private_over_fileprivate
- void_return
- operator_whitespace
- function_parameter_count

line_length:
- 150
- 200

function_body_length:
- 50

type_name:
min_length: 1
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -7,8 +7,10 @@ before_install:
- gem install cocoapods --pre # Since Travis is not always on latest version
- brew update
- brew install swiftformat
- brew outdated swiftlint || brew upgrade swiftlint
script:
- swiftformat --lint --verbose .
- swiftlint
- pod lib lint
- xcodebuild test -enableCodeCoverage YES -scheme XMLCoder
# this runs the tests the second time, but it's the only way to make sure
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,22 +1,33 @@
//
// XMLElement.swift
// XMLCoderElement.swift
// XMLCoder
//
// Created by Vincent Esche on 12/18/18.
//

import Foundation

struct _XMLElement {
struct XMLCoderElement: Equatable {
static let attributesKey = "___ATTRIBUTES"
static let escapedCharacterSet = [("&", "&amp;"), ("<", "&lt;"), (">", "&gt;"), ("'", "&apos;"), ("\"", "&quot;")]
static let escapedCharacterSet = [
("&", "&amp;"),
("<", "&lt;"),
(">", "&gt;"),
("'", "&apos;"),
("\"", "&quot;"),
]

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

init(key: String, value: String? = nil, elements: [_XMLElement] = [], attributes: [String: String] = [:]) {
init(
key: String,
value: String? = nil,
elements: [XMLCoderElement] = [],
attributes: [String: String] = [:]
) {
self.key = key
self.value = value
self.elements = elements
Expand All @@ -25,43 +36,55 @@ struct _XMLElement {

init(key: String, box: UnkeyedBox) {
let elements = box.map { box in
_XMLElement(key: key, box: box)
XMLCoderElement(key: key, box: box)
}

self.init(key: key, elements: elements)
}

init(key: String, box: KeyedBox) {
var elements: [_XMLElement] = []
var elements: [XMLCoderElement] = []

for (key, box) in box.elements {
let fail = {
preconditionFailure("Unclassified box: \(type(of: box))")
}

switch box {
case let sharedUnkeyedBox as SharedBox<UnkeyedBox>:
let box = sharedUnkeyedBox.unbox() as! UnkeyedBox
elements.append(contentsOf: box.map { _XMLElement(key: key, box: $0) })
guard let box = sharedUnkeyedBox.unbox() as? UnkeyedBox else {
fail()
}
elements.append(contentsOf: box.map {
XMLCoderElement(key: key, box: $0)
})
case let unkeyedBox as UnkeyedBox:
// This basically injects the unkeyed children directly into self:
elements.append(contentsOf: unkeyedBox.map {
_XMLElement(key: key, box: $0)
XMLCoderElement(key: key, box: $0)
})
case let sharedKeyedBox as SharedBox<KeyedBox>:
let box = sharedKeyedBox.unbox() as! KeyedBox
elements.append(_XMLElement(key: key, box: box))
guard let box = sharedKeyedBox.unbox() as? KeyedBox else {
fail()
}
elements.append(XMLCoderElement(key: key, box: box))
case let keyedBox as KeyedBox:
elements.append(_XMLElement(key: key, box: keyedBox))
elements.append(XMLCoderElement(key: key, box: keyedBox))
case let simpleBox as SimpleBox:
elements.append(_XMLElement(key: key, box: simpleBox))
case let box:
preconditionFailure("Unclassified box: \(type(of: box))")
elements.append(XMLCoderElement(key: key, box: simpleBox))
default:
fail()
}
}

let attributes: [String: String] = Dictionary(uniqueKeysWithValues: box.attributes.compactMap { key, box in
guard let value = box.xmlString() else {
return nil
let attributes: [String: String] = Dictionary(
uniqueKeysWithValues: box.attributes.compactMap { key, box in
guard let value = box.xmlString() else {
return nil
}
return (key, value)
}
return (key, value)
})
)

self.init(key: key, elements: elements, attributes: attributes)
}
Expand Down Expand Up @@ -94,7 +117,7 @@ struct _XMLElement {
self.value = value
}

mutating func append(element: _XMLElement, forKey key: String) {
mutating func append(element: XMLCoderElement, forKey key: String) {
elements.append(element)
}

Expand Down Expand Up @@ -152,71 +175,144 @@ struct _XMLElement {
return keyedBox
}

func toXMLString(with header: XMLHeader? = nil, withCDATA cdata: Bool, formatting: XMLEncoder.OutputFormatting, ignoreEscaping _: Bool = false) -> String {
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, into: &string, at: level, cdata: cdata, formatting: formatting, prettyPrinted: prettyPrinted)
private func formatUnsortedXMLElements(
_ string: inout String,
_ level: Int,
_ cdata: Bool,
_ formatting: XMLEncoder.OutputFormatting,
_ prettyPrinted: Bool
) {
formatXMLElements(
from: elements,
into: &string,
at: level,
cdata: cdata,
formatting: formatting,
prettyPrinted: prettyPrinted
)
}

fileprivate func elementString(for element: _XMLElement, at level: Int, cdata: Bool, formatting: XMLEncoder.OutputFormatting, prettyPrinted: Bool) -> String {
fileprivate func elementString(
for element: XMLCoderElement,
at level: Int,
cdata: Bool,
formatting: XMLEncoder.OutputFormatting,
prettyPrinted: Bool
) -> String {
var string = ""
string += element._toXMLString(indented: level + 1, withCDATA: cdata, formatting: formatting)
string += element._toXMLString(
indented: level + 1, withCDATA: cdata, formatting: formatting
)
string += prettyPrinted ? "\n" : ""
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 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))\""
return " \(key)=\"\(value.escape(XMLCoderElement.escapedCharacterSet))\""
}

fileprivate func formatXMLAttributes(from keyValuePairs: [(key: String, value: String)], into string: inout String) {
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: [_XMLElement], into string: inout String, at level: Int, cdata: Bool, formatting: XMLEncoder.OutputFormatting, prettyPrinted: Bool) {
fileprivate func formatXMLElements(
from elements: [XMLCoderElement],
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)
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)
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)
formatXMLAttributes(
from: attributes.map { (key: $0, value: $1) }, into: &string
)
}

fileprivate func formatXMLAttributes(_ formatting: XMLEncoder.OutputFormatting, _ string: inout String) {
private func formatXMLAttributes(
_ formatting: XMLEncoder.OutputFormatting,
_ string: inout String
) {
if formatting.contains(.sortedKeys) {
formatSortedXMLAttributes(&string)
return
}
formatUnsortedXMLAttributes(&string)
}

fileprivate func formatXMLElements(_ formatting: XMLEncoder.OutputFormatting, _ string: inout String, _ level: Int, _ cdata: Bool, _ prettyPrinted: Bool) {
private func formatXMLElements(
_ formatting: XMLEncoder.OutputFormatting,
_ string: inout String,
_ level: Int,
_ cdata: Bool,
_ prettyPrinted: Bool
) {
if formatting.contains(.sortedKeys) {
formatSortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
formatSortedXMLElements(
&string, level, cdata, formatting, prettyPrinted
)
return
}
formatUnsortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
formatUnsortedXMLElements(
&string, level, cdata, formatting, prettyPrinted
)
}

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

Expand All @@ -225,7 +321,8 @@ struct _XMLElement {
if let value = value {
string += ">"
if !ignoreEscaping {
string += (cdata == true ? "<![CDATA[\(value)]]>" : "\(value.escape(_XMLElement.escapedCharacterSet))")
string += (cdata == true ? "<![CDATA[\(value)]]>" :
"\(value.escape(XMLCoderElement.escapedCharacterSet))")
} else {
string += "\(value)"
}
Expand All @@ -243,5 +340,3 @@ struct _XMLElement {
return string
}
}

extension _XMLElement: Equatable {}
4 changes: 2 additions & 2 deletions Sources/XMLCoder/Auxiliaries/XMLKey.swift
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Shared Key Types
struct _XMLKey: CodingKey {
struct XMLKey: CodingKey {
public let stringValue: String
public let intValue: Int?

Expand All @@ -34,5 +34,5 @@ struct _XMLKey: CodingKey {
self.init(stringValue: "\(index)", intValue: index)
}

static let `super` = _XMLKey(stringValue: "super")!
static let `super` = XMLKey(stringValue: "super")!
}
16 changes: 8 additions & 8 deletions Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Expand Up @@ -10,21 +10,21 @@ import Foundation

struct XMLElementContext {}

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

static func parse(with data: Data,
errorContextLength length: UInt) throws -> KeyedBox {
let parser = _XMLStackParser()
let parser = XMLStackParser()

let node = try parser.parse(with: data, errorContextLength: length)

return node.flatten()
}

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

Expand Down Expand Up @@ -78,15 +78,15 @@ class _XMLStackParser: NSObject {
))
}

func withCurrentElement(_ body: (inout _XMLElement) throws -> ()) rethrows {
func withCurrentElement(_ body: (inout XMLCoderElement) throws -> ()) rethrows {
guard !stack.isEmpty else {
return
}
try body(&stack[stack.count - 1])
}
}

extension _XMLStackParser: XMLParserDelegate {
extension XMLStackParser: XMLParserDelegate {
func parserDidStartDocument(_: XMLParser) {
root = nil
stack = []
Expand All @@ -97,7 +97,7 @@ extension _XMLStackParser: XMLParserDelegate {
namespaceURI _: String?,
qualifiedName _: String?,
attributes attributeDict: [String: String] = [:]) {
let element = _XMLElement(key: elementName, attributes: attributeDict)
let element = XMLCoderElement(key: elementName, attributes: attributeDict)
stack.append(element)
}

Expand Down

0 comments on commit 401de6a

Please sign in to comment.