Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need explanation / improve tutorial #928

Open
gelldur opened this issue Jul 25, 2020 · 4 comments
Open

Need explanation / improve tutorial #928

gelldur opened this issue Jul 25, 2020 · 4 comments

Comments

@gelldur
Copy link

gelldur commented Jul 25, 2020

I just started using yaml-cpp and I did navigation from key like: "my.magic.str.value" to access nested values in YAML files.

I did this with code:

	YAML::Node node = _root;
	for(const auto& element : path)
	{
		node = node[element];
		if(!node)
		{
			return node;
		}
	}
	return node;

So thanks to that I could navigate through tree.
It works fine for first execution then it stops as we modify _root ...

I found this question also: https://stackoverflow.com/questions/43597237/yaml-cpp-modifies-underlying-container-even-for-const-nodes

It would be nice to explain this in tutorial or forbid it somehow. Solution is to use recursion.
E.g.

static YAML::Node goDeep(YAML::Node parent, unsigned level, const std::vector<std::string>& levels)
{
	if(level == levels.size())
	{
		return parent;
	}
	if(!parent)
	{
		return parent;
	}

	return goDeep(parent[levels.at(level)], level + 1, levels);
}

YAML::Node ConfigValue::find(const std::string& key) const
{
	const auto levels = gcpp::string::split(key, "."s);
	return goDeep(*_storage, 0, levels);
}
@SignalWhisperer
Copy link

I face the same issue in implementing the same behavior. It renders this library useless, especially since there are no compiler warnings despite marking my class member function const. A contract is broken.

@robambalu
Copy link

FYI Ive hit the same issue, I had to wrap YAML::Node in my own object to control for this behavior

@stilgarpl
Copy link

I have the same issue. The assignment semantics for Node makes no sense. Even if it acts as a pointer, accessing a copy should not modify original object. I guess I'll have to use libfyaml...

@hinell
Copy link

hinell commented Jun 12, 2023

Please, either open Wiki for editing or update tutorial. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants