Skip to content

Commit

Permalink
Merge pull request #177 from drmohundro/fix-for-filter-plus-serializa…
Browse files Browse the repository at this point in the history
…tion

Fix issue with filter plus serialization
  • Loading branch information
drmohundro authored Feb 6, 2018
2 parents c7cd744 + 9aafb10 commit 30a13aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
27 changes: 12 additions & 15 deletions Source/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,10 @@ public enum XMLIndexer {
public func filter(_ included: (_ elem: XMLElement, _ index: Int) -> Bool) -> XMLIndexer {
switch self {
case .list(let list):
let results = filterWithIndex(seq: list, included: included)
if results.count == 1 {
return XMLIndexer.element(results.first!)
}
return XMLIndexer.list(results)
return handleFilteredResults(list: list, included: included)

case .element(let elem):
return XMLIndexer.list(filterWithIndex(seq: elem.xmlChildren, included: included))
return handleFilteredResults(list: elem.xmlChildren, included: included)

case .stream(let ops):
let found = ops.findElements()
Expand All @@ -588,20 +584,21 @@ public enum XMLIndexer {
} else {
list = found.all.map { $0.element! }
}
let results = filterWithIndex(seq: list, included: included)
if results.count == 1 {
return XMLIndexer.element(results.first!)
}
return XMLIndexer.list(results)

return handleFilteredResults(list: list, included: included)

default:
return XMLIndexer.list([])
return .list([])
}
}

private func filterWithIndex(seq: [XMLElement],
included: (_ elem: XMLElement, _ index: Int) -> Bool) -> [XMLElement] {
return zip(seq.indices, seq).filter { included($1, $0) }.map { $1 }
private func handleFilteredResults(list: [XMLElement],
included: (_ elem: XMLElement, _ index: Int) -> Bool) -> XMLIndexer {
let results = zip(list.indices, list).filter { included($1, $0) }.map { $1 }
if results.count == 1 {
return .element(results.first!)
}
return .list(results)
}

/// All child elements from the currently indexed level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ class TypeConversionArrayOfNonPrimitiveTypesTests: XCTestCase {
}
}
}

func testFilterAndSerializationShouldWork() {
do {
let subParser = parser!["root"]["arrayOfGoodBasicItems"].filter({ _, idx in idx == 0 })

let value: BasicItem = try subParser.value()

XCTAssertNotNil(value)
} catch {
XCTFail("\(error)")
}
}
}

extension TypeConversionArrayOfNonPrimitiveTypesTests {
Expand All @@ -229,7 +241,8 @@ extension TypeConversionArrayOfNonPrimitiveTypesTests {
("testShouldConvertArrayOfGoodAttributeItemsToArrayOfOptionals", testShouldConvertArrayOfGoodAttributeItemsToArrayOfOptionals),
("testShouldThrowWhenConvertingArrayOfBadAttributeItemsToNonOptional", testShouldThrowWhenConvertingArrayOfBadAttributeItemsToNonOptional),
("testShouldThrowWhenConvertingArrayOfBadAttributeItemsToOptional", testShouldThrowWhenConvertingArrayOfBadAttributeItemsToOptional),
("testShouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals", testShouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals)
("testShouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals", testShouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals),
("testFilterAndSerializationShouldWork", testFilterAndSerializationShouldWork)
]
}
}

0 comments on commit 30a13aa

Please sign in to comment.