quotes: Fix quotes on ints and floats - #125
Open
justinvreeland wants to merge 1 commit into
Open
justinvreeland wants to merge 1 commit into
justinvreeland wants to merge 1 commit into
Conversation
justinvreeland
force-pushed
the
jvreeland-fix-quotes
branch
from
July 10, 2025 23:34
519067d to
e2e07a0
Compare
If the file that is supposed to be quoted is a float or int convert it to a string so that we can apply DoubleQuotes. Most fields in melange configs are strings anyway for replacements to work.
justinvreeland
force-pushed
the
jvreeland-fix-quotes
branch
from
July 10, 2025 23:36
e2e07a0 to
67371a7
Compare
This was referenced Oct 1, 2026
Open
stevebeattie
added a commit
that referenced
this pull request
Oct 2, 2026
## Summary This adds `TestQuoteScalars`, a table-driven regression test for how quote expressions (`--quote` / `quote:` in `.yam.yaml`) handle each kind of YAML scalar. Only tests change; formatter behavior stays the same. yaml.v3 already does the right thing here. When a scalar on a quote path isn't explicitly tagged, it drops the implicit tag and writes the value double-quoted with its original text, so `5.12`, `42`, `0x1F`, `.inf`, `true` and `2024-01-01` all become strings with no type coercion in yam. Until now only a single float fixture (`testdata/format/quotes.yaml`) covered quoting, and it didn't check other types, explicit tags, or the decoded type. That's how #125 came to propose a fix that wasn't needed. ## What's covered Each row checks three things: the exact formatted text, the decoded value (so a change that keeps the text but changes the type fails), and that formatting the output a second time changes nothing. - **Implicit scalars become quoted strings with their text unchanged:** float, int, hex and octal ints, `.inf`, `.NaN`, exponent floats, `1_000`, `true`, timestamps, and block literals. - **Strings:** plain strings, YAML 1.1 bool words (`yes`), single-quoted strings, and already double-quoted strings (left unchanged). - **Explicit tags are kept:** `!!float 5.12` becomes `!!float "5.12"`, and the same for `!!int`, `!!bool` and `!!str`. Quoting should never override a type the author wrote out. - **Things that stay attached:** anchors and line comments on quoted values. Nulls stay null. - **Paths:** several quote expressions all apply; `.v[]` quotes every sequence item; `.v[1]` quotes only that item. - **Paths that don't match leave values unchanged:** a sibling key, a same-depth path under a different parent (the mistake in the #100 repro), a deeper path, and a path to a sequence rather than its items. The block-literal row deliberately pins today's behavior: quoting a `|` scalar flattens it to one escaped line. A comment on the row says so. ## Verification - `go test ./...` and `go test -race ./pkg/yam/formatted` pass on main. - With #125's encoder change applied, exactly two rows fail (`!!float "5.12"` → `!!str "5.12"`, `!!int "42"` → `!!str "42"`). - I broke the code on purpose in several other ways, and each one fails the row written for it. Before this test, the first five passed the entire repo suite: - path matching that compares only the last key - applying only the first or only the last quote expression - quoting nulls into `"null"` - dropping line comments from quoted values - losing explicit tags (`|=` changed to `=`) - skipping quoting for strings - `gofmt -l .` is clean. `golangci-lint run --new-from-rev=origin/main` reports 0 issues. Related: #100, #125 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
If the file that is supposed to be quoted is a float or int convert it to a string so that we can apply DoubleQuotes. Most fields in melange configs are strings anyway for replacements to work.
Fixes: #100