Skip to content
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

fix: remove any trailing empty lines if the block scalar has strip indicator #421

Merged
merged 1 commit into from Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions decode_test.go
Expand Up @@ -848,6 +848,14 @@ func TestDecoder(t *testing.T) {
},
},
},
{
"v:\n- A\n- |-\n B\n C\n\n\n",
map[string][]string{
"v": {
"A", "B\nC",
},
},
},
{
"v:\n- A\n- >-\n B\n C\n",
map[string][]string{
Expand All @@ -856,6 +864,14 @@ func TestDecoder(t *testing.T) {
},
},
},
{
"v:\n- A\n- >-\n B\n C\n\n\n",
map[string][]string{
"v": {
"A", "B C",
},
},
},
{
"a: b\nc: d\n",
struct {
Expand Down
13 changes: 10 additions & 3 deletions scanner/context.go
Expand Up @@ -196,9 +196,16 @@ func (c *Context) existsBuffer() bool {

func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if len(src) > 0 && src[len(src)-1] == '\n' && c.isDocument() && c.literalOpt == "-" {
// remove end '\n' character
src = src[:len(src)-1]
if c.isDocument() && c.literalOpt == "-" {
// remove end '\n' character and trailing empty lines
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
for {
if len(src) > 0 && src[len(src)-1] == '\n' {
src = src[:len(src)-1]
continue
}
break
}
}
return src
}
Expand Down