CompSciGuy139 CompSciGuy139 - 1 year ago 99
C++ Question

yams-cpp nested sequence returning map instead of value

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


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


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
I am expecting to be 1 the first loop, 2 the second, etc. What I am getting instead is
is of type map. What am I missing in my understanding here?



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

Answer Source

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.

    - 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download