Remove a child/subtree
The YAML tree resolve is great, as it allows me to use anchors. The result is then parsed as a JSON tree (using cJSON), which I am more familiar with.
Basically, I have a defaults-file, which has:
transactions:
- transaction: &tr_one name: Transaction One
- transaction: &tr_two name: Transaction Two
I then "merge" this with my requested input file, but that leaves me with this "transactions" node in the YAML-tree, that I am trying to get rid off. Except I can't figure out how to remove it...
As a minor comment: I am actually parsing it back to JSON, because I can't really figure out how to do everything with rapidyaml. Other than that, rapidyaml combined with c4conf is extremely powerful.
Edit: The reason for this is that I want to save the merged YAML tree, so I can use that as an input and even if the defaults change, I can reproduce my output. It works even with that transactions node there, so it is merely cosmetic.
Kind regards, Martijn
(I'm writing this on a phone, so the answer may be incomplete)
In the Tree there are methods to delete nodes and children nodes. From memory, I'm not sure the Node API has the equivalent methods.
As for using cJSON to build your data structures. Of course, you can do that. But if you're finding it hard to use rapidyaml to achieve the same, I would be curious to find out why. Mostly the operator>> should just work, and the QuickStart shows how to override it. If this info is not enough, I encourage you to open a new issue to track that.
Caro João Paulo,
I will look into the delete nodes/children nodes methods. as you suggested.
As for using cJSON: it is what I was used too, and I do think it could be done in (rapid)YAML. I was hoping there was an API documentation somewhere. Google sends me to https://rapidyaml.docsforge.com/, but that is down and the cached version is practically impossible to browse.
Abraço
Yes, unfortunately docsforge went down recently and I need to remove those links.
But if you browse through the QuickStart, you will be on your way. It shows the several ways you can deserialise.
In particular, the functions sample_*_types.
Were you able to do the removal?
No, whenever I try to remove something from the tree, my entire tree is removed.
Here is the YAML I parsed into a tree
simulation:
logging:
csv_separator: ","
ts_allocation_log_interval: -1
transactions:
- trans: &SR4.6.1
name: "SR 4.6.1"
I want to remove the entire transactions-child, but never understood how to do it. Since it was mere "cosmetic", I went on to other things more urgent.
BTW, did you see the issue I opened in c4conf? I have included a patch that resolves it for me, but don't know if it is the correct one.
I must have been fighting myself...it is as easy as doing:
tree["transactions"].clear();
Thanks
Actually, I know what I have been fighting. Not sure if it is a bug. I let you decide.
When doing:
tree["transactions"].clear();
But there was no "transactions" child in the tree, it will clear the entire original tree! I can go around it by doing:
ryml::ConstNodeRef trs_child = tree_result["transactions"];
if (trs_child.valid()) {
tree_result["transactions"].clear();
}
It needs to be ConstNodeRef for valid to be false if it does not exist, but then for clear it cannot be ConstNodeRef. This was not too intuitive and in my expectations I would consider it a bug. If there is no tree["transactions"], then a clear should do nothing and not clear the entire tree (unless this is documented somewhere and I missed it).
Edit: It sometimes still clears the entire tree .....still lost
Sorry for the late reply. I've been abroad and with limited access. Let's go by parts, addressing first the unwanted clear. Let's discuss in another time the behavior of calling .clear() on an invalid node.
Can you clarify what you mean with "sometimes still clears the entire tree"? Can you get together a MVE (minimum verifiable example) which reproduces the problem?
Also, I reckon you are using a Release build? If you build ryml in Debug, the assertions will be enabled and trigger an error at the appropriate point. This is generally very helpful. There is also a macro that ensures the assertions are enabled in release builds, (at a slight cost in codegen and speed).
No problem. I will create a minimal example, but am a bit overloaded at the moment.