From 31fe1baacec127337140701face2e64a356075fd Mon Sep 17 00:00:00 2001 From: Kenta Mori Date: Fri, 26 Jan 2024 16:45:04 +0900 Subject: [PATCH] fix: remove any trailing empty lines if the block scalar has strip indicator (#421) --- decode_test.go | 16 ++++++++++++++++ scanner/context.go | 13 ++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/decode_test.go b/decode_test.go index 2373804..cabfd33 100644 --- a/decode_test.go +++ b/decode_test.go @@ -860,6 +860,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{ @@ -868,6 +876,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 { diff --git a/scanner/context.go b/scanner/context.go index 09d0a2d..3aaec56 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -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 }