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

Dynamic node encoding + new formatters + various fixes #70

Merged
merged 20 commits into from Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/XMLCoder/Auxiliaries/Box/BoolBox.swift
Expand Up @@ -17,9 +17,9 @@ struct BoolBox: Equatable {
}

init?(xmlString: String) {
switch xmlString {
case "false", "0": self.init(false)
case "true", "1": self.init(true)
switch xmlString.lowercased() {
case "false", "0", "n", "no": self.init(false)
case "true", "1", "y", "yes": self.init(true)
case _: return nil
}
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/XMLCoder/Auxiliaries/Box/Box.swift
Expand Up @@ -17,6 +17,17 @@ protocol SimpleBox: Box {
// A simple tagging protocol, for now.
}

protocol SharedBoxProtocol {
func unbox() -> Box
protocol TypeErasedSharedBoxProtocol {
func typeErasedUnbox() -> Box
}

protocol SharedBoxProtocol: TypeErasedSharedBoxProtocol {
associatedtype B: Box
func unbox() -> B
}

extension SharedBoxProtocol {
func typeErasedUnbox() -> Box {
return unbox()
}
}
31 changes: 13 additions & 18 deletions Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
Expand Up @@ -47,9 +47,7 @@ struct KeyedStorage<Key: Hashable & Comparable, Value> {
}

extension KeyedStorage: Sequence {
typealias Iterator = Buffer.Iterator

func makeIterator() -> Iterator {
func makeIterator() -> Buffer.Iterator {
return buffer.makeIterator()
}
}
Expand All @@ -74,30 +72,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
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Auxiliaries/Box/SharedBox.swift
Expand Up @@ -30,7 +30,7 @@ extension SharedBox: Box {
}

extension SharedBox: SharedBoxProtocol {
func unbox() -> Box {
func unbox() -> Unboxed {
return unboxed
}
}
29 changes: 27 additions & 2 deletions Sources/XMLCoder/Auxiliaries/String+Extensions.swift
Expand Up @@ -7,9 +7,9 @@

import Foundation

extension String {
extension StringProtocol where Self.Index == String.Index {
func escape(_ characterSet: [(character: String, escapedCharacter: String)]) -> String {
var string = self
var string = String(self)

for set in characterSet {
string = string.replacingOccurrences(of: set.character, with: set.escapedCharacter, options: .literal)
Expand All @@ -18,3 +18,28 @@ extension String {
return string
}
}

extension StringProtocol {
func capitalizingFirstLetter() -> Self {
guard count > 1 else {
return self
}
return Self(prefix(1).uppercased() + dropFirst())!
}

mutating func capitalizeFirstLetter() {
self = capitalizingFirstLetter()
}

func lowercasingFirstLetter() -> Self {
// avoid lowercasing single letters (I), or capitalized multiples (AThing ! to aThing, leave as AThing)
guard count > 1, !(String(prefix(2)) == prefix(2).lowercased()) else {
return self
}
return Self(prefix(1).lowercased() + dropFirst())!
}

mutating func lowercaseFirstLetter() {
self = lowercasingFirstLetter()
}
}