CompSciGuy139 CompSciGuy139 - 1 month ago 6
C++ Question

yams-cpp nested sequence returning map instead of value

I am trying to parse the following config.yaml file.

config.yaml


foo:
bar:
baz: [1, 2, 3, 4]
bam: "some_string_value"


test.cpp


YAML::Node configObj = YAML::LoadFile("cfig.yaml"); // loads file just fine

YAML::Node fooObj = configObj["foo"]; // this Node object is a Map

// iterate over foo node to get bar node
for( auto it = fooObj.begin(); it != fooObj.end(); ++it){
YAML::Node barMap = it->second; // this Node object is a Map

// iterate over bar node to get bad node
for( auto i = barMap.begin(); i != barMap.end(); ++i){
YAML::Node bazMap = i->second; // this node is a sequence

for( std::size_t i=0; i<bazMap.size(); i++
auto index = bazMap[i].as<int>(); // <<< This is the problem
}
}
}


The problem as far as I can see is that I am expecting index to be an int but
bazMap[i].as<int>()
I am expecting to be 1 the first loop, 2 the second, etc. What I am getting instead is
bazMap[i].as<int>()
is of type map. What am I missing in my understanding here?

Thanks,

Bruce

Update The answer was that I stopped early in my nested for loops.

Answer

The answer that I found rests in that I was unclear on what I was getting.

In the config.yaml file I was expecting the following:

foo.Type() == Map
bar.Type() == Sequence
baz.Type() == Sequence

but what I was getting was:

foo.Type() == Map
bar.Type() == Map
baz.Type() == Sequence

all I had to do to resolve this issue was to change the config file.

foo:
  bar:
    - baz: [1, 2, 3, 4]
    - bam: "some_string_value"

Then it parsed exactly as I had expected.

So the issue was the format of the config file and not in the parsing logic.

Comments