CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
795 stars 107 forks source link

Implement `removeWhitespaceElements ` on `XMLDecoder` #222

Closed wooj2 closed 3 years ago

wooj2 commented 3 years ago

This updates the XMLStackParser to accept a parameter called removeWhitespaceElements.

The purpose of the XMLStackParser is to call the XML parser and create a tree of XMLCoderElement representing the structure of the parsed XML.

Assuming that XMLStackParser has trimValueWhitespaces set to false, when attempting to parse a nested data structure like the following:

<SomeType>
    <nestedStringList>
        <member>
            <member>foo</member>
            <member>bar</member>
        </member>
        <member>
            <member>baz</member>
            <member>qux</member>
        </member>
    </nestedStringList>
</SomeType>

... then there will multiple XMLCoderElements in the tree which will have elements set to elements that are either: a. Purely whitespaced elements or b. The child elements

These purely whitespaced elements are problematic for users who are implementing custom Decoder logic, as they are interpreted as regular child elements. Therefore, setting removeWhitespaceElements to true while trimValueWhitespaces is set to false, will remove these whitespace elements during the construction of the XMLCoderElement tree.

An in-depth analysis of the original problem can be found here.

For historical purposes, a separate approach was implemented. It uses a similar algorithm in a different part of the code. https://github.com/MaxDesiatov/XMLCoder/pull/221

codecov[bot] commented 3 years ago

Codecov Report

Merging #222 (02da507) into main (a469f60) will increase coverage by 0.37%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #222      +/-   ##
==========================================
+ Coverage   73.50%   73.87%   +0.37%     
==========================================
  Files          46       46              
  Lines        2404     2431      +27     
==========================================
+ Hits         1767     1796      +29     
+ Misses        637      635       -2     
Impacted Files Coverage Δ
...urces/XMLCoder/Auxiliaries/String+Extensions.swift 100.00% <100.00%> (ø)
Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift 96.39% <100.00%> (+0.04%) :arrow_up:
Sources/XMLCoder/Auxiliaries/XMLStackParser.swift 93.93% <100.00%> (+1.16%) :arrow_up:
Sources/XMLCoder/Decoder/XMLDecoder.swift 78.72% <100.00%> (+2.08%) :arrow_up:
...es/XMLCoder/Decoder/XMLDecoderImplementation.swift 67.74% <0.00%> (+0.19%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a469f60...02da507. Read the comment docs.

MaxDesiatov commented 3 years ago

@wooj2 one last request, I promise 🙂

Can you add a subsection to the "Advanced features" section in README.md about this new flag? You can state it's available starting with the 0.13.0 release, which I'm going to tag after this is merged.

Just an example of your use case and a short description of the feature, similar to other subsections that describe advanced features. Thank you!

wooj2 commented 3 years ago

@wooj2 one last request, I promise 🙂

Can you add a subsection to the "Advanced features" section in README.md about this new flag? You can state it's available starting with the 0.13.0 release, which I'm going to tag after this is merged.

Just an example of your use case and a short description of the feature, similar to other subsections that describe advanced features. Thank you!

No prob at all! Just updated it. Thanks again for all the support

MaxDesiatov commented 3 years ago

@kneekey23 @wooj2 perfect, thanks for uncovering this edge case! Your contribution is very appreciated 👏