Home > Blockchain >  Attaching a Comment to a YAML::Node for Presentation in Output
Attaching a Comment to a YAML::Node for Presentation in Output

Time:10-21

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." :-)

CodePudding user response:

That is not possible with the current API. The Emitter uses an EventHandler which, as you can see, is not able to emit comments.

The Emit function that creates the events does not create any comment events via other means either.

Since operator<< on Node will internally use the Emitter class, there's no way to emit comments by adding them to a node, unless you rewrite the emitter yourself.

  • Related