wwkimball / yamlpath

YAML/JSON/EYAML/Compatible get/set/merge/validate/scan/convert/diff processors using powerful, intuitive, command-line friendly syntax.
https://github.com/wwkimball/yamlpath/wiki
ISC License
120 stars 23 forks source link

Feature: Remove nodes with yamlpath library function #105

Closed am21 closed 3 years ago

am21 commented 4 years ago

The only way to delete nodes now is with yaml-set command line. Would be really nice to have the functionality in a yamlpath function somehow. Maybe from the node itself, so we could write:

for node in proc.get_nodes("/some/path"):
   node.delete()

Or maybe there is already a simple to do it and I missed it?

wwkimball commented 4 years ago

You are absolutely right. I hastily added delete and null capabilities directly to the CLI tool to meet an immediate need in that context. Limited by that frame of mind, I completely overlooked library users.

Your idea of adding node management capabilities directly to the NodeCoords class is intriguing. I will consider the options and take this ticket as an enhancement request.

Thank you!

am21 commented 4 years ago

Intriguing, right ... I didn't put too much thought in it when I opened the issue ;-) If you find a better way, by all means ..

wwkimball commented 3 years ago

I went down a deep rabbit hole while looking into this idea of extending NodeCoords for direct data manipulation. I'm going to shelve that idea for a later major version change. I will instead implement your request as the library extension I should have originally added.

Sorry for the long delay.

am21 commented 3 years ago

No worries. Sorry I sent you down that rabbit hole ... on the other hand, I always find it interesting to look critically at an API and try things. Thanks in advance for the upcoming fix.

wwkimball commented 3 years ago

PR #115 resolves this request. The syntax for deleting via the library will be a bit different from your original request due to the nature of how Python Generators work; your code must consume each deleted node for the node to actually be deleted. In trade, you get the node before it is deleted for tracking, analysis, restoration, relocation, or whatever you might do with the deleted data.

A code example is in one of the new tests for this capability: https://github.com/wwkimball/yamlpath/blob/development/tests/test_processor.py#L811-L814

The next two code blocks in that test (1) verify the deleted nodes were as expected and (2) verify the remaining document still contains all other expected nodes.

These changes will be in the next release pursuant to https://github.com/wwkimball/yamlpath/projects/1