Add DSL-like initializers for document and element #186
+131
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I recently looked at a bit of XML-handling code of mine and thought that the API could be improved to be more ergonomic using
@resultBuilder
s.This introduces two
@resultBuilder
-based builders:AEXMLDocumentBuilder
andAEXMLElementBuilder
. The main difference is that the first produces anAEXMLDocument
and only accepts a singularAEXMLElement
as its root. The latter produces an array ofAEXMLElement
s.Both can be used in conjunction to create new hierarchies of elements easily and quickly. Instead of creating child elements before adding them to their parents, they can be created in code in a way that more closely resembles the actual XML structure in the resulting document.
An example use from code that I plan to ship into production is the following. Note how the structure of the document is easily defined and resembles the XML string that would be generated from it.
I added the document builder to the function but I could as well have just wrapped the root element in a
AEXMLDocument {}
without it.I think this API is very flexible and adds to the library's readability. It doesn't take away any existing APIs so they remain the same. Would love your feedback. Just let me know what you think about this addition and if I should adjust anything.
(Btw, I have been using your library for years and I am super happy with it! Thank you for providing this to the community!)