Add --preserve for surgical, formatting-preserving json/yaml edits#6
Merged
Merged
Conversation
json/yaml set/del gain a --preserve (-P) flag that performs byte-level edits instead of the default decode/re-encode round-trip, so only the targeted node changes and everything else (comments and their column alignment, key order, quoting, indentation, blank lines) stays byte-for-byte identical. - YAML: locate the target via the yaml.Node source line/column and splice the new value into the original bytes; every splice is re-parsed and validated, falling back to the safe re-encode path on any mismatch. - JSON: splice the exact value span; set can also insert a new key into an existing object, formatting-matched to siblings; del drops the entry plus one adjacent comma. - In-place writes are atomic (temp file + fsync + rename) and preserve the original file mode. - toml/csv return a clear error in preserve mode. Default behavior is unchanged; --preserve is opt-in. Adds 29 service tests and 4 end-to-end tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a
--preserve(-P) flag tojson/yamlsetanddel. Instead of the default decode → mutate → re-encode round-trip (which drops comments, reorders keys, re-indents, and rewrites quoting),--preserveperforms a byte-level surgical edit: only the targeted node changes and everything else stays byte-for-byte identical — comments and their column alignment, key order, quoting style, indentation, and blank lines.Motivation: bumping e.g.
.image.tagin a hand-maintained Helmvalues.yamlshouldn't reformat the whole file.How
yaml.Nodesource line/column and splice the new value into the original bytes. Every splice is re-parsed and validated against the intended value; on any mismatch (e.g. a type-ambiguous plain-numeric edit) it falls back to the safe re-encode path, so a surgical edit can never corrupt or mis-edit the file.setcan also insert a new key into an existing object, formatting-matched to its siblings (mirrors comma/newline separator and colon spacing).delremoves the entry plus one adjacent comma.fsync+rename, preserving the original file's permission bits. A crash mid-write can't truncate the file.toml/csvreturn a clear error in preserve mode.Compatibility
Default behavior is unchanged;
--preserveis strictly opt-in.Examples
Tests
go vetclean.