Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/libyaml/representer.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ func (r *Representer) stringv(tag string, in reflect.Value) *Node {
case needsQuoting:
// Force quoting for YAML 1.1 compatibility values
style = SingleQuotedStyle
// and honor the configured quote preference.
if r.quotePreference.ScalarStyle() == DOUBLE_QUOTED_SCALAR_STYLE {
style = DoubleQuotedStyle
}
default:
// Plain style by default - Desolver will add quotes if type mismatch
style = 0
Expand Down
58 changes: 54 additions & 4 deletions testdata/encode.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
a: '1:1'
type: map[string]string
want: |
a: '1:1'
a: "1:1"

- encode:
name: null byte in string
Expand Down Expand Up @@ -396,15 +396,15 @@
'<<': []
type: map[string]any
want: |
'<<': []
"<<": []
Comment on lines 396 to +399

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something strange to me here.

There is no reason to use the double quote unless we are in v3 compatibility, right?

Or is it our test suite that forces this setting.

What do you think @colinjlacy ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @ccoVeille I missed the question mark there, I thought you were expecting @colinjlacy to answer.

Yes, it is the test suite. runEncodeTest uses yaml.Marshal, which always passes WithV3Defaults(), so the preference is QuoteLegacy, and QuoteLegacy means double quotes in the representer.

About v3 compatibility: v3 does not quote << at all, so it cannot tell us single or double here. I kept double because QuoteLegacy already gives it without a special case.

I also added two encode-opts tests for <<, one with single and one with double, so both preferences are now covered.

❯ make test-internal
go test ./internal/...    
ok      go.yaml.in/yaml/v4/internal/libyaml     (cached)
ok      go.yaml.in/yaml/v4/internal/testutil/assert     (cached)
?       go.yaml.in/yaml/v4/internal/testutil/datatest   [no test files]
ALL INTERNAL FILES PASS

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK thank you. I feel like we should move the test suites to v4 default then.

I'm not expecting this to be done in your PR.

We need to discuss it with other maintainers


- encode:
name: merge key as value
data:
foo: '<<'
type: map[string]any
want: |
foo: '<<'
foo: "<<"

# Long strings
- encode:
Expand Down Expand Up @@ -509,7 +509,7 @@
a: off
type: testStructA_String
want: |
a: 'off'
a: "off"

# Struct tests with yaml tags
- encode:
Expand Down Expand Up @@ -801,6 +801,26 @@
want: |
v: '-'

- encode-opts:
name: QuoteSingle - string "off" (YAML 1.1 bool, representer path)
data:
v: 'off'
type: map[string]string
opts:
required-quotes: single
want: |
v: 'off'

- encode-opts:
name: QuoteSingle - merge key "<<" (representer path)
data:
'<<': []
type: map[string]any
opts:
required-quotes: single
want: |
'<<': []

# WithQuotePreference tests - QuoteDouble
- encode-opts:
name: QuoteDouble - string "true"
Expand Down Expand Up @@ -882,6 +902,26 @@
want: |
v: "-"

- encode-opts:
name: QuoteDouble - string "off" (YAML 1.1 bool, representer path)
data:
v: 'off'
type: map[string]string
opts:
required-quotes: double
want: |
v: "off"

- encode-opts:
name: QuoteDouble - merge key "<<" (representer path)
data:
'<<': []
type: map[string]any
opts:
required-quotes: double
want: |
"<<": []

# WithQuotePreference tests - QuoteLegacy (v3 behavior)
# Legacy: bool-like strings (representer) → double quotes
# Legacy: whitespace strings (emitter) → single quotes
Expand Down Expand Up @@ -925,6 +965,16 @@
want: |
v: "123"

- encode-opts:
name: QuoteLegacy - string "off" (YAML 1.1 bool, double from representer)
data:
v: 'off'
type: map[string]string
opts:
required-quotes: legacy
want: |
v: "off"

- encode-opts:
name: QuoteLegacy - leading whitespace (single from emitter)
data:
Expand Down
Loading