Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0a8a07a
Bool: Support init from y,n,yes,no any case, false,true added upperca…
JoeMatt Jan 31, 2019
ae4d50a
KeyedEncoding: Add new string formatters, capitalized, lowercased, up…
JoeMatt Jan 31, 2019
e65d30b
SharedBoxProtocol: Generalize for any Box inheritance
JoeMatt Jan 31, 2019
99fde81
Remove junk string in BreakfastTest xml
JoeMatt Jan 31, 2019
38b79a2
Element coding, remove empty brackets if element string value is empt…
JoeMatt Jan 31, 2019
0300dd1
Add DynamicNodeEncoding protocol
JoeMatt Jan 31, 2019
14549a6
XMLEncoder: Add both option to value encoding, refactor encoder
JoeMatt Jan 31, 2019
f8c594a
XMLDecoder.XMLDecodingStorage refactor initial value to inline var
JoeMatt Jan 31, 2019
2ee4b03
Clear up most swiftlint warnings
JoeMatt Jan 31, 2019
f5f6f8d
Rename left over values from different branch
JoeMatt Jan 31, 2019
e86be14
test: Add coding / decoding tests to DynamicNodeEncoding
JoeMatt Jan 31, 2019
5e94557
Convrted BooksTest to DynamicNodeEncoding, tests string equality
JoeMatt Jan 31, 2019
39b5999
Swiftfomat corrections
JoeMatt Jan 31, 2019
f5a8578
Add test coverage for String+Extensions
JoeMatt Jan 31, 2019
a110b83
Fix lowercasingFirstLetter was capitalized
JoeMatt Feb 6, 2019
f60bb38
Apply SwiftFormat fix to UnkeyedDecodingContainer
MaxDesiatov Feb 6, 2019
110a66b
Add isEmpty property to KeyedStorage
MaxDesiatov Feb 6, 2019
2061b34
Merge branch 'master' into feature/dynamicNodeEncoding
MaxDesiatov Feb 6, 2019
d29ce8f
Remove redundant extension of DynamicNodeEncoding
MaxDesiatov Feb 9, 2019
32f58ee
Update DynamicNodeEncoding extensions, fix test
MaxDesiatov Feb 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clear up most swiftlint warnings
  • Loading branch information
JoeMatt committed Feb 6, 2019
commit 2ee4b03cf73a79fe9cb73d45aefc38855964263d
27 changes: 12 additions & 15 deletions Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,27 @@ struct KeyedBox {
typealias Attributes = KeyedStorage<Key, Attribute>
typealias Elements = KeyedStorage<Key, Element>

var attributes: Attributes = [:]
var elements: Elements = [:]
var attributes: Attributes = [:]

init() {
attributes = [:]
elements = [:]
func unbox() -> (elements: Elements, attributes: Attributes) {
return (
elements: elements,
attributes: attributes
)
}
}

extension KeyedBox {
init<E, A>(elements: E, attributes: A)
where E: Sequence, E.Element == (Key, Element), A: Sequence, A.Element == (Key, Attribute) {
self.elements = Elements(Dictionary(uniqueKeysWithValues: elements))
self.attributes = Attributes(Dictionary(uniqueKeysWithValues: attributes))
let elements = Elements(Dictionary(uniqueKeysWithValues: elements))
let attributes = Attributes(Dictionary(uniqueKeysWithValues: attributes))
self.init(elements: elements, attributes: attributes)
}

init(elements: [Key: Element], attributes: [Key: Attribute]) {
self.elements = Elements(elements)
self.attributes = Attributes(attributes)
}

func unbox() -> (elements: Elements, attributes: Attributes) {
return (
elements: elements,
attributes: attributes
)
self.init(elements: Elements(elements), attributes: Attributes(attributes))
}
}

Expand Down
187 changes: 93 additions & 94 deletions Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,13 @@ struct XMLCoderElement: Equatable {
value: String? = nil,
elements: [XMLCoderElement] = [],
attributes: [String: String] = [:]
) {
) {
self.key = key
self.value = value
self.elements = elements
self.attributes = attributes
}

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

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

init(key: String, box: KeyedBox) {
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>:
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 {
XMLCoderElement(key: key, box: $0)
})
case let sharedKeyedBox as SharedBox<KeyedBox>:
guard let box = sharedKeyedBox.unbox() as? KeyedBox else {
fail()
}
elements.append(XMLCoderElement(key: key, box: box))
case let keyedBox as KeyedBox:
elements.append(XMLCoderElement(key: key, box: keyedBox))
case let simpleBox as SimpleBox:
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
}
return (key, value)
}
)

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

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

init(key: String, box: Box) {
switch box {
case let sharedUnkeyedBox as SharedBox<UnkeyedBox>:
self.init(key: key, box: sharedUnkeyedBox.unbox())
case let sharedKeyedBox as SharedBox<KeyedBox>:
self.init(key: key, box: sharedKeyedBox.unbox())
case let unkeyedBox as UnkeyedBox:
self.init(key: key, box: unkeyedBox)
case let keyedBox as KeyedBox:
self.init(key: key, box: keyedBox)
case let simpleBox as SimpleBox:
self.init(key: key, box: simpleBox)
case let box:
preconditionFailure("Unclassified box: \(type(of: box))")
}
}

mutating func append(value string: String) {
var value = self.value ?? ""
value += string.trimmingCharacters(in: .whitespacesAndNewlines)
Expand All @@ -124,9 +47,8 @@ struct XMLCoderElement: Equatable {
func flatten() -> KeyedBox {
let attributes = self.attributes.mapValues { StringBox($0) }

var elements: [String: Box] = [:]

for element in self.elements {
let keyedElements: [String: Box] = self.elements.reduce([String: Box]()) { (result, element) -> [String: Box] in
var result = result
let key = element.key

let hasValue = element.value != nil
Expand All @@ -135,42 +57,43 @@ struct XMLCoderElement: Equatable {

if hasValue || hasElements || hasAttributes {
if let content = element.value {
switch elements[key] {
switch result[key] {
case var unkeyedBox as UnkeyedBox:
unkeyedBox.append(StringBox(content))
elements[key] = unkeyedBox
result[key] = unkeyedBox
case let stringBox as StringBox:
elements[key] = UnkeyedBox([stringBox, StringBox(content)])
result[key] = UnkeyedBox([stringBox, StringBox(content)])
default:
elements[key] = StringBox(content)
result[key] = StringBox(content)
}
}
if hasElements || hasAttributes {
let content = element.flatten()
switch elements[key] {
switch result[key] {
case var unkeyedBox as UnkeyedBox:
unkeyedBox.append(content)
elements[key] = unkeyedBox
result[key] = unkeyedBox
case let box?:
elements[key] = UnkeyedBox([box, content])
result[key] = UnkeyedBox([box, content])
default:
elements[key] = content
result[key] = content
}
}
} else {
switch elements[key] {
switch result[key] {
case var unkeyedBox as UnkeyedBox:
unkeyedBox.append(NullBox())
elements[key] = unkeyedBox
result[key] = unkeyedBox
case let box?:
elements[key] = UnkeyedBox([box, NullBox()])
result[key] = UnkeyedBox([box, NullBox()])
default:
elements[key] = NullBox()
result[key] = NullBox()
}
}
return result
}

let keyedBox = KeyedBox(elements: elements, attributes: attributes)
let keyedBox = KeyedBox(elements: keyedElements, attributes: attributes)

return keyedBox
}
Expand Down Expand Up @@ -349,3 +272,79 @@ struct XMLCoderElement: Equatable {
return string
}
}

// MARK: - Convenience Initializers
extension XMLCoderElement {
init(key: String, box: UnkeyedBox) {
let elements = box.map { box in
XMLCoderElement(key: key, box: box)
}

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

init(key: String, box: KeyedBox) {
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()
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 {
XMLCoderElement(key: key, box: $0)
})
case let sharedKeyedBox as SharedBox<KeyedBox>:
let box = sharedKeyedBox.unbox()
elements.append(XMLCoderElement(key: key, box: box))
case let keyedBox as KeyedBox:
elements.append(XMLCoderElement(key: key, box: keyedBox))
case let simpleBox as SimpleBox:
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
}
return (key, value)
}
)

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

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

init(key: String, box: Box) {
switch box {
case let sharedUnkeyedBox as SharedBox<UnkeyedBox>:
self.init(key: key, box: sharedUnkeyedBox.unbox())
case let sharedKeyedBox as SharedBox<KeyedBox>:
self.init(key: key, box: sharedKeyedBox.unbox())
case let unkeyedBox as UnkeyedBox:
self.init(key: key, box: unkeyedBox)
case let keyedBox as KeyedBox:
self.init(key: key, box: keyedBox)
case let simpleBox as SimpleBox:
self.init(key: key, box: simpleBox)
case let box:
preconditionFailure("Unclassified box: \(type(of: box))")
}
}
}
Loading