pantoniou / libfyaml

Fully feature complete YAML parser and emitter, supporting the latest YAML spec and passing the full YAML testsuite.
MIT License
239 stars 73 forks source link

Replacing node with a completely different node #84

Closed yrashk closed 9 months ago

yrashk commented 1 year ago

I have a case where I want to replace (say, a scalar) node with an entirely different node type. I am doing this while iterating a list (which makes it even more complicated).

I couldn't find anything that would address this. Do you have any opinions on this, and would you be open in PR to address this need?

My current handwavy thinking suggests that since all fy_nodes are of the same type and size, something like an "in place" create would have solved my issue. Something along the lines of splitting fy_node_alloc into fy_node_alloc and fy_node_init (where fy_node_alloc will continue calling fy_node_init) and generalizing create functions to something like fy_node_create_TYPE_allocated (that would take allocated memory and type and do its own initialization using fy_node_init). The only potentially missing piece here would be the ability to get sizeof(struct fy_node) for which a new function will be needed so that external programs can allocate memory independently.

What do you think?

pantoniou commented 11 months ago

There is no way to replace a fy_node's content 'under the covers' so to speak. By design a fy_node is uniquely identified by it's pointer in memory, so altering the node contents breaks this assumption.

On top of that each node may contain allocated resources that are dependent on the type (for instance key nodes are inserted in hashes for fast retrieval).

This is a very peculiar usage pattern, what are you trying to do?

yrashk commented 11 months ago

I am essentially replacing nodes in a list I am iterating in. Right now I am doing this by removing and adding nodes but that's rather brittle (although works)

pantoniou commented 9 months ago

Due to the design decision that a node is uniquely identified by it's pointer, having that assumption change (i.e. same node pointer but different content) make this impossible.

Workaround is to remove and create new nodes.