kode icon indicating copy to clipboard operation
kode copied to clipboard

[kxml_compiler]Simplify the generated code for single sequence child elements

Open martonmiklos opened this issue 6 years ago • 4 comments

Hi guys,

I am have an XSD generated code where there are a lot elements which have only have one sequence type child (and no attributes). Example: Library -> Symbols -> Symbol::List

In this example (I think) it would be very convenient if the Symbol::List could be accessed directly from the Library itself. (Currently I access them with

lib->symbols().symbolList() ).

I have implemented it in my simplify_single_child_lists: https://github.com/cornelius/kode/compare/master...martonmiklos:simplify_single_child_lists?expand=1

If you have a bit time and find this feature useful please take a look on it. Any review comment is welcome!

martonmiklos avatar Jan 16 '19 22:01 martonmiklos

I have to ask... how about simplifying the XSD instead? ;-)

Also, this means that if the Symbols element is modified later to gain attributes or other children, the existing code won't compile anymore... which breaks the X in XML (extensible).

dfaure-kdab avatar Jan 17 '19 10:01 dfaure-kdab

Well I am generating code from XSD for an existing XML file format.

If attributes/childs added to the parsed XML the parser (generated with this simplified method) will still parse the XML, just leaving out the new elements/attributes. BTW. the current code has the same scenario.

If new attributes/childs added to the schema the generated code will be the same as it is generated by the current code.

martonmiklos avatar Jan 17 '19 11:01 martonmiklos

What I mean is:

  • Today Symbols has no attributes, with your patch, you write C++ code that says lib->symbolList()
  • Next month someone adds an attribute, the simplification can no longer be done, this C++ code doesn't compile anymore.

Without the patch, the code is lib->symbols().symbolList() in both cases, so it keeps compiling.

dfaure-kdab avatar Jan 17 '19 11:01 dfaure-kdab

Ah I understand your concern now!

The patch adds this feature together with a command line option (which is disabled by default).

In my use case I am parsing XML files generated with an existing software where the format of the mentioned sections will unlikely to be changed. It might be the case that someone else has the similar needs.

What do you think shall I merge it upstream or not?

martonmiklos avatar Jan 17 '19 13:01 martonmiklos