Skip to content

quotes: Fix quotes on ints and floats - #125

Open
justinvreeland wants to merge 1 commit into
chainguard-dev:mainfrom
justinvreeland:jvreeland-fix-quotes
Open

justinvreeland wants to merge 1 commit into
chainguard-dev:mainfrom
justinvreeland:jvreeland-fix-quotes

Conversation

@justinvreeland

Copy link
Copy Markdown
Member

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

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.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

yam --quotes doesn't work for floats (and probably others)

1 participant