bitpit icon indicating copy to clipboard operation
bitpit copied to clipboard

IO: add support for reading/writing XML attributes

Open andrea-iob opened this issue 3 years ago • 1 comments

The class ConfigParser is now able to read/write XML attributes. XML attributes are designed to contain data related to a specific element, for example here:

<x min="0" max="100">11.11</x>

min and max are two attributes of the element x.

I tried to keep the interface for interacting with the attributes as close as possible to the existing interface to interact with options. Attribute can be get/set using the following functions (and their corresponding templates):

    std::string getAttribute(const std::string &key, const std::string &name) const;
    std::string getAttribute(const std::string &key, const std::string &name, const std::string &fallback) const;
    const Attributes & getAttributes(const std::string &key) const;
    void setAttribute(const std::string &key, const std::string &name, const std::string &value);

Options stored in the configuration are now a struct and not a string anymore. This break compatibility with previous versions, however this is a problem only if the function "getOptions" was used. The struct stores both the option value an its attributes. There are new function to add/get Option objects:

    Option & getOption(const std::string &key);
    const Option & getOption(const std::string &key) const;
    void addOption(const std::string &key, const Option &option);
    void addOption(const std::string &key, Option &&option);
    void addOption(const std::string &key, const std::string &value);
    void addOption(const std::string &key, std::string &&value);
    void addOption(const std::string &key, const std::string &value, const Attributes &attributes);
    void addOption(const std::string &key, std::string &&value, Attributes &&attributes);

andrea-iob avatar Feb 09 '22 21:02 andrea-iob

Hi, I had a very quick look, and honestly I need some time to study better the solution you proposed and test it from my side. Meanwhile, two remarks:

  1. why Attributes are considered attached only on value cases (leaf element of the xml tree) and not on Sections (see below an example of what i meant )
  2. json does not support attributes as xml: it considers attributes as regular leaf element of the tree. If we want to have 1:1 reversibility between the 2 formas, we need a special distinctive mark(maybe a @ before the name of each element in the json file ) to absorb some element of the json tree as attributes of the Config tree and viceversa to flush.

Anyway, give me some time please, I can came up with a more consistent and clear review to make my point. Meanwhile a xml example of what I meant in 1) :

<messages>
  <note id="501">
    <to internal="yes">Tove</to>
    <from internal="yes">Jani</from>
    <heading>Reminder</heading>
    <body font="Tahoma">Don't forget me this weekend!</body>
  </note>
  <note id="502">
    <to internal="yes">Jani</to>
    <from internal="yes">Tove</from>
    <heading>Re: Reminder</heading>
    <body font="Calibri">I will not</body>
  </note>
</messages>

roccoarpa avatar Feb 10 '22 17:02 roccoarpa