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

set_value not dumping to file #218

Closed gustavolessa23 closed 1 year ago

gustavolessa23 commented 1 year ago

Hi, I am coming from a Java background and therefore I apologise if this is a non-issue. I have read the documentation and apparently it is expected that the original YAML file is updated after 'set_value' is called on the processor level. Is that correct?

If not, how can I save latest YAML to original file? In the below steps, after setting a value, we can check the value was updated in the code, however apparently the contents are not automatically dumped to the file.

How can I achieve that?

I appreciate your help, Thanks a lot.

Operating System

  1. Name/Distribution: Windows 11
  2. Version: running using git-bash

Version of Python and packages in use at the time of the issue.

  1. Distribution:
  2. Python Version: 3.8.8
  3. Version of yamlpath installed: 3.8.0
  4. Version of ruamel.yaml installed: 0.17.21

Minimum sample of YAML (or compatible) data necessary to trigger the issue

key1: key2: value

Complete steps to reproduce the issue when triggered via Libraries:

Following basic steps from documentation, read YAML using a Processor. Test that 'value' is retrieved when using 'get_nodes' -> either by iterating through the Generator or using next() Change value of same YAMLPath using 'set_value'. Check changed value using 'get_nodes'

Expected Outcome

New value (updated) is retrieved when last checking through 'get_nodes' Original YAML file is updated accordingly.

Actual Outcome

New value (updated) is retrieved when last checking through 'get_nodes' Original YAML file is NOT updated accordingly.

wwkimball commented 1 year ago

I come from a very long list of programming languages, including Java. You seem to have missed a fundamental programming precept (for all languages): It would be inefficient for any code to dump file buffer contents to storage after every operation against that buffer; the programmer may wish to perform many operations against the buffer and may even decide -- for their own reasons -- to abandon changes to the buffer before deciding whether to dump the buffer content back to storage. It is not up to the library to decide for the programmer when or whether to dump the content to storage.

It is up to you to write the buffer back to storage when you have finished making all of your changes to it and are happy with the result.

This project provides several reference implementations of the library's major capabilities as command-line tools. Feel free to "steal my code" (giving credit to where you stole it from). The atomic yaml-set tool provides an excellent example for how to write YAML/JSON file content back to storage when all operations have completed. It may be overkill for what you're trying to do, but it does cover several best-practices. Start at https://github.com/wwkimball/yamlpath/blob/master/yamlpath/commands/yaml_set.py#L664 and trace through the code to see how I very carefully handle writing the buffer back to storage, including overwriting the original file when the user indicates that is the desired outcome.