Given a YAML::Node how can we visit all Scalar Nodes within that node (to modify them)? My best guess is a recursive function:
void parseNode(YAML::Node node) {
if (node.IsMap()) {
for (YAML::iterator it = node.begin(); it != node.end(); ++it) {
parseNode(it->second);
}
}
else if (node.IsSequence()) {
for (YAML::iterator it = node.begin(); it != node.end(); ++it) {
parseNode(*it);
}
}
else if (node.IsScalar()) {
// Perform modifications here.
}
}
This works fine for my needs except for in one case: if there is a recursive anchor/alias. (I think this is permitted by the YAML 1.2 spec? yaml-cpp certainly doesn't throw an Exception when parsing one) e.g.:
first: &firstanchor
a: 5
b: *firstanchor
The code above will follow the alias to the anchor repeatedly until crashing (SEGFAULT) presumably due to stack issues. How would one overcome this issue?
- Is there a better way to iterate through an entire unknown
Nodestructure that would solve this? - Is there a way to check from the
Nodewith the public API if it is an alias so we can maintain a stack of previous visited aliases to detect looping and break. - Or is there a way to fetch some unique
Nodeidentifier so I can maintain a stack of previously visited nodes? - Or finally is this recursive anchor/alias just not spec-compliant - in which case how can I check for its occurrence to ensure I can return an appropriate error?