mercredi 19 octobre 2022

Attaching a Comment to a YAML::Node for Presentation in Output

I'm using yaml-cpp with C++11. I can create a YAML file using something simple like this:

#include <yaml-cpp/yaml.h>
#include <iostream>

int main(void)
{
  YAML::Node  topNode;

  topNode["one"]["two"]["A"] = "foo";
  topNode["one"]["two"]["B"] = 42;

  std::cout << "%YAML 1.2\n---\n" << topNode;
  return 0;
}

That will produce a YAML file like this:

%YAML 1.2
---
one:
  two:
    A: foo
    B: 42

Lovely!

I can also produce exactly the same YAML file like this:

#include <yaml-cpp/yaml.h>
#include <iostream>

int main(void)
{
  YAML::Emitter  out;

  out << YAML::BeginMap // document {
      << "one"
        << YAML::BeginMap // one {
        << "two"
          << YAML::BeginMap // two {
          << YAML::Key << "A" << YAML::Value << "foo"
          << YAML::Key << "B" << YAML::Value << 42
          << YAML::EndMap // two }
        << YAML::EndMap // one }
      << YAML::EndMap // document }
      ;

  std::cout << "%YAML 1.2\n---\n"
            << out.c_str();
  return 0;
}

The nice thing about the second approach is that I can also add comments into the output file:

#include <yaml-cpp/yaml.h>
#include <iostream>

int main(void)
{
  YAML::Emitter  out;

  out << YAML::BeginMap // document {
      << "one"
        << YAML::BeginMap // one {
        << "two"
          << YAML::BeginMap // two {
          << YAML::Key << "A" << YAML::Value << "foo"
             << YAML::Comment("A should be 'foo'")
          << YAML::Key << "B" << YAML::Value << 42
             << YAML::Comment("B is meaningful")
          << YAML::EndMap // two }
        << YAML::EndMap // one }
      << YAML::EndMap // document }
      ;

  std::cout << "%YAML 1.2\n---\n"
            << out.c_str();
  return 0;
}

to produce:

%YAML 1.2
---
one:
  two:
    A: foo  # A should be 'foo'
    B: 42  # B is meaningful

My question if there is a way to add comments into the first approach? Perhaps something like this:

  topNode["one"]["two"]["A"] = "foo";
  topNode["one"]["two"]["A"].addComment("A should be 'foo'");

I could subclass YAML::Node, adding my addComment() method, but I don't want to re-write all of YAML::Emitter to get my comment appended appropriately. The code is there, but I don't know how to get to it. How? Can you point me to an example or an approach?

I understand that the YAML specification says that comments are not an integral part of a YAML file, and can be discarded. My users find them useful, so I don't relish a debate that begins with "Your question is stupid." :-)

Aucun commentaire:

Enregistrer un commentaire