-
-
Notifications
You must be signed in to change notification settings - Fork 760
Quote top-level string scalars whose bare form re-parses as a container #2656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
barry3406
wants to merge
1
commit into
mikefarah:master
Choose a base branch
from
barry3406:fix/quote-bare-string-scalar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestYamlEncoderUnwrapScalarRoundtripSafety verifies that a top-level string | ||
| // scalar whose unquoted form would re-parse as a non-scalar node (map or | ||
| // sequence) is emitted quoted even when UnwrapScalar is enabled. Safe plain | ||
| // strings continue to round-trip through the existing fast-path. See #2608. | ||
| func TestYamlEncoderUnwrapScalarRoundtripSafety(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| value string | ||
| wantBare bool // true: output equals value+"\n"; false: output must differ | ||
| }{ | ||
| {name: "colon_parses_as_map", value: "this: should really work"}, | ||
| {name: "dash_parses_as_seq", value: "- item"}, | ||
| {name: "multiline_maplike", value: "a: a\nb: b"}, | ||
| {name: "safe_plain_string", value: "hello world", wantBare: true}, | ||
| {name: "safe_identifier", value: "cat", wantBare: true}, | ||
| {name: "safe_digits_preserved", value: "123", wantBare: true}, | ||
| {name: "safe_null_word_preserved", value: "null", wantBare: true}, | ||
| {name: "safe_tag_shorthand_preserved", value: "!!int", wantBare: true}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| prefs := NewDefaultYamlPreferences() | ||
| prefs.UnwrapScalar = true | ||
|
|
||
| var buf bytes.Buffer | ||
| err := NewYamlEncoder(prefs).Encode(&buf, &CandidateNode{ | ||
| Kind: ScalarNode, | ||
| Tag: "!!str", | ||
| Value: tc.value, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("encode failed: %v", err) | ||
| } | ||
| got := buf.String() | ||
|
|
||
| if tc.wantBare { | ||
| if got != tc.value+"\n" { | ||
| t.Fatalf("expected bare %q, got %q", tc.value+"\n", got) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Ambiguous input: must not be emitted as the bare value. | ||
| if got == tc.value+"\n" { | ||
| t.Fatalf("value %q was emitted bare; expected quoted form", tc.value) | ||
| } | ||
|
|
||
| // The output must round-trip back to a string scalar with the | ||
| // same value, proving structural roundtrip safety. | ||
| decoder := NewYamlDecoder(NewDefaultYamlPreferences()) | ||
| nodes, err := readDocuments(strings.NewReader(got), "test.yaml", 0, decoder) | ||
| if err != nil { | ||
| t.Fatalf("decode of %q failed: %v", got, err) | ||
| } | ||
| if nodes.Len() != 1 { | ||
| t.Fatalf("expected one document, got %d", nodes.Len()) | ||
| } | ||
| candidate := nodes.Front().Value.(*CandidateNode) | ||
| // readDocuments wraps the document; descend to the scalar. | ||
| scalar := candidate | ||
| for scalar.Kind != ScalarNode && len(scalar.Content) == 1 { | ||
| scalar = scalar.Content[0] | ||
| } | ||
| if scalar.Kind != ScalarNode { | ||
| t.Fatalf("round-tripped node is not a scalar: kind=%v value=%q", scalar.Kind, scalar.Value) | ||
| } | ||
| if scalar.Tag != "!!str" { | ||
| t.Fatalf("round-tripped tag is %q, want !!str", scalar.Tag) | ||
| } | ||
| if scalar.Value != tc.value { | ||
| t.Fatalf("round-tripped value is %q, want %q", scalar.Value, tc.value) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what worries me:
a) how would you now emit this as a bare string if you wanted to;
b) this change could break a whole bunch of scripts / CI/CD pipelines for people
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a valid concern. A couple thoughts:
(a) Users can still get the raw value with
-o=raw/--outputformat=raw, which bypasses the yaml encoder entirely. So there is an escape hatch.(b) On breakage — this only kicks in for strings whose bare form re-parses as a map or sequence (like
a: bor- item). Plain strings, numbers, bools are untouched. So it's a pretty narrow change, but I get that even narrow changes can bite someone.If you'd prefer a flag to opt into this behavior instead of making it the default, I can rework it that way.