rapidyaml  0.12.0
parse and emit YAML, and do it fast
c4::yml::ReferenceResolver Struct Reference

Reusable object to resolve references/aliases in a Tree. More...

#include <reference_resolver.hpp>

Public Member Functions

 ReferenceResolver ()=default
 
void resolve (Tree *tree, bool clear_anchors=true)
 Resolve references: for each reference, look for a matching anchor, and copy its contents to the ref node. More...
 

Detailed Description

Reusable object to resolve references/aliases in a Tree.

Definition at line 16 of file reference_resolver.hpp.

Constructor & Destructor Documentation

◆ ReferenceResolver()

c4::yml::ReferenceResolver::ReferenceResolver ( )
default

Member Function Documentation

◆ resolve()

void c4::yml::ReferenceResolver::resolve ( Tree tree,
bool  clear_anchors = true 
)

Resolve references: for each reference, look for a matching anchor, and copy its contents to the ref node.

tree the subject tree

clear_anchors whether to clear existing anchors after resolving

This method first does a full traversal of the tree to gather all anchors and references in a separate collection, then it goes through that collection to locate the names, which it does by obeying the YAML standard diktat that "an alias node refers to the most recent node in the serialization having the specified anchor"

So, depending on the number of anchor/alias nodes, this is a potentially expensive operation, with a best-case linear complexity (from the initial traversal). This potential cost is one of the reasons for requiring an explicit call.

The Tree has an Tree::resolve() overload set forwarding here. Previously this operation was done there, using a discarded object; using this separate class offers opportunity for reuse of the object.

Warning
resolving references opens an attack vector when the data is malicious or severely malformed, as the tree can expand exponentially. See for example the Billion Laughs Attack.

The documentation for this struct was generated from the following file: